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));
165 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP6, sw_if_index);
167 case SR_BEHAVIOR_DX4:
168 ls->sw_if_index = sw_if_index;
169 clib_memcpy (&ls->next_hop.ip4, &nh_addr->ip4, sizeof (ip4_address_t));
171 case SR_BEHAVIOR_DX6:
172 ls->sw_if_index = sw_if_index;
173 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
175 case SR_BEHAVIOR_DT6:
176 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP6, sw_if_index);
178 case SR_BEHAVIOR_DT4:
179 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP4, sw_if_index);
181 case SR_BEHAVIOR_DX2:
182 ls->sw_if_index = sw_if_index;
183 ls->vlan_index = vlan_index;
187 /* Figure out the adjacency magic for Xconnect variants */
188 if (ls->behavior == SR_BEHAVIOR_X || ls->behavior == SR_BEHAVIOR_DX4
189 || ls->behavior == SR_BEHAVIOR_DX6)
191 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
193 /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
194 if (ls->behavior == SR_BEHAVIOR_DX6 || ls->behavior == SR_BEHAVIOR_X)
195 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
196 nh_addr, sw_if_index);
198 else if (ls->behavior == SR_BEHAVIOR_DX4)
199 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
200 nh_addr, sw_if_index);
202 /* Check for ADJ creation error. If so panic */
203 if (nh_adj_index == ADJ_INDEX_INVALID)
205 pool_put (sm->localsids, ls);
209 ls->nh_adj = nh_adj_index;
213 if (ls->behavior == SR_BEHAVIOR_END || ls->behavior == SR_BEHAVIOR_X)
214 dpo_set (&dpo, sr_localsid_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
215 else if (ls->behavior > SR_BEHAVIOR_D_FIRST
216 && ls->behavior < SR_BEHAVIOR_LAST)
217 dpo_set (&dpo, sr_localsid_d_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
218 else if (ls->behavior >= SR_BEHAVIOR_LAST)
220 sr_localsid_fn_registration_t *plugin = 0;
221 plugin = pool_elt_at_index (sm->plugin_functions,
222 ls->behavior - SR_BEHAVIOR_LAST);
223 /* Copy the unformat memory result */
224 ls->plugin_mem = ls_plugin_mem;
225 /* Callback plugin creation function */
226 rv = plugin->creation (ls);
229 pool_put (sm->localsids, ls);
232 dpo_set (&dpo, plugin->dpo, DPO_PROTO_IP6, ls - sm->localsids);
235 /* Set hash key for searching localsid by address */
236 mhash_set (&sm->sr_localsids_index_hash, localsid_addr, 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.t %u", &sw_if_index))
304 behavior = SR_BEHAVIOR_T;
305 else if (unformat (input, "end.dx6 %U %U",
306 unformat_vnet_sw_interface, vnm, &sw_if_index,
307 unformat_ip6_address, &next_hop.ip6))
308 behavior = SR_BEHAVIOR_DX6;
309 else if (unformat (input, "end.dx4 %U %U",
310 unformat_vnet_sw_interface, vnm, &sw_if_index,
311 unformat_ip4_address, &next_hop.ip4))
312 behavior = SR_BEHAVIOR_DX4;
313 else if (unformat (input, "end.dx2 %U",
314 unformat_vnet_sw_interface, vnm, &sw_if_index))
315 behavior = SR_BEHAVIOR_DX2;
316 else if (unformat (input, "end.dt6 %u", &sw_if_index))
317 behavior = SR_BEHAVIOR_DT6;
318 else if (unformat (input, "end.dt4 %u", &sw_if_index))
319 behavior = SR_BEHAVIOR_DT4;
322 /* Loop over all the plugin behavior format functions */
323 sr_localsid_fn_registration_t *plugin = 0, **vec_plugins = 0;
324 sr_localsid_fn_registration_t **plugin_it = 0;
326 /* Create a vector out of the plugin pool as recommended */
328 pool_foreach (plugin, sm->plugin_functions,
330 vec_add1 (vec_plugins, plugin);
334 vec_foreach (plugin_it, vec_plugins)
337 (input, "%U", (*plugin_it)->ls_unformat, &ls_plugin_mem))
339 behavior = (*plugin_it)->sr_localsid_function_number;
347 if (unformat (input, "end"))
348 behavior = SR_BEHAVIOR_END;
353 else if (!end_psp && unformat (input, "psp"))
359 if (!behavior && end_psp)
360 behavior = SR_BEHAVIOR_END;
363 return clib_error_return (0,
364 "Error: SRv6 LocalSID address is mandatory.");
365 if (!is_del && !behavior)
366 return clib_error_return (0,
367 "Error: SRv6 LocalSID behavior is mandatory.");
368 if (vlan_index != (u32) ~ 0)
369 return clib_error_return (0,
370 "Error: SRv6 End.DX2 with rewrite VLAN tag not supported by now.");
371 if (end_psp && !(behavior == SR_BEHAVIOR_END || behavior == SR_BEHAVIOR_X))
372 return clib_error_return (0,
373 "Error: SRv6 PSP only compatible with End and End.X");
375 rv = sr_cli_localsid (is_del, &resulting_address, end_psp, behavior,
376 sw_if_index, vlan_index, fib_index, &next_hop,
386 return clib_error_return (0,
387 "Identical localsid already exists. Requested localsid not created.");
389 return clib_error_return (0,
390 "The requested localsid could not be deleted. SR localsid not found");
392 return clib_error_return (0, "FIB table %u does not exist", fib_index);
394 return clib_error_return (0, "There is already one FIB entry for the"
395 "requested localsid non segment routing related");
397 return clib_error_return (0,
398 "Could not create ARP/ND entry for such next_hop. Internal error.");
400 return clib_error_return (0,
401 "Error on the plugin based localsid creation.");
403 return clib_error_return (0, "BUG: sr localsid returns %d", rv);
409 VLIB_CLI_COMMAND (sr_localsid_command, static) = {
410 .path = "sr localsid",
411 .short_help = "sr localsid (del) address XX:XX::YY:YY"
412 "(fib-table 8) behavior STRING",
414 "Create SR LocalSID and binds it to a particular behavior\n"
416 "\tlocalSID IPv6_addr(128b) LocalSID IPv6 address\n"
417 "\t(fib-table X) Optional. VRF where to install SRv6 localsid\n"
418 "\tbehavior STRING Specifies the behavior\n"
420 "\tEnd\t-> Endpoint.\n"
421 "\tEnd.X\t-> Endpoint with decapsulation and Layer-3 cross-connect.\n"
422 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
423 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
424 "\t\tParameters: '<iface>'\n"
425 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
426 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
427 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
428 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
429 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
430 "\t\tParameters: '<ip6_fib_table>'\n"
431 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
432 "\t\tParameters: '<ip4_fib_table>'\n",
433 .function = sr_cli_localsid_command_fn,
438 * @brief CLI function to 'show' all SR LocalSIDs on console.
440 static clib_error_t *
441 show_sr_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
442 vlib_cli_command_t * cmd)
444 vnet_main_t *vnm = vnet_get_main ();
445 ip6_sr_main_t *sm = &sr_main;
446 ip6_sr_localsid_t **localsid_list = 0;
447 ip6_sr_localsid_t *ls;
450 vlib_cli_output (vm, "SRv6 - My LocalSID Table:");
451 vlib_cli_output (vm, "=========================");
453 pool_foreach (ls, sm->localsids, ({ vec_add1 (localsid_list, ls); }));
455 for (i = 0; i < vec_len (localsid_list); i++)
457 ls = localsid_list[i];
458 switch (ls->behavior)
460 case SR_BEHAVIOR_END:
461 vlib_cli_output (vm, "\tAddress: \t%U\n\tBehavior: \tEnd",
462 format_ip6_address, &ls->localsid);
466 "\tAddress: \t%U\n\tBehavior: \tX (Endpoint with Layer-3 cross-connect)"
467 "\n\tIface: \t%U\n\tNext hop: \t%U",
468 format_ip6_address, &ls->localsid,
469 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
470 format_ip6_address, &ls->next_hop.ip6);
474 "\tAddress: \t%U\n\tBehavior: \tT (Endpoint with specific IPv6 table lookup)"
476 format_ip6_address, &ls->localsid,
477 format_vnet_sw_if_index_name, vnm, ls->vrf_index);
479 case SR_BEHAVIOR_DX4:
481 "\tAddress: \t%U\n\tBehavior: \tDX4 (Endpoint with decapsulation and IPv4 cross-connect)"
482 "\n\tIface: \t%U\n\tNext hop: \t%U",
483 format_ip6_address, &ls->localsid,
484 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
485 format_ip4_address, &ls->next_hop.ip4);
487 case SR_BEHAVIOR_DX6:
489 "\tAddress: \t%U\n\tBehavior: \tDX6 (Endpoint with decapsulation and IPv6 cross-connect)"
490 "\n\tIface: \t%U\n\tNext hop: \t%U",
491 format_ip6_address, &ls->localsid,
492 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
493 format_ip6_address, &ls->next_hop.ip6);
495 case SR_BEHAVIOR_DX2:
496 if (ls->vlan_index == (u32) ~ 0)
498 "\tAddress: \t%U\n\tBehavior: \tDX2 (Endpoint with decapulation and Layer-2 cross-connect)"
499 "\n\tIface: \t%U", format_ip6_address,
500 &ls->localsid, format_vnet_sw_if_index_name, vnm,
504 "Unsupported yet. (DX2 with egress VLAN rewrite)");
506 case SR_BEHAVIOR_DT6:
508 "\tAddress: \t%U\n\tBehavior: \tDT6 (Endpoint with decapsulation and specific IPv6 table lookup)"
509 "\n\tTable: %u", format_ip6_address, &ls->localsid,
512 case SR_BEHAVIOR_DT4:
514 "\tAddress: \t%U\n\tBehavior: \tDT4 (Endpoint with decapsulation and specific IPv4 table lookup)"
515 "\n\tTable: \t%u", format_ip6_address,
516 &ls->localsid, ls->vrf_index);
519 if (ls->behavior >= SR_BEHAVIOR_LAST)
521 sr_localsid_fn_registration_t *plugin =
522 pool_elt_at_index (sm->plugin_functions,
523 ls->behavior - SR_BEHAVIOR_LAST);
525 vlib_cli_output (vm, "\tAddress: \t%U\n"
526 "\tBehavior: \t%s (%s)\n\t%U",
527 format_ip6_address, &ls->localsid,
528 plugin->keyword_str, plugin->def_str,
529 plugin->ls_format, ls->plugin_mem);
532 //Should never get here...
533 vlib_cli_output (vm, "Internal error");
537 vlib_cli_output (vm, "\tPSP: \tTrue\n");
540 vlib_counter_t valid, invalid;
541 vlib_get_combined_counter (&(sm->sr_ls_valid_counters), i, &valid);
542 vlib_get_combined_counter (&(sm->sr_ls_invalid_counters), i, &invalid);
543 vlib_cli_output (vm, "\tGood traffic: \t[%Ld packets : %Ld bytes]\n",
544 valid.packets, valid.bytes);
545 vlib_cli_output (vm, "\tBad traffic: \t[%Ld packets : %Ld bytes]\n",
546 invalid.packets, invalid.bytes);
547 vlib_cli_output (vm, "--------------------");
553 VLIB_CLI_COMMAND (show_sr_localsid_command, static) = {
554 .path = "show sr localsids",
555 .short_help = "show sr localsids",
556 .function = show_sr_localsid_command_fn,
561 * @brief Function to 'clear' ALL SR localsid counters
563 static clib_error_t *
564 clear_sr_localsid_counters_command_fn (vlib_main_t * vm,
565 unformat_input_t * input,
566 vlib_cli_command_t * cmd)
568 ip6_sr_main_t *sm = &sr_main;
570 vlib_clear_combined_counters (&(sm->sr_ls_valid_counters));
571 vlib_clear_combined_counters (&(sm->sr_ls_invalid_counters));
577 VLIB_CLI_COMMAND (clear_sr_localsid_counters_command, static) = {
578 .path = "clear sr localsid-counters",
579 .short_help = "clear sr localsid-counters",
580 .function = clear_sr_localsid_counters_command_fn,
584 /************************ SR LocalSID graphs node ****************************/
586 * @brief SR localsid node trace
590 ip6_address_t localsid;
595 } sr_localsid_trace_t;
597 #define foreach_sr_localsid_error \
598 _(NO_INNER_HEADER, "(SR-Error) No inner IP header") \
599 _(NO_MORE_SEGMENTS, "(SR-Error) No more segments") \
600 _(NO_SRH, "(SR-Error) No SR header") \
601 _(NO_PSP, "(SR-Error) PSP Not available (segments left > 0)") \
602 _(NOT_LS, "(SR-Error) Decaps not available (segments left > 0)") \
603 _(L2, "(SR-Error) SRv6 decapsulated a L2 frame without dest")
607 #define _(sym,str) SR_LOCALSID_ERROR_##sym,
608 foreach_sr_localsid_error
611 } sr_localsid_error_t;
613 static char *sr_localsid_error_strings[] = {
614 #define _(sym,string) string,
615 foreach_sr_localsid_error
619 #define foreach_sr_localsid_next \
620 _(ERROR, "error-drop") \
621 _(IP6_LOOKUP, "ip6-lookup") \
622 _(IP4_LOOKUP, "ip4-lookup") \
623 _(IP6_REWRITE, "ip6-rewrite") \
624 _(IP4_REWRITE, "ip4-rewrite") \
625 _(INTERFACE_OUTPUT, "interface-output")
629 #define _(s,n) SR_LOCALSID_NEXT_##s,
630 foreach_sr_localsid_next
633 } sr_localsid_next_t;
636 * @brief SR LocalSID graph node trace function
641 format_sr_localsid_trace (u8 * s, va_list * args)
643 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
644 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
645 sr_localsid_trace_t *t = va_arg (*args, sr_localsid_trace_t *);
648 format (s, "SR-LOCALSID:\n\tLocalsid: %U\n", format_ip6_address,
652 case SR_BEHAVIOR_END:
653 s = format (s, "\tBehavior: End\n");
655 case SR_BEHAVIOR_DX6:
656 s = format (s, "\tBehavior: Decapsulation with IPv6 L3 xconnect\n");
658 case SR_BEHAVIOR_DX4:
659 s = format (s, "\tBehavior: Decapsulation with IPv4 L3 xconnect\n");
662 s = format (s, "\tBehavior: IPv6 L3 xconnect\n");
665 s = format (s, "\tBehavior: IPv6 specific table lookup\n");
667 case SR_BEHAVIOR_DT6:
668 s = format (s, "\tBehavior: Decapsulation with IPv6 Table lookup\n");
670 case SR_BEHAVIOR_DT4:
671 s = format (s, "\tBehavior: Decapsulation with IPv4 Table lookup\n");
673 case SR_BEHAVIOR_DX2:
674 s = format (s, "\tBehavior: Decapsulation with L2 xconnect\n");
677 s = format (s, "\tBehavior: defined in plugin\n"); //TODO
680 if (t->num_segments != 0xFF)
682 if (t->num_segments > 0)
684 s = format (s, "\tSegments left: %d\n", t->segments_left);
685 s = format (s, "\tSID list: [in ietf order]");
687 for (i = 0; i < t->num_segments; i++)
689 s = format (s, "\n\t-> %U", format_ip6_address,
690 (ip6_address_t *) & t->sr[i *
691 sizeof (ip6_address_t)]);
699 * @brief Function doing End processing.
701 static_always_inline void
702 end_srh_processing (vlib_node_runtime_t * node,
705 ip6_sr_header_t * sr0,
706 ip6_sr_localsid_t * ls0,
707 u32 * next0, u8 psp, ip6_ext_header_t * prev0)
709 ip6_address_t *new_dst0;
711 if (PREDICT_TRUE (sr0->type == ROUTING_HEADER_TYPE_SR))
713 if (sr0->segments_left == 1 && psp)
716 u64 *copy_dst0, *copy_src0;
717 u32 copy_len_u64s0 = 0;
719 ip0->dst_address.as_u64[0] = sr0->segments->as_u64[0];
720 ip0->dst_address.as_u64[1] = sr0->segments->as_u64[1];
722 /* Remove the SRH taking care of the rest of IPv6 ext header */
724 prev0->next_hdr = sr0->protocol;
726 ip0->protocol = sr0->protocol;
728 sr_len = ip6_ext_header_len (sr0);
729 vlib_buffer_advance (b0, sr_len);
730 new_l0 = clib_net_to_host_u16 (ip0->payload_length) - sr_len;
731 ip0->payload_length = clib_host_to_net_u16 (new_l0);
732 copy_src0 = (u64 *) ip0;
733 copy_dst0 = copy_src0 + (sr0->length + 1);
734 /* number of 8 octet units to copy
735 * By default in absence of extension headers it is equal to length of ip6 header
736 * With extension headers it number of 8 octet units of ext headers preceding
740 (((u8 *) sr0 - (u8 *) ip0) - sizeof (ip6_header_t)) >> 3;
741 copy_dst0[4 + copy_len_u64s0] = copy_src0[4 + copy_len_u64s0];
742 copy_dst0[3 + copy_len_u64s0] = copy_src0[3 + copy_len_u64s0];
743 copy_dst0[2 + copy_len_u64s0] = copy_src0[2 + copy_len_u64s0];
744 copy_dst0[1 + copy_len_u64s0] = copy_src0[1 + copy_len_u64s0];
745 copy_dst0[0 + copy_len_u64s0] = copy_src0[0 + copy_len_u64s0];
748 for (i = copy_len_u64s0 - 1; i >= 0; i--)
750 copy_dst0[i] = copy_src0[i];
753 if (ls0->behavior == SR_BEHAVIOR_X)
755 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
756 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
758 else if (ls0->behavior == SR_BEHAVIOR_T)
760 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
763 else if (PREDICT_TRUE (sr0->segments_left > 0))
765 sr0->segments_left -= 1;
766 new_dst0 = (ip6_address_t *) (sr0->segments);
767 new_dst0 += sr0->segments_left;
768 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
769 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
771 if (ls0->behavior == SR_BEHAVIOR_X)
773 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
774 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
776 else if (ls0->behavior == SR_BEHAVIOR_T)
778 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
783 *next0 = SR_LOCALSID_NEXT_ERROR;
784 b0->error = node->errors[SR_LOCALSID_ERROR_NO_MORE_SEGMENTS];
789 /* Error. Routing header of type != SR */
790 *next0 = SR_LOCALSID_NEXT_ERROR;
791 b0->error = node->errors[SR_LOCALSID_ERROR_NO_SRH];
796 * @brief Function doing SRH processing for D* variants
798 static_always_inline void
799 end_decaps_srh_processing (vlib_node_runtime_t * node,
802 ip6_sr_header_t * sr0,
803 ip6_sr_localsid_t * ls0, u32 * next0)
805 /* Compute the size of the IPv6 header with all Ext. headers */
807 ip6_ext_header_t *next_ext_header;
810 next_proto = ip0->protocol;
811 next_ext_header = (void *) (ip0 + 1);
812 total_size = sizeof (ip6_header_t);
813 while (ip6_ext_hdr (next_proto))
815 total_size += ip6_ext_header_len (next_ext_header);
816 next_proto = next_ext_header->next_hdr;
817 next_ext_header = ip6_ext_next_header (next_ext_header);
820 /* Ensure this is the last segment. Otherwise drop. */
821 if (sr0 && sr0->segments_left != 0)
823 *next0 = SR_LOCALSID_NEXT_ERROR;
824 b0->error = node->errors[SR_LOCALSID_ERROR_NOT_LS];
830 case IP_PROTOCOL_IPV6:
831 /* Encap-End IPv6. Pop outer IPv6 header. */
832 if (ls0->behavior == SR_BEHAVIOR_DX6)
834 vlib_buffer_advance (b0, total_size);
835 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
836 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
839 else if (ls0->behavior == SR_BEHAVIOR_DT6)
841 vlib_buffer_advance (b0, total_size);
842 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
846 case IP_PROTOCOL_IP_IN_IP:
847 /* Encap-End IPv4. Pop outer IPv6 header */
848 if (ls0->behavior == SR_BEHAVIOR_DX4)
850 vlib_buffer_advance (b0, total_size);
851 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
852 *next0 = SR_LOCALSID_NEXT_IP4_REWRITE;
855 else if (ls0->behavior == SR_BEHAVIOR_DT4)
857 vlib_buffer_advance (b0, total_size);
858 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
859 *next0 = SR_LOCALSID_NEXT_IP4_LOOKUP;
863 case IP_PROTOCOL_IP6_NONXT:
865 if (ls0->behavior == SR_BEHAVIOR_DX2)
867 vlib_buffer_advance (b0, total_size);
868 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->sw_if_index;
869 *next0 = SR_LOCALSID_NEXT_INTERFACE_OUTPUT;
874 *next0 = SR_LOCALSID_NEXT_ERROR;
875 b0->error = node->errors[SR_LOCALSID_ERROR_NO_INNER_HEADER];
880 * @brief SR LocalSID graph node. Supports all default SR Endpoint variants with decaps
883 sr_localsid_d_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
884 vlib_frame_t * from_frame)
886 u32 n_left_from, next_index, *from, *to_next;
887 ip6_sr_main_t *sm = &sr_main;
888 from = vlib_frame_vector_args (from_frame);
889 n_left_from = from_frame->n_vectors;
890 next_index = node->cached_next_index;
891 u32 thread_index = vlib_get_thread_index ();
893 while (n_left_from > 0)
896 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
899 while (n_left_from >= 8 && n_left_to_next >= 4)
901 u32 bi0, bi1, bi2, bi3;
902 vlib_buffer_t *b0, *b1, *b2, *b3;
903 ip6_header_t *ip0, *ip1, *ip2, *ip3;
904 ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
905 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
906 u32 next0, next1, next2, next3;
907 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
908 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
910 /* Prefetch next iteration. */
912 vlib_buffer_t *p4, *p5, *p6, *p7;
914 p4 = vlib_get_buffer (vm, from[4]);
915 p5 = vlib_get_buffer (vm, from[5]);
916 p6 = vlib_get_buffer (vm, from[6]);
917 p7 = vlib_get_buffer (vm, from[7]);
919 /* Prefetch the buffer header and packet for the N+4 loop iteration */
920 vlib_prefetch_buffer_header (p4, LOAD);
921 vlib_prefetch_buffer_header (p5, LOAD);
922 vlib_prefetch_buffer_header (p6, LOAD);
923 vlib_prefetch_buffer_header (p7, LOAD);
925 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
926 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
927 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
928 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
931 to_next[0] = bi0 = from[0];
932 to_next[1] = bi1 = from[1];
933 to_next[2] = bi2 = from[2];
934 to_next[3] = bi3 = from[3];
940 b0 = vlib_get_buffer (vm, bi0);
941 b1 = vlib_get_buffer (vm, bi1);
942 b2 = vlib_get_buffer (vm, bi2);
943 b3 = vlib_get_buffer (vm, bi3);
946 pool_elt_at_index (sm->localsids,
947 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
949 pool_elt_at_index (sm->localsids,
950 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
952 pool_elt_at_index (sm->localsids,
953 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
955 pool_elt_at_index (sm->localsids,
956 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
958 ip0 = vlib_buffer_get_current (b0);
959 ip1 = vlib_buffer_get_current (b1);
960 ip2 = vlib_buffer_get_current (b2);
961 ip3 = vlib_buffer_get_current (b3);
963 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
964 ip6_ext_header_find_t (ip1, prev1, sr1, IP_PROTOCOL_IPV6_ROUTE);
965 ip6_ext_header_find_t (ip2, prev2, sr2, IP_PROTOCOL_IPV6_ROUTE);
966 ip6_ext_header_find_t (ip3, prev3, sr3, IP_PROTOCOL_IPV6_ROUTE);
968 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
969 end_decaps_srh_processing (node, b1, ip1, sr1, ls1, &next1);
970 end_decaps_srh_processing (node, b2, ip2, sr2, ls2, &next2);
971 end_decaps_srh_processing (node, b3, ip3, sr3, ls3, &next3);
973 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
975 sr_localsid_trace_t *tr =
976 vlib_add_trace (vm, node, b0, sizeof (*tr));
977 tr->num_segments = 0;
978 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
979 sizeof (tr->localsid.as_u8));
980 tr->behavior = ls0->behavior;
981 if (ip0 == vlib_buffer_get_current (b0))
983 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
984 && sr0->type == ROUTING_HEADER_TYPE_SR)
986 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
988 sr0->length * 8 / sizeof (ip6_address_t);
989 tr->segments_left = sr0->segments_left;
993 tr->num_segments = 0xFF;
996 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
998 sr_localsid_trace_t *tr =
999 vlib_add_trace (vm, node, b1, sizeof (*tr));
1000 tr->num_segments = 0;
1001 clib_memcpy (tr->localsid.as_u8, ls1->localsid.as_u8,
1002 sizeof (tr->localsid.as_u8));
1003 tr->behavior = ls1->behavior;
1004 if (ip1 == vlib_buffer_get_current (b1))
1006 if (ip1->protocol == IP_PROTOCOL_IPV6_ROUTE
1007 && sr1->type == ROUTING_HEADER_TYPE_SR)
1009 clib_memcpy (tr->sr, sr1->segments, sr1->length * 8);
1011 sr1->length * 8 / sizeof (ip6_address_t);
1012 tr->segments_left = sr1->segments_left;
1016 tr->num_segments = 0xFF;
1019 if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
1021 sr_localsid_trace_t *tr =
1022 vlib_add_trace (vm, node, b2, sizeof (*tr));
1023 tr->num_segments = 0;
1024 clib_memcpy (tr->localsid.as_u8, ls2->localsid.as_u8,
1025 sizeof (tr->localsid.as_u8));
1026 tr->behavior = ls2->behavior;
1027 if (ip2 == vlib_buffer_get_current (b2))
1029 if (ip2->protocol == IP_PROTOCOL_IPV6_ROUTE
1030 && sr2->type == ROUTING_HEADER_TYPE_SR)
1032 clib_memcpy (tr->sr, sr2->segments, sr2->length * 8);
1034 sr2->length * 8 / sizeof (ip6_address_t);
1035 tr->segments_left = sr2->segments_left;
1039 tr->num_segments = 0xFF;
1042 if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
1044 sr_localsid_trace_t *tr =
1045 vlib_add_trace (vm, node, b3, sizeof (*tr));
1046 tr->num_segments = 0;
1047 clib_memcpy (tr->localsid.as_u8, ls3->localsid.as_u8,
1048 sizeof (tr->localsid.as_u8));
1049 tr->behavior = ls3->behavior;
1050 if (ip3 == vlib_buffer_get_current (b3))
1052 if (ip3->protocol == IP_PROTOCOL_IPV6_ROUTE
1053 && sr3->type == ROUTING_HEADER_TYPE_SR)
1055 clib_memcpy (tr->sr, sr3->segments, sr3->length * 8);
1057 sr3->length * 8 / sizeof (ip6_address_t);
1058 tr->segments_left = sr3->segments_left;
1062 tr->num_segments = 0xFF;
1065 vlib_increment_combined_counter
1067 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1068 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1069 1, vlib_buffer_length_in_chain (vm, b0));
1071 vlib_increment_combined_counter
1073 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1074 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1075 1, vlib_buffer_length_in_chain (vm, b1));
1077 vlib_increment_combined_counter
1079 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1080 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1081 1, vlib_buffer_length_in_chain (vm, b2));
1083 vlib_increment_combined_counter
1085 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1086 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1087 1, vlib_buffer_length_in_chain (vm, b3));
1089 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1090 n_left_to_next, bi0, bi1, bi2, bi3,
1091 next0, next1, next2, next3);
1094 /* Single loop for potentially the last three packets */
1095 while (n_left_from > 0 && n_left_to_next > 0)
1100 ip6_ext_header_t *prev0;
1101 ip6_sr_header_t *sr0;
1102 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1103 ip6_sr_localsid_t *ls0;
1110 n_left_to_next -= 1;
1112 b0 = vlib_get_buffer (vm, bi0);
1113 ip0 = vlib_buffer_get_current (b0);
1115 /* Lookup the SR End behavior based on IP DA (adj) */
1117 pool_elt_at_index (sm->localsids,
1118 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1120 /* Find SRH as well as previous header */
1121 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1123 /* SRH processing and End variants */
1124 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1126 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1128 sr_localsid_trace_t *tr =
1129 vlib_add_trace (vm, node, b0, sizeof (*tr));
1130 tr->num_segments = 0;
1131 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1132 sizeof (tr->localsid.as_u8));
1133 tr->behavior = ls0->behavior;
1134 if (ip0 == vlib_buffer_get_current (b0))
1136 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1137 && sr0->type == ROUTING_HEADER_TYPE_SR)
1139 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1141 sr0->length * 8 / sizeof (ip6_address_t);
1142 tr->segments_left = sr0->segments_left;
1146 tr->num_segments = 0xFF;
1149 /* Increase the counters */
1150 vlib_increment_combined_counter
1152 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1153 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1154 1, vlib_buffer_length_in_chain (vm, b0));
1156 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1157 n_left_to_next, bi0, next0);
1159 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1161 return from_frame->n_vectors;
1165 VLIB_REGISTER_NODE (sr_localsid_d_node) = {
1166 .function = sr_localsid_d_fn,
1167 .name = "sr-localsid-d",
1168 .vector_size = sizeof (u32),
1169 .format_trace = format_sr_localsid_trace,
1170 .type = VLIB_NODE_TYPE_INTERNAL,
1171 .n_errors = SR_LOCALSID_N_ERROR,
1172 .error_strings = sr_localsid_error_strings,
1173 .n_next_nodes = SR_LOCALSID_N_NEXT,
1175 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1176 foreach_sr_localsid_next
1183 * @brief SR LocalSID graph node. Supports all default SR Endpoint without decaps
1186 sr_localsid_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1187 vlib_frame_t * from_frame)
1189 u32 n_left_from, next_index, *from, *to_next;
1190 ip6_sr_main_t *sm = &sr_main;
1191 from = vlib_frame_vector_args (from_frame);
1192 n_left_from = from_frame->n_vectors;
1193 next_index = node->cached_next_index;
1194 u32 thread_index = vlib_get_thread_index ();
1196 while (n_left_from > 0)
1199 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1202 while (n_left_from >= 8 && n_left_to_next >= 4)
1204 u32 bi0, bi1, bi2, bi3;
1205 vlib_buffer_t *b0, *b1, *b2, *b3;
1206 ip6_header_t *ip0, *ip1, *ip2, *ip3;
1207 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
1208 ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
1209 u32 next0, next1, next2, next3;
1210 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1211 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
1213 /* Prefetch next iteration. */
1215 vlib_buffer_t *p4, *p5, *p6, *p7;
1217 p4 = vlib_get_buffer (vm, from[4]);
1218 p5 = vlib_get_buffer (vm, from[5]);
1219 p6 = vlib_get_buffer (vm, from[6]);
1220 p7 = vlib_get_buffer (vm, from[7]);
1222 /* Prefetch the buffer header and packet for the N+2 loop iteration */
1223 vlib_prefetch_buffer_header (p4, LOAD);
1224 vlib_prefetch_buffer_header (p5, LOAD);
1225 vlib_prefetch_buffer_header (p6, LOAD);
1226 vlib_prefetch_buffer_header (p7, LOAD);
1228 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
1229 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
1230 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
1231 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
1234 to_next[0] = bi0 = from[0];
1235 to_next[1] = bi1 = from[1];
1236 to_next[2] = bi2 = from[2];
1237 to_next[3] = bi3 = from[3];
1241 n_left_to_next -= 4;
1243 b0 = vlib_get_buffer (vm, bi0);
1244 b1 = vlib_get_buffer (vm, bi1);
1245 b2 = vlib_get_buffer (vm, bi2);
1246 b3 = vlib_get_buffer (vm, bi3);
1248 ip0 = vlib_buffer_get_current (b0);
1249 ip1 = vlib_buffer_get_current (b1);
1250 ip2 = vlib_buffer_get_current (b2);
1251 ip3 = vlib_buffer_get_current (b3);
1253 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1254 ip6_ext_header_find_t (ip1, prev1, sr1, IP_PROTOCOL_IPV6_ROUTE);
1255 ip6_ext_header_find_t (ip2, prev2, sr2, IP_PROTOCOL_IPV6_ROUTE);
1256 ip6_ext_header_find_t (ip3, prev3, sr3, IP_PROTOCOL_IPV6_ROUTE);
1259 pool_elt_at_index (sm->localsids,
1260 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1262 pool_elt_at_index (sm->localsids,
1263 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1265 pool_elt_at_index (sm->localsids,
1266 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1268 pool_elt_at_index (sm->localsids,
1269 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1271 end_srh_processing (node, b0, ip0, sr0, ls0, &next0, ls0->end_psp,
1273 end_srh_processing (node, b1, ip1, sr1, ls1, &next1, ls1->end_psp,
1275 end_srh_processing (node, b2, ip2, sr2, ls2, &next2, ls2->end_psp,
1277 end_srh_processing (node, b3, ip3, sr3, ls3, &next3, ls3->end_psp,
1280 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1282 sr_localsid_trace_t *tr =
1283 vlib_add_trace (vm, node, b0, sizeof (*tr));
1284 tr->num_segments = 0;
1285 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1286 sizeof (tr->localsid.as_u8));
1287 tr->behavior = ls0->behavior;
1288 if (ip0 == vlib_buffer_get_current (b0))
1290 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1291 && sr0->type == ROUTING_HEADER_TYPE_SR)
1293 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1295 sr0->length * 8 / sizeof (ip6_address_t);
1296 tr->segments_left = sr0->segments_left;
1300 tr->num_segments = 0xFF;
1303 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
1305 sr_localsid_trace_t *tr =
1306 vlib_add_trace (vm, node, b1, sizeof (*tr));
1307 tr->num_segments = 0;
1308 clib_memcpy (tr->localsid.as_u8, ls1->localsid.as_u8,
1309 sizeof (tr->localsid.as_u8));
1310 tr->behavior = ls1->behavior;
1311 if (ip1 == vlib_buffer_get_current (b1))
1313 if (ip1->protocol == IP_PROTOCOL_IPV6_ROUTE
1314 && sr1->type == ROUTING_HEADER_TYPE_SR)
1316 clib_memcpy (tr->sr, sr1->segments, sr1->length * 8);
1318 sr1->length * 8 / sizeof (ip6_address_t);
1319 tr->segments_left = sr1->segments_left;
1323 tr->num_segments = 0xFF;
1326 if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
1328 sr_localsid_trace_t *tr =
1329 vlib_add_trace (vm, node, b2, sizeof (*tr));
1330 tr->num_segments = 0;
1331 clib_memcpy (tr->localsid.as_u8, ls2->localsid.as_u8,
1332 sizeof (tr->localsid.as_u8));
1333 tr->behavior = ls2->behavior;
1334 if (ip2 == vlib_buffer_get_current (b2))
1336 if (ip2->protocol == IP_PROTOCOL_IPV6_ROUTE
1337 && sr2->type == ROUTING_HEADER_TYPE_SR)
1339 clib_memcpy (tr->sr, sr2->segments, sr2->length * 8);
1341 sr2->length * 8 / sizeof (ip6_address_t);
1342 tr->segments_left = sr2->segments_left;
1346 tr->num_segments = 0xFF;
1349 if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
1351 sr_localsid_trace_t *tr =
1352 vlib_add_trace (vm, node, b3, sizeof (*tr));
1353 tr->num_segments = 0;
1354 clib_memcpy (tr->localsid.as_u8, ls3->localsid.as_u8,
1355 sizeof (tr->localsid.as_u8));
1356 tr->behavior = ls3->behavior;
1357 if (ip3 == vlib_buffer_get_current (b3))
1359 if (ip3->protocol == IP_PROTOCOL_IPV6_ROUTE
1360 && sr3->type == ROUTING_HEADER_TYPE_SR)
1362 clib_memcpy (tr->sr, sr3->segments, sr3->length * 8);
1364 sr3->length * 8 / sizeof (ip6_address_t);
1365 tr->segments_left = sr3->segments_left;
1369 tr->num_segments = 0xFF;
1372 vlib_increment_combined_counter
1374 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1375 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1376 1, vlib_buffer_length_in_chain (vm, b0));
1378 vlib_increment_combined_counter
1380 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1381 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1382 1, vlib_buffer_length_in_chain (vm, b1));
1384 vlib_increment_combined_counter
1386 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1387 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1388 1, vlib_buffer_length_in_chain (vm, b2));
1390 vlib_increment_combined_counter
1392 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1393 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1394 1, vlib_buffer_length_in_chain (vm, b3));
1396 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1397 n_left_to_next, bi0, bi1, bi2, bi3,
1398 next0, next1, next2, next3);
1401 /* Single loop for potentially the last three packets */
1402 while (n_left_from > 0 && n_left_to_next > 0)
1406 ip6_header_t *ip0 = 0;
1407 ip6_ext_header_t *prev0;
1408 ip6_sr_header_t *sr0;
1409 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1410 ip6_sr_localsid_t *ls0;
1417 n_left_to_next -= 1;
1419 b0 = vlib_get_buffer (vm, bi0);
1420 ip0 = vlib_buffer_get_current (b0);
1421 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1423 /* Lookup the SR End behavior based on IP DA (adj) */
1425 pool_elt_at_index (sm->localsids,
1426 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1428 /* SRH processing */
1429 end_srh_processing (node, b0, ip0, sr0, ls0, &next0, ls0->end_psp,
1432 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1434 sr_localsid_trace_t *tr =
1435 vlib_add_trace (vm, node, b0, sizeof (*tr));
1436 tr->num_segments = 0;
1437 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1438 sizeof (tr->localsid.as_u8));
1439 tr->behavior = ls0->behavior;
1440 if (ip0 == vlib_buffer_get_current (b0))
1442 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1443 && sr0->type == ROUTING_HEADER_TYPE_SR)
1445 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1447 sr0->length * 8 / sizeof (ip6_address_t);
1448 tr->segments_left = sr0->segments_left;
1452 tr->num_segments = 0xFF;
1455 vlib_increment_combined_counter
1457 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1458 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1459 1, vlib_buffer_length_in_chain (vm, b0));
1461 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1462 n_left_to_next, bi0, next0);
1464 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1466 return from_frame->n_vectors;
1470 VLIB_REGISTER_NODE (sr_localsid_node) = {
1471 .function = sr_localsid_fn,
1472 .name = "sr-localsid",
1473 .vector_size = sizeof (u32),
1474 .format_trace = format_sr_localsid_trace,
1475 .type = VLIB_NODE_TYPE_INTERNAL,
1476 .n_errors = SR_LOCALSID_N_ERROR,
1477 .error_strings = sr_localsid_error_strings,
1478 .n_next_nodes = SR_LOCALSID_N_NEXT,
1480 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1481 foreach_sr_localsid_next
1488 format_sr_dpo (u8 * s, va_list * args)
1490 index_t index = va_arg (*args, index_t);
1491 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
1493 return (format (s, "SR: localsid_index:[%d]", index));
1496 const static dpo_vft_t sr_loc_vft = {
1497 .dv_lock = sr_dpo_lock,
1498 .dv_unlock = sr_dpo_unlock,
1499 .dv_format = format_sr_dpo,
1502 const static char *const sr_loc_ip6_nodes[] = {
1507 const static char *const *const sr_loc_nodes[DPO_PROTO_NUM] = {
1508 [DPO_PROTO_IP6] = sr_loc_ip6_nodes,
1511 const static char *const sr_loc_d_ip6_nodes[] = {
1516 const static char *const *const sr_loc_d_nodes[DPO_PROTO_NUM] = {
1517 [DPO_PROTO_IP6] = sr_loc_d_ip6_nodes,
1521 /*************************** SR LocalSID plugins ******************************/
1523 * @brief SR LocalSID plugin registry
1526 sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name,
1527 u8 * keyword_str, u8 * def_str,
1528 u8 * params_str, dpo_type_t * dpo,
1529 format_function_t * ls_format,
1530 unformat_function_t * ls_unformat,
1531 sr_plugin_callback_t * creation_fn,
1532 sr_plugin_callback_t * removal_fn)
1534 ip6_sr_main_t *sm = &sr_main;
1537 sr_localsid_fn_registration_t *plugin;
1539 /* Did this function exist? If so update it */
1540 p = hash_get_mem (sm->plugin_functions_by_key, fn_name);
1543 plugin = pool_elt_at_index (sm->plugin_functions, p[0]);
1545 /* Else create a new one and set hash key */
1548 pool_get (sm->plugin_functions, plugin);
1549 hash_set_mem (sm->plugin_functions_by_key, fn_name,
1550 plugin - sm->plugin_functions);
1553 memset (plugin, 0, sizeof (*plugin));
1555 plugin->sr_localsid_function_number = (plugin - sm->plugin_functions);
1556 plugin->sr_localsid_function_number += SR_BEHAVIOR_LAST;
1557 plugin->ls_format = ls_format;
1558 plugin->ls_unformat = ls_unformat;
1559 plugin->creation = creation_fn;
1560 plugin->removal = removal_fn;
1561 clib_memcpy (&plugin->dpo, dpo, sizeof (dpo_type_t));
1562 plugin->function_name = format (0, "%s%c", fn_name, 0);
1563 plugin->keyword_str = format (0, "%s%c", keyword_str, 0);
1564 plugin->def_str = format (0, "%s%c", def_str, 0);
1565 plugin->params_str = format (0, "%s%c", params_str, 0);
1567 return plugin->sr_localsid_function_number;
1571 * @brief CLI function to 'show' all available SR LocalSID behaviors
1573 static clib_error_t *
1574 show_sr_localsid_behaviors_command_fn (vlib_main_t * vm,
1575 unformat_input_t * input,
1576 vlib_cli_command_t * cmd)
1578 ip6_sr_main_t *sm = &sr_main;
1579 sr_localsid_fn_registration_t *plugin;
1580 sr_localsid_fn_registration_t **plugins_vec = 0;
1583 vlib_cli_output (vm,
1584 "SR LocalSIDs behaviors:\n-----------------------\n\n");
1587 pool_foreach (plugin, sm->plugin_functions,
1588 ({ vec_add1 (plugins_vec, plugin); }));
1591 /* Print static behaviors */
1592 vlib_cli_output (vm, "Default behaviors:\n"
1593 "\tEnd\t-> Endpoint.\n"
1594 "\tEnd.X\t-> Endpoint with Layer-3 cross-connect.\n"
1595 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1596 "\tEnd.T\t-> Endpoint with specific IPv6 table lookup.\n"
1597 "\t\tParameters: '<fib_table>'\n"
1598 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
1599 "\t\tParameters: '<iface>'\n"
1600 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
1601 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1602 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
1603 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
1604 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
1605 "\t\tParameters: '<ip6_fib_table>'\n"
1606 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
1607 "\t\tParameters: '<ip4_fib_table>'\n");
1608 vlib_cli_output (vm, "Plugin behaviors:\n");
1609 for (i = 0; i < vec_len (plugins_vec); i++)
1611 plugin = plugins_vec[i];
1612 vlib_cli_output (vm, "\t%s\t-> %s.\n", plugin->keyword_str,
1614 vlib_cli_output (vm, "\t\tParameters: '%s'\n", plugin->params_str);
1620 VLIB_CLI_COMMAND (show_sr_localsid_behaviors_command, static) = {
1621 .path = "show sr localsids behaviors",
1622 .short_help = "show sr localsids behaviors",
1623 .function = show_sr_localsid_behaviors_command_fn,
1628 * @brief SR LocalSID initialization
1631 sr_localsids_init (vlib_main_t * vm)
1633 /* Init memory for function keys */
1634 ip6_sr_main_t *sm = &sr_main;
1635 mhash_init (&sm->sr_localsids_index_hash, sizeof (uword),
1636 sizeof (ip6_address_t));
1637 /* Init SR behaviors DPO type */
1638 sr_localsid_dpo_type = dpo_register_new_type (&sr_loc_vft, sr_loc_nodes);
1639 /* Init SR behaviors DPO type */
1640 sr_localsid_d_dpo_type =
1641 dpo_register_new_type (&sr_loc_vft, sr_loc_d_nodes);
1642 /* Init memory for localsid plugins */
1643 sm->plugin_functions_by_key = hash_create_string (0, sizeof (uword));
1647 VLIB_INIT_FUNCTION (sr_localsids_init);
1649 * fd.io coding-style-patch-verification: ON
1652 * eval: (c-set-style "gnu")