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 u16 localsid_prefix_len, char end_psp, u8 behavior,
68 u32 sw_if_index, u32 vlan_index, u32 fib_table,
69 ip46_address_t * nh_addr, void *ls_plugin_mem)
71 ip6_sr_main_t *sm = &sr_main;
75 sr_localsid_fn_registration_t *plugin = 0;
77 ip6_sr_localsid_t *ls = 0;
79 dpo_id_t dpo = DPO_INVALID;
81 /* Search for the item */
82 p = mhash_get (&sm->sr_localsids_index_hash, localsid_addr);
88 /* Retrieve localsid */
89 ls = pool_elt_at_index (sm->localsids, p[0]);
90 if (ls->behavior >= SR_BEHAVIOR_LAST)
92 plugin = pool_elt_at_index (sm->plugin_functions,
93 ls->behavior - SR_BEHAVIOR_LAST);
94 pref_length = plugin->prefix_length;
97 if (localsid_prefix_len != 0)
99 pref_length = localsid_prefix_len;
102 /* Delete FIB entry */
104 .fp_proto = FIB_PROTOCOL_IP6,
105 .fp_len = pref_length,
107 .ip6 = *localsid_addr,
111 fib_table_entry_delete (fib_table_find
112 (FIB_PROTOCOL_IP6, fib_table), &pfx,
115 /* In case it is a Xconnect iface remove the (OIF, NHOP) adj */
116 if (ls->behavior == SR_BEHAVIOR_X || ls->behavior == SR_BEHAVIOR_DX6
117 || ls->behavior == SR_BEHAVIOR_DX4)
118 adj_unlock (ls->nh_adj);
120 if (ls->behavior >= SR_BEHAVIOR_LAST)
122 /* Callback plugin removal function */
123 rv = plugin->removal (ls);
126 /* Delete localsid registry */
127 pool_put (sm->localsids, ls);
128 mhash_unset (&sm->sr_localsids_index_hash, localsid_addr, NULL);
131 else /* create with function already existing; complain */
135 /* delete; localsid does not exist; complain */
139 if (behavior >= SR_BEHAVIOR_LAST)
141 sr_localsid_fn_registration_t *plugin = 0;
143 pool_elt_at_index (sm->plugin_functions, behavior - SR_BEHAVIOR_LAST);
144 pref_length = plugin->prefix_length;
147 /* Check whether there exists a FIB entry with such address */
149 .fp_proto = FIB_PROTOCOL_IP6,
150 .fp_len = pref_length,
153 pfx.fp_addr.as_u64[0] = localsid_addr->as_u64[0];
154 pfx.fp_addr.as_u64[1] = localsid_addr->as_u64[1];
156 if (pref_length != 0)
158 pfx.fp_len = pref_length;
161 /* Lookup the FIB index associated to the table id provided */
162 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP6, fib_table);
166 /* Lookup the localsid in such FIB table */
167 fib_node_index_t fei = fib_table_lookup_exact_match (fib_index, &pfx);
168 if (FIB_NODE_INDEX_INVALID != fei)
169 return -4; //There is an entry for such address (the localsid addr)
171 /* Create a new localsid registry */
172 pool_get (sm->localsids, ls);
173 clib_memset (ls, 0, sizeof (*ls));
175 clib_memcpy (&ls->localsid, localsid_addr, sizeof (ip6_address_t));
176 ls->localsid_prefix_len = pref_length;
177 ls->end_psp = end_psp;
178 ls->behavior = behavior;
179 ls->nh_adj = (u32) ~ 0;
180 ls->fib_table = fib_table;
183 case SR_BEHAVIOR_END:
186 ls->sw_if_index = sw_if_index;
187 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
190 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP6, sw_if_index);
192 case SR_BEHAVIOR_DX4:
193 ls->sw_if_index = sw_if_index;
194 clib_memcpy (&ls->next_hop.ip4, &nh_addr->ip4, sizeof (ip4_address_t));
196 case SR_BEHAVIOR_DX6:
197 ls->sw_if_index = sw_if_index;
198 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
200 case SR_BEHAVIOR_DT6:
201 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP6, sw_if_index);
203 case SR_BEHAVIOR_DT4:
204 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP4, sw_if_index);
206 case SR_BEHAVIOR_DX2:
207 ls->sw_if_index = sw_if_index;
208 ls->vlan_index = vlan_index;
212 /* Figure out the adjacency magic for Xconnect variants */
213 if (ls->behavior == SR_BEHAVIOR_X || ls->behavior == SR_BEHAVIOR_DX4
214 || ls->behavior == SR_BEHAVIOR_DX6)
216 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
218 /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
219 if (ls->behavior == SR_BEHAVIOR_DX6 || ls->behavior == SR_BEHAVIOR_X)
220 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
221 nh_addr, sw_if_index);
223 else if (ls->behavior == SR_BEHAVIOR_DX4)
224 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
225 nh_addr, sw_if_index);
227 /* Check for ADJ creation error. If so panic */
228 if (nh_adj_index == ADJ_INDEX_INVALID)
230 pool_put (sm->localsids, ls);
234 ls->nh_adj = nh_adj_index;
238 if (ls->behavior == SR_BEHAVIOR_END || ls->behavior == SR_BEHAVIOR_X
239 || ls->behavior == SR_BEHAVIOR_T)
240 dpo_set (&dpo, sr_localsid_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
241 else if (ls->behavior > SR_BEHAVIOR_D_FIRST
242 && ls->behavior < SR_BEHAVIOR_LAST)
243 dpo_set (&dpo, sr_localsid_d_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
244 else if (ls->behavior >= SR_BEHAVIOR_LAST)
246 sr_localsid_fn_registration_t *plugin = 0;
247 plugin = pool_elt_at_index (sm->plugin_functions,
248 ls->behavior - SR_BEHAVIOR_LAST);
249 /* Copy the unformat memory result */
250 ls->plugin_mem = ls_plugin_mem;
251 /* Callback plugin creation function */
252 rv = plugin->creation (ls);
255 pool_put (sm->localsids, ls);
258 dpo_set (&dpo, plugin->dpo, DPO_PROTO_IP6, ls - sm->localsids);
261 /* Set hash key for searching localsid by address */
262 mhash_set (&sm->sr_localsids_index_hash, localsid_addr, ls - sm->localsids,
265 fib_table_entry_special_dpo_add (fib_index, &pfx, FIB_SOURCE_SR,
266 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
269 /* Set counter to zero */
270 vlib_validate_combined_counter (&(sm->sr_ls_valid_counters),
272 vlib_validate_combined_counter (&(sm->sr_ls_invalid_counters),
275 vlib_zero_combined_counter (&(sm->sr_ls_valid_counters),
277 vlib_zero_combined_counter (&(sm->sr_ls_invalid_counters),
284 * @brief SR LocalSID CLI function.
286 * @see sr_cli_localsid
288 static clib_error_t *
289 sr_cli_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
290 vlib_cli_command_t * cmd)
292 vnet_main_t *vnm = vnet_get_main ();
293 ip6_sr_main_t *sm = &sr_main;
294 u32 sw_if_index = (u32) ~ 0, vlan_index = (u32) ~ 0, fib_index = 0;
298 ip6_address_t resulting_address;
299 ip46_address_t next_hop;
300 char address_set = 0;
302 void *ls_plugin_mem = 0;
306 clib_memset (&resulting_address, 0, sizeof (ip6_address_t));
307 ip46_address_reset (&next_hop);
309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
311 if (unformat (input, "del"))
313 else if (!address_set
314 && unformat (input, "address %U/%u", unformat_ip6_address,
315 &resulting_address, &prefix_len))
317 else if (!address_set
318 && unformat (input, "address %U", unformat_ip6_address,
321 else if (!address_set
322 && unformat (input, "addr %U", unformat_ip6_address,
325 else if (unformat (input, "fib-table %u", &fib_index));
326 else if (vlan_index == (u32) ~ 0
327 && unformat (input, "vlan %u", &vlan_index));
328 else if (!behavior && unformat (input, "behavior"))
330 if (unformat (input, "end.x %U %U",
331 unformat_vnet_sw_interface, vnm, &sw_if_index,
332 unformat_ip6_address, &next_hop.ip6))
333 behavior = SR_BEHAVIOR_X;
334 else if (unformat (input, "end.t %u", &sw_if_index))
335 behavior = SR_BEHAVIOR_T;
336 else if (unformat (input, "end.dx6 %U %U",
337 unformat_vnet_sw_interface, vnm, &sw_if_index,
338 unformat_ip6_address, &next_hop.ip6))
339 behavior = SR_BEHAVIOR_DX6;
340 else if (unformat (input, "end.dx4 %U %U",
341 unformat_vnet_sw_interface, vnm, &sw_if_index,
342 unformat_ip4_address, &next_hop.ip4))
343 behavior = SR_BEHAVIOR_DX4;
344 else if (unformat (input, "end.dx2 %U",
345 unformat_vnet_sw_interface, vnm, &sw_if_index))
346 behavior = SR_BEHAVIOR_DX2;
347 else if (unformat (input, "end.dt6 %u", &sw_if_index))
348 behavior = SR_BEHAVIOR_DT6;
349 else if (unformat (input, "end.dt4 %u", &sw_if_index))
350 behavior = SR_BEHAVIOR_DT4;
353 /* Loop over all the plugin behavior format functions */
354 sr_localsid_fn_registration_t *plugin = 0, **vec_plugins = 0;
355 sr_localsid_fn_registration_t **plugin_it = 0;
357 /* Create a vector out of the plugin pool as recommended */
359 pool_foreach (plugin, sm->plugin_functions,
361 vec_add1 (vec_plugins, plugin);
365 vec_foreach (plugin_it, vec_plugins)
368 (input, "%U", (*plugin_it)->ls_unformat, &ls_plugin_mem))
370 behavior = (*plugin_it)->sr_localsid_function_number;
378 if (unformat (input, "end"))
379 behavior = SR_BEHAVIOR_END;
384 else if (!end_psp && unformat (input, "psp"))
390 if (!behavior && end_psp)
391 behavior = SR_BEHAVIOR_END;
394 return clib_error_return (0,
395 "Error: SRv6 LocalSID address is mandatory.");
396 if (!is_del && !behavior)
397 return clib_error_return (0,
398 "Error: SRv6 LocalSID behavior is mandatory.");
399 if (vlan_index != (u32) ~ 0)
400 return clib_error_return (0,
401 "Error: SRv6 End.DX2 with rewrite VLAN tag not supported by now.");
402 if (end_psp && !(behavior == SR_BEHAVIOR_END || behavior == SR_BEHAVIOR_X))
403 return clib_error_return (0,
404 "Error: SRv6 PSP only compatible with End and End.X");
407 sr_cli_localsid (is_del, &resulting_address, prefix_len, end_psp,
408 behavior, sw_if_index, vlan_index, fib_index, &next_hop,
418 return clib_error_return (0,
419 "Identical localsid already exists. Requested localsid not created.");
421 return clib_error_return (0,
422 "The requested localsid could not be deleted. SR localsid not found");
424 return clib_error_return (0, "FIB table %u does not exist", fib_index);
426 return clib_error_return (0, "There is already one FIB entry for the"
427 "requested localsid non segment routing related");
429 return clib_error_return (0,
430 "Could not create ARP/ND entry for such next_hop. Internal error.");
432 return clib_error_return (0,
433 "Error on the plugin based localsid creation.");
435 return clib_error_return (0, "BUG: sr localsid returns %d", rv);
441 VLIB_CLI_COMMAND (sr_localsid_command, static) = {
442 .path = "sr localsid",
443 .short_help = "sr localsid (del) address XX:XX::YY:YY"
444 "(fib-table 8) behavior STRING",
446 "Create SR LocalSID and binds it to a particular behavior\n"
448 "\tlocalSID IPv6_addr(128b) LocalSID IPv6 address\n"
449 "\t(fib-table X) Optional. VRF where to install SRv6 localsid\n"
450 "\tbehavior STRING Specifies the behavior\n"
452 "\tEnd\t-> Endpoint.\n"
453 "\tEnd.X\t-> Endpoint with decapsulation and Layer-3 cross-connect.\n"
454 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
455 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
456 "\t\tParameters: '<iface>'\n"
457 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
458 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
459 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
460 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
461 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
462 "\t\tParameters: '<ip6_fib_table>'\n"
463 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
464 "\t\tParameters: '<ip4_fib_table>'\n",
465 .function = sr_cli_localsid_command_fn,
470 * @brief CLI function to 'show' all SR LocalSIDs on console.
472 static clib_error_t *
473 show_sr_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
474 vlib_cli_command_t * cmd)
476 vnet_main_t *vnm = vnet_get_main ();
477 ip6_sr_main_t *sm = &sr_main;
478 ip6_sr_localsid_t **localsid_list = 0;
479 ip6_sr_localsid_t *ls;
482 vlib_cli_output (vm, "SRv6 - My LocalSID Table:");
483 vlib_cli_output (vm, "=========================");
485 pool_foreach (ls, sm->localsids, ({ vec_add1 (localsid_list, ls); }));
487 for (i = 0; i < vec_len (localsid_list); i++)
489 ls = localsid_list[i];
490 switch (ls->behavior)
492 case SR_BEHAVIOR_END:
493 vlib_cli_output (vm, "\tAddress: \t%U/%u\n\tBehavior: \tEnd",
494 format_ip6_address, &ls->localsid,
495 ls->localsid_prefix_len);
499 "\tAddress: \t%U/%u\n\tBehavior: \tX (Endpoint with Layer-3 cross-connect)"
500 "\n\tIface: \t%U\n\tNext hop: \t%U",
501 format_ip6_address, &ls->localsid,
502 ls->localsid_prefix_len,
503 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
504 format_ip6_address, &ls->next_hop.ip6);
508 "\tAddress: \t%U/%u\n\tBehavior: \tT (Endpoint with specific IPv6 table lookup)"
510 format_ip6_address, &ls->localsid,
511 ls->localsid_prefix_len,
512 fib_table_get_table_id (ls->vrf_index,
515 case SR_BEHAVIOR_DX4:
517 "\tAddress: \t%U/%u\n\tBehavior: \tDX4 (Endpoint with decapsulation and IPv4 cross-connect)"
518 "\n\tIface: \t%U\n\tNext hop: \t%U",
519 format_ip6_address, &ls->localsid,
520 ls->localsid_prefix_len,
521 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
522 format_ip4_address, &ls->next_hop.ip4);
524 case SR_BEHAVIOR_DX6:
526 "\tAddress: \t%U/%u\n\tBehavior: \tDX6 (Endpoint with decapsulation and IPv6 cross-connect)"
527 "\n\tIface: \t%U\n\tNext hop: \t%U",
528 format_ip6_address, &ls->localsid,
529 ls->localsid_prefix_len,
530 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
531 format_ip6_address, &ls->next_hop.ip6);
533 case SR_BEHAVIOR_DX2:
534 if (ls->vlan_index == (u32) ~ 0)
536 "\tAddress: \t%U/%u\n\tBehavior: \tDX2 (Endpoint with decapulation and Layer-2 cross-connect)"
537 "\n\tIface: \t%U", format_ip6_address,
538 &ls->localsid, ls->localsid_prefix_len,
539 format_vnet_sw_if_index_name, vnm,
543 "Unsupported yet. (DX2 with egress VLAN rewrite)");
545 case SR_BEHAVIOR_DT6:
547 "\tAddress: \t%U/%u\n\tBehavior: \tDT6 (Endpoint with decapsulation and specific IPv6 table lookup)"
548 "\n\tTable: %u", format_ip6_address, &ls->localsid,
549 ls->localsid_prefix_len,
550 fib_table_get_table_id (ls->vrf_index,
553 case SR_BEHAVIOR_DT4:
555 "\tAddress: \t%U/%u\n\tBehavior: \tDT4 (Endpoint with decapsulation and specific IPv4 table lookup)"
556 "\n\tTable: \t%u", format_ip6_address,
557 &ls->localsid, ls->localsid_prefix_len,
558 fib_table_get_table_id (ls->vrf_index,
562 if (ls->behavior >= SR_BEHAVIOR_LAST)
564 sr_localsid_fn_registration_t *plugin =
565 pool_elt_at_index (sm->plugin_functions,
566 ls->behavior - SR_BEHAVIOR_LAST);
568 vlib_cli_output (vm, "\tAddress: \t%U/%u\n"
569 "\tBehavior: \t%s (%s)\n\t%U",
570 format_ip6_address, &ls->localsid,
571 ls->localsid_prefix_len, plugin->keyword_str,
572 plugin->def_str, plugin->ls_format,
576 //Should never get here...
577 vlib_cli_output (vm, "Internal error");
581 vlib_cli_output (vm, "\tPSP: \tTrue\n");
584 vlib_counter_t valid, invalid;
585 vlib_get_combined_counter (&(sm->sr_ls_valid_counters), i, &valid);
586 vlib_get_combined_counter (&(sm->sr_ls_invalid_counters), i, &invalid);
587 vlib_cli_output (vm, "\tGood traffic: \t[%Ld packets : %Ld bytes]\n",
588 valid.packets, valid.bytes);
589 vlib_cli_output (vm, "\tBad traffic: \t[%Ld packets : %Ld bytes]\n",
590 invalid.packets, invalid.bytes);
591 vlib_cli_output (vm, "--------------------");
597 VLIB_CLI_COMMAND (show_sr_localsid_command, static) = {
598 .path = "show sr localsids",
599 .short_help = "show sr localsids",
600 .function = show_sr_localsid_command_fn,
605 * @brief Function to 'clear' ALL SR localsid counters
607 static clib_error_t *
608 clear_sr_localsid_counters_command_fn (vlib_main_t * vm,
609 unformat_input_t * input,
610 vlib_cli_command_t * cmd)
612 ip6_sr_main_t *sm = &sr_main;
614 vlib_clear_combined_counters (&(sm->sr_ls_valid_counters));
615 vlib_clear_combined_counters (&(sm->sr_ls_invalid_counters));
621 VLIB_CLI_COMMAND (clear_sr_localsid_counters_command, static) = {
622 .path = "clear sr localsid-counters",
623 .short_help = "clear sr localsid-counters",
624 .function = clear_sr_localsid_counters_command_fn,
628 /************************ SR LocalSID graphs node ****************************/
630 * @brief SR localsid node trace
634 ip6_address_t localsid;
639 } sr_localsid_trace_t;
641 #define foreach_sr_localsid_error \
642 _(NO_INNER_HEADER, "(SR-Error) No inner IP header") \
643 _(NO_MORE_SEGMENTS, "(SR-Error) No more segments") \
644 _(NO_SRH, "(SR-Error) No SR header") \
645 _(NO_PSP, "(SR-Error) PSP Not available (segments left > 0)") \
646 _(NOT_LS, "(SR-Error) Decaps not available (segments left > 0)") \
647 _(L2, "(SR-Error) SRv6 decapsulated a L2 frame without dest")
651 #define _(sym,str) SR_LOCALSID_ERROR_##sym,
652 foreach_sr_localsid_error
655 } sr_localsid_error_t;
657 static char *sr_localsid_error_strings[] = {
658 #define _(sym,string) string,
659 foreach_sr_localsid_error
663 #define foreach_sr_localsid_next \
664 _(ERROR, "error-drop") \
665 _(IP6_LOOKUP, "ip6-lookup") \
666 _(IP4_LOOKUP, "ip4-lookup") \
667 _(IP6_REWRITE, "ip6-rewrite") \
668 _(IP4_REWRITE, "ip4-rewrite") \
669 _(INTERFACE_OUTPUT, "interface-output")
673 #define _(s,n) SR_LOCALSID_NEXT_##s,
674 foreach_sr_localsid_next
677 } sr_localsid_next_t;
680 * @brief SR LocalSID graph node trace function
685 format_sr_localsid_trace (u8 * s, va_list * args)
687 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
688 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
689 sr_localsid_trace_t *t = va_arg (*args, sr_localsid_trace_t *);
692 format (s, "SR-LOCALSID:\n\tLocalsid: %U\n", format_ip6_address,
696 case SR_BEHAVIOR_END:
697 s = format (s, "\tBehavior: End\n");
699 case SR_BEHAVIOR_DX6:
700 s = format (s, "\tBehavior: Decapsulation with IPv6 L3 xconnect\n");
702 case SR_BEHAVIOR_DX4:
703 s = format (s, "\tBehavior: Decapsulation with IPv4 L3 xconnect\n");
706 s = format (s, "\tBehavior: IPv6 L3 xconnect\n");
709 s = format (s, "\tBehavior: IPv6 specific table lookup\n");
711 case SR_BEHAVIOR_DT6:
712 s = format (s, "\tBehavior: Decapsulation with IPv6 Table lookup\n");
714 case SR_BEHAVIOR_DT4:
715 s = format (s, "\tBehavior: Decapsulation with IPv4 Table lookup\n");
717 case SR_BEHAVIOR_DX2:
718 s = format (s, "\tBehavior: Decapsulation with L2 xconnect\n");
721 s = format (s, "\tBehavior: defined in plugin\n"); //TODO
724 if (t->num_segments != 0xFF)
726 if (t->num_segments > 0)
728 s = format (s, "\tSegments left: %d\n", t->segments_left);
729 s = format (s, "\tSID list: [in ietf order]");
731 for (i = 0; i < t->num_segments; i++)
733 s = format (s, "\n\t-> %U", format_ip6_address,
734 (ip6_address_t *) & t->sr[i *
735 sizeof (ip6_address_t)]);
743 * @brief Function doing End processing.
745 static_always_inline void
746 end_srh_processing (vlib_node_runtime_t * node,
749 ip6_sr_header_t * sr0,
750 ip6_sr_localsid_t * ls0,
751 u32 * next0, u8 psp, ip6_ext_header_t * prev0)
753 ip6_address_t *new_dst0;
755 if (PREDICT_TRUE (sr0 && sr0->type == ROUTING_HEADER_TYPE_SR))
757 if (sr0->segments_left == 1 && psp)
760 u64 *copy_dst0, *copy_src0;
761 u32 copy_len_u64s0 = 0;
763 ip0->dst_address.as_u64[0] = sr0->segments->as_u64[0];
764 ip0->dst_address.as_u64[1] = sr0->segments->as_u64[1];
766 /* Remove the SRH taking care of the rest of IPv6 ext header */
768 prev0->next_hdr = sr0->protocol;
770 ip0->protocol = sr0->protocol;
772 sr_len = ip6_ext_header_len (sr0);
773 vlib_buffer_advance (b0, sr_len);
774 new_l0 = clib_net_to_host_u16 (ip0->payload_length) - sr_len;
775 ip0->payload_length = clib_host_to_net_u16 (new_l0);
776 copy_src0 = (u64 *) ip0;
777 copy_dst0 = copy_src0 + (sr0->length + 1);
778 /* number of 8 octet units to copy
779 * By default in absence of extension headers it is equal to length of ip6 header
780 * With extension headers it number of 8 octet units of ext headers preceding
784 (((u8 *) sr0 - (u8 *) ip0) - sizeof (ip6_header_t)) >> 3;
785 copy_dst0[4 + copy_len_u64s0] = copy_src0[4 + copy_len_u64s0];
786 copy_dst0[3 + copy_len_u64s0] = copy_src0[3 + copy_len_u64s0];
787 copy_dst0[2 + copy_len_u64s0] = copy_src0[2 + copy_len_u64s0];
788 copy_dst0[1 + copy_len_u64s0] = copy_src0[1 + copy_len_u64s0];
789 copy_dst0[0 + copy_len_u64s0] = copy_src0[0 + copy_len_u64s0];
792 for (i = copy_len_u64s0 - 1; i >= 0; i--)
794 copy_dst0[i] = copy_src0[i];
797 if (ls0->behavior == SR_BEHAVIOR_X)
799 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
800 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
802 else if (ls0->behavior == SR_BEHAVIOR_T)
804 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
807 else if (PREDICT_TRUE (sr0->segments_left > 0))
809 sr0->segments_left -= 1;
810 new_dst0 = (ip6_address_t *) (sr0->segments);
811 new_dst0 += sr0->segments_left;
812 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
813 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
815 if (ls0->behavior == SR_BEHAVIOR_X)
817 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
818 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
820 else if (ls0->behavior == SR_BEHAVIOR_T)
822 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
827 *next0 = SR_LOCALSID_NEXT_ERROR;
828 b0->error = node->errors[SR_LOCALSID_ERROR_NO_MORE_SEGMENTS];
833 /* Error. Routing header of type != SR */
834 *next0 = SR_LOCALSID_NEXT_ERROR;
835 b0->error = node->errors[SR_LOCALSID_ERROR_NO_SRH];
840 * @brief Function doing SRH processing for D* variants
842 static_always_inline void
843 end_decaps_srh_processing (vlib_node_runtime_t * node,
846 ip6_sr_header_t * sr0,
847 ip6_sr_localsid_t * ls0, u32 * next0)
849 /* Compute the size of the IPv6 header with all Ext. headers */
851 ip6_ext_header_t *next_ext_header;
854 next_proto = ip0->protocol;
855 next_ext_header = (void *) (ip0 + 1);
856 total_size = sizeof (ip6_header_t);
857 while (ip6_ext_hdr (next_proto))
859 total_size += ip6_ext_header_len (next_ext_header);
860 next_proto = next_ext_header->next_hdr;
861 next_ext_header = ip6_ext_next_header (next_ext_header);
864 /* Ensure this is the last segment. Otherwise drop. */
865 if (sr0 && sr0->segments_left != 0)
867 *next0 = SR_LOCALSID_NEXT_ERROR;
868 b0->error = node->errors[SR_LOCALSID_ERROR_NOT_LS];
874 case IP_PROTOCOL_IPV6:
875 /* Encap-End IPv6. Pop outer IPv6 header. */
876 if (ls0->behavior == SR_BEHAVIOR_DX6)
878 vlib_buffer_advance (b0, total_size);
879 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
880 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
883 else if (ls0->behavior == SR_BEHAVIOR_DT6)
885 vlib_buffer_advance (b0, total_size);
886 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
890 case IP_PROTOCOL_IP_IN_IP:
891 /* Encap-End IPv4. Pop outer IPv6 header */
892 if (ls0->behavior == SR_BEHAVIOR_DX4)
894 vlib_buffer_advance (b0, total_size);
895 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
896 *next0 = SR_LOCALSID_NEXT_IP4_REWRITE;
899 else if (ls0->behavior == SR_BEHAVIOR_DT4)
901 vlib_buffer_advance (b0, total_size);
902 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
903 *next0 = SR_LOCALSID_NEXT_IP4_LOOKUP;
907 case IP_PROTOCOL_IP6_ETHERNET:
909 if (ls0->behavior == SR_BEHAVIOR_DX2)
911 vlib_buffer_advance (b0, total_size);
912 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->sw_if_index;
913 *next0 = SR_LOCALSID_NEXT_INTERFACE_OUTPUT;
918 *next0 = SR_LOCALSID_NEXT_ERROR;
919 b0->error = node->errors[SR_LOCALSID_ERROR_NO_INNER_HEADER];
924 * @brief SR LocalSID graph node. Supports all default SR Endpoint variants with decaps
927 sr_localsid_d_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
928 vlib_frame_t * from_frame)
930 u32 n_left_from, next_index, *from, *to_next;
931 ip6_sr_main_t *sm = &sr_main;
932 from = vlib_frame_vector_args (from_frame);
933 n_left_from = from_frame->n_vectors;
934 next_index = node->cached_next_index;
935 u32 thread_index = vm->thread_index;
937 while (n_left_from > 0)
940 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
943 while (n_left_from >= 8 && n_left_to_next >= 4)
945 u32 bi0, bi1, bi2, bi3;
946 vlib_buffer_t *b0, *b1, *b2, *b3;
947 ip6_header_t *ip0, *ip1, *ip2, *ip3;
948 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
949 u32 next0, next1, next2, next3;
950 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
951 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
953 /* Prefetch next iteration. */
955 vlib_buffer_t *p4, *p5, *p6, *p7;
957 p4 = vlib_get_buffer (vm, from[4]);
958 p5 = vlib_get_buffer (vm, from[5]);
959 p6 = vlib_get_buffer (vm, from[6]);
960 p7 = vlib_get_buffer (vm, from[7]);
962 /* Prefetch the buffer header and packet for the N+4 loop iteration */
963 vlib_prefetch_buffer_header (p4, LOAD);
964 vlib_prefetch_buffer_header (p5, LOAD);
965 vlib_prefetch_buffer_header (p6, LOAD);
966 vlib_prefetch_buffer_header (p7, LOAD);
968 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
969 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
970 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
971 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
974 to_next[0] = bi0 = from[0];
975 to_next[1] = bi1 = from[1];
976 to_next[2] = bi2 = from[2];
977 to_next[3] = bi3 = from[3];
983 b0 = vlib_get_buffer (vm, bi0);
984 b1 = vlib_get_buffer (vm, bi1);
985 b2 = vlib_get_buffer (vm, bi2);
986 b3 = vlib_get_buffer (vm, bi3);
989 pool_elt_at_index (sm->localsids,
990 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
992 pool_elt_at_index (sm->localsids,
993 vnet_buffer (b1)->ip.adj_index[VLIB_TX]);
995 pool_elt_at_index (sm->localsids,
996 vnet_buffer (b2)->ip.adj_index[VLIB_TX]);
998 pool_elt_at_index (sm->localsids,
999 vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
1001 ip0 = vlib_buffer_get_current (b0);
1002 ip1 = vlib_buffer_get_current (b1);
1003 ip2 = vlib_buffer_get_current (b2);
1004 ip3 = vlib_buffer_get_current (b3);
1007 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, NULL);
1009 ip6_ext_header_find (vm, b1, ip1, IP_PROTOCOL_IPV6_ROUTE, NULL);
1011 ip6_ext_header_find (vm, b2, ip2, IP_PROTOCOL_IPV6_ROUTE, NULL);
1013 ip6_ext_header_find (vm, b3, ip3, IP_PROTOCOL_IPV6_ROUTE, NULL);
1015 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1016 end_decaps_srh_processing (node, b1, ip1, sr1, ls1, &next1);
1017 end_decaps_srh_processing (node, b2, ip2, sr2, ls2, &next2);
1018 end_decaps_srh_processing (node, b3, ip3, sr3, ls3, &next3);
1020 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1022 sr_localsid_trace_t *tr =
1023 vlib_add_trace (vm, node, b0, sizeof (*tr));
1024 tr->num_segments = 0;
1025 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1026 sizeof (tr->localsid.as_u8));
1027 tr->behavior = ls0->behavior;
1028 if (ip0 == vlib_buffer_get_current (b0))
1030 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1031 && sr0->type == ROUTING_HEADER_TYPE_SR)
1033 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1035 sr0->length * 8 / sizeof (ip6_address_t);
1036 tr->segments_left = sr0->segments_left;
1040 tr->num_segments = 0xFF;
1043 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
1045 sr_localsid_trace_t *tr =
1046 vlib_add_trace (vm, node, b1, sizeof (*tr));
1047 tr->num_segments = 0;
1048 clib_memcpy (tr->localsid.as_u8, ls1->localsid.as_u8,
1049 sizeof (tr->localsid.as_u8));
1050 tr->behavior = ls1->behavior;
1051 if (ip1 == vlib_buffer_get_current (b1))
1053 if (ip1->protocol == IP_PROTOCOL_IPV6_ROUTE
1054 && sr1->type == ROUTING_HEADER_TYPE_SR)
1056 clib_memcpy (tr->sr, sr1->segments, sr1->length * 8);
1058 sr1->length * 8 / sizeof (ip6_address_t);
1059 tr->segments_left = sr1->segments_left;
1063 tr->num_segments = 0xFF;
1066 if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
1068 sr_localsid_trace_t *tr =
1069 vlib_add_trace (vm, node, b2, sizeof (*tr));
1070 tr->num_segments = 0;
1071 clib_memcpy (tr->localsid.as_u8, ls2->localsid.as_u8,
1072 sizeof (tr->localsid.as_u8));
1073 tr->behavior = ls2->behavior;
1074 if (ip2 == vlib_buffer_get_current (b2))
1076 if (ip2->protocol == IP_PROTOCOL_IPV6_ROUTE
1077 && sr2->type == ROUTING_HEADER_TYPE_SR)
1079 clib_memcpy (tr->sr, sr2->segments, sr2->length * 8);
1081 sr2->length * 8 / sizeof (ip6_address_t);
1082 tr->segments_left = sr2->segments_left;
1086 tr->num_segments = 0xFF;
1089 if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
1091 sr_localsid_trace_t *tr =
1092 vlib_add_trace (vm, node, b3, sizeof (*tr));
1093 tr->num_segments = 0;
1094 clib_memcpy (tr->localsid.as_u8, ls3->localsid.as_u8,
1095 sizeof (tr->localsid.as_u8));
1096 tr->behavior = ls3->behavior;
1097 if (ip3 == vlib_buffer_get_current (b3))
1099 if (ip3->protocol == IP_PROTOCOL_IPV6_ROUTE
1100 && sr3->type == ROUTING_HEADER_TYPE_SR)
1102 clib_memcpy (tr->sr, sr3->segments, sr3->length * 8);
1104 sr3->length * 8 / sizeof (ip6_address_t);
1105 tr->segments_left = sr3->segments_left;
1109 tr->num_segments = 0xFF;
1112 vlib_increment_combined_counter
1114 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1115 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1116 1, vlib_buffer_length_in_chain (vm, b0));
1118 vlib_increment_combined_counter
1120 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1121 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1122 1, vlib_buffer_length_in_chain (vm, b1));
1124 vlib_increment_combined_counter
1126 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1127 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1128 1, vlib_buffer_length_in_chain (vm, b2));
1130 vlib_increment_combined_counter
1132 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1133 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1134 1, vlib_buffer_length_in_chain (vm, b3));
1136 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1137 n_left_to_next, bi0, bi1, bi2, bi3,
1138 next0, next1, next2, next3);
1141 /* Single loop for potentially the last three packets */
1142 while (n_left_from > 0 && n_left_to_next > 0)
1147 ip6_sr_header_t *sr0;
1148 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1149 ip6_sr_localsid_t *ls0;
1156 n_left_to_next -= 1;
1158 b0 = vlib_get_buffer (vm, bi0);
1159 ip0 = vlib_buffer_get_current (b0);
1161 /* Lookup the SR End behavior based on IP DA (adj) */
1163 pool_elt_at_index (sm->localsids,
1164 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1166 /* Find SRH as well as previous header */
1168 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, NULL);
1170 /* SRH processing and End variants */
1171 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1173 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1175 sr_localsid_trace_t *tr =
1176 vlib_add_trace (vm, node, b0, sizeof (*tr));
1177 tr->num_segments = 0;
1178 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1179 sizeof (tr->localsid.as_u8));
1180 tr->behavior = ls0->behavior;
1181 if (ip0 == vlib_buffer_get_current (b0))
1183 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1184 && sr0->type == ROUTING_HEADER_TYPE_SR)
1186 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1188 sr0->length * 8 / sizeof (ip6_address_t);
1189 tr->segments_left = sr0->segments_left;
1193 tr->num_segments = 0xFF;
1196 /* Increase the counters */
1197 vlib_increment_combined_counter
1199 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1200 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1201 1, vlib_buffer_length_in_chain (vm, b0));
1203 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1204 n_left_to_next, bi0, next0);
1206 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1208 return from_frame->n_vectors;
1212 VLIB_REGISTER_NODE (sr_localsid_d_node) = {
1213 .function = sr_localsid_d_fn,
1214 .name = "sr-localsid-d",
1215 .vector_size = sizeof (u32),
1216 .format_trace = format_sr_localsid_trace,
1217 .type = VLIB_NODE_TYPE_INTERNAL,
1218 .n_errors = SR_LOCALSID_N_ERROR,
1219 .error_strings = sr_localsid_error_strings,
1220 .n_next_nodes = SR_LOCALSID_N_NEXT,
1222 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1223 foreach_sr_localsid_next
1230 * @brief SR LocalSID graph node. Supports all default SR Endpoint without decaps
1233 sr_localsid_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1234 vlib_frame_t * from_frame)
1236 u32 n_left_from, next_index, *from, *to_next;
1237 ip6_sr_main_t *sm = &sr_main;
1238 from = vlib_frame_vector_args (from_frame);
1239 n_left_from = from_frame->n_vectors;
1240 next_index = node->cached_next_index;
1241 u32 thread_index = vm->thread_index;
1243 while (n_left_from > 0)
1246 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1249 while (n_left_from >= 8 && n_left_to_next >= 4)
1251 u32 bi0, bi1, bi2, bi3;
1252 vlib_buffer_t *b0, *b1, *b2, *b3;
1253 ip6_header_t *ip0, *ip1, *ip2, *ip3;
1254 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
1255 ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
1256 u32 next0, next1, next2, next3;
1257 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1258 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
1260 /* Prefetch next iteration. */
1262 vlib_buffer_t *p4, *p5, *p6, *p7;
1264 p4 = vlib_get_buffer (vm, from[4]);
1265 p5 = vlib_get_buffer (vm, from[5]);
1266 p6 = vlib_get_buffer (vm, from[6]);
1267 p7 = vlib_get_buffer (vm, from[7]);
1269 /* Prefetch the buffer header and packet for the N+2 loop iteration */
1270 vlib_prefetch_buffer_header (p4, LOAD);
1271 vlib_prefetch_buffer_header (p5, LOAD);
1272 vlib_prefetch_buffer_header (p6, LOAD);
1273 vlib_prefetch_buffer_header (p7, LOAD);
1275 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
1276 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
1277 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
1278 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
1281 to_next[0] = bi0 = from[0];
1282 to_next[1] = bi1 = from[1];
1283 to_next[2] = bi2 = from[2];
1284 to_next[3] = bi3 = from[3];
1288 n_left_to_next -= 4;
1290 b0 = vlib_get_buffer (vm, bi0);
1291 b1 = vlib_get_buffer (vm, bi1);
1292 b2 = vlib_get_buffer (vm, bi2);
1293 b3 = vlib_get_buffer (vm, bi3);
1295 ip0 = vlib_buffer_get_current (b0);
1296 ip1 = vlib_buffer_get_current (b1);
1297 ip2 = vlib_buffer_get_current (b2);
1298 ip3 = vlib_buffer_get_current (b3);
1301 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, &prev0);
1303 ip6_ext_header_find (vm, b1, ip1, IP_PROTOCOL_IPV6_ROUTE, &prev1);
1305 ip6_ext_header_find (vm, b2, ip2, IP_PROTOCOL_IPV6_ROUTE, &prev2);
1307 ip6_ext_header_find (vm, b3, ip3, IP_PROTOCOL_IPV6_ROUTE, &prev3);
1310 pool_elt_at_index (sm->localsids,
1311 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1313 pool_elt_at_index (sm->localsids,
1314 vnet_buffer (b1)->ip.adj_index[VLIB_TX]);
1316 pool_elt_at_index (sm->localsids,
1317 vnet_buffer (b2)->ip.adj_index[VLIB_TX]);
1319 pool_elt_at_index (sm->localsids,
1320 vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
1322 end_srh_processing (node, b0, ip0, sr0, ls0, &next0, ls0->end_psp,
1324 end_srh_processing (node, b1, ip1, sr1, ls1, &next1, ls1->end_psp,
1326 end_srh_processing (node, b2, ip2, sr2, ls2, &next2, ls2->end_psp,
1328 end_srh_processing (node, b3, ip3, sr3, ls3, &next3, ls3->end_psp,
1331 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1333 sr_localsid_trace_t *tr =
1334 vlib_add_trace (vm, node, b0, sizeof (*tr));
1335 tr->num_segments = 0;
1336 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1337 sizeof (tr->localsid.as_u8));
1338 tr->behavior = ls0->behavior;
1339 if (ip0 == vlib_buffer_get_current (b0))
1341 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1342 && sr0->type == ROUTING_HEADER_TYPE_SR)
1344 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1346 sr0->length * 8 / sizeof (ip6_address_t);
1347 tr->segments_left = sr0->segments_left;
1351 tr->num_segments = 0xFF;
1354 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
1356 sr_localsid_trace_t *tr =
1357 vlib_add_trace (vm, node, b1, sizeof (*tr));
1358 tr->num_segments = 0;
1359 clib_memcpy (tr->localsid.as_u8, ls1->localsid.as_u8,
1360 sizeof (tr->localsid.as_u8));
1361 tr->behavior = ls1->behavior;
1362 if (ip1 == vlib_buffer_get_current (b1))
1364 if (ip1->protocol == IP_PROTOCOL_IPV6_ROUTE
1365 && sr1->type == ROUTING_HEADER_TYPE_SR)
1367 clib_memcpy (tr->sr, sr1->segments, sr1->length * 8);
1369 sr1->length * 8 / sizeof (ip6_address_t);
1370 tr->segments_left = sr1->segments_left;
1374 tr->num_segments = 0xFF;
1377 if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
1379 sr_localsid_trace_t *tr =
1380 vlib_add_trace (vm, node, b2, sizeof (*tr));
1381 tr->num_segments = 0;
1382 clib_memcpy (tr->localsid.as_u8, ls2->localsid.as_u8,
1383 sizeof (tr->localsid.as_u8));
1384 tr->behavior = ls2->behavior;
1385 if (ip2 == vlib_buffer_get_current (b2))
1387 if (ip2->protocol == IP_PROTOCOL_IPV6_ROUTE
1388 && sr2->type == ROUTING_HEADER_TYPE_SR)
1390 clib_memcpy (tr->sr, sr2->segments, sr2->length * 8);
1392 sr2->length * 8 / sizeof (ip6_address_t);
1393 tr->segments_left = sr2->segments_left;
1397 tr->num_segments = 0xFF;
1400 if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
1402 sr_localsid_trace_t *tr =
1403 vlib_add_trace (vm, node, b3, sizeof (*tr));
1404 tr->num_segments = 0;
1405 clib_memcpy (tr->localsid.as_u8, ls3->localsid.as_u8,
1406 sizeof (tr->localsid.as_u8));
1407 tr->behavior = ls3->behavior;
1408 if (ip3 == vlib_buffer_get_current (b3))
1410 if (ip3->protocol == IP_PROTOCOL_IPV6_ROUTE
1411 && sr3->type == ROUTING_HEADER_TYPE_SR)
1413 clib_memcpy (tr->sr, sr3->segments, sr3->length * 8);
1415 sr3->length * 8 / sizeof (ip6_address_t);
1416 tr->segments_left = sr3->segments_left;
1420 tr->num_segments = 0xFF;
1423 vlib_increment_combined_counter
1425 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1426 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1427 1, vlib_buffer_length_in_chain (vm, b0));
1429 vlib_increment_combined_counter
1431 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1432 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1433 1, vlib_buffer_length_in_chain (vm, b1));
1435 vlib_increment_combined_counter
1437 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1438 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1439 1, vlib_buffer_length_in_chain (vm, b2));
1441 vlib_increment_combined_counter
1443 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1444 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1445 1, vlib_buffer_length_in_chain (vm, b3));
1447 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1448 n_left_to_next, bi0, bi1, bi2, bi3,
1449 next0, next1, next2, next3);
1452 /* Single loop for potentially the last three packets */
1453 while (n_left_from > 0 && n_left_to_next > 0)
1457 ip6_header_t *ip0 = 0;
1458 ip6_ext_header_t *prev0;
1459 ip6_sr_header_t *sr0;
1460 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1461 ip6_sr_localsid_t *ls0;
1468 n_left_to_next -= 1;
1470 b0 = vlib_get_buffer (vm, bi0);
1471 ip0 = vlib_buffer_get_current (b0);
1473 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, &prev0);
1475 /* Lookup the SR End behavior based on IP DA (adj) */
1477 pool_elt_at_index (sm->localsids,
1478 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1480 /* SRH processing */
1481 end_srh_processing (node, b0, ip0, sr0, ls0, &next0, ls0->end_psp,
1484 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1486 sr_localsid_trace_t *tr =
1487 vlib_add_trace (vm, node, b0, sizeof (*tr));
1488 tr->num_segments = 0;
1489 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1490 sizeof (tr->localsid.as_u8));
1491 tr->behavior = ls0->behavior;
1492 if (ip0 == vlib_buffer_get_current (b0))
1494 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1495 && sr0->type == ROUTING_HEADER_TYPE_SR)
1497 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1499 sr0->length * 8 / sizeof (ip6_address_t);
1500 tr->segments_left = sr0->segments_left;
1504 tr->num_segments = 0xFF;
1507 vlib_increment_combined_counter
1509 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1510 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1511 1, vlib_buffer_length_in_chain (vm, b0));
1513 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1514 n_left_to_next, bi0, next0);
1516 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1518 return from_frame->n_vectors;
1522 VLIB_REGISTER_NODE (sr_localsid_node) = {
1523 .function = sr_localsid_fn,
1524 .name = "sr-localsid",
1525 .vector_size = sizeof (u32),
1526 .format_trace = format_sr_localsid_trace,
1527 .type = VLIB_NODE_TYPE_INTERNAL,
1528 .n_errors = SR_LOCALSID_N_ERROR,
1529 .error_strings = sr_localsid_error_strings,
1530 .n_next_nodes = SR_LOCALSID_N_NEXT,
1532 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1533 foreach_sr_localsid_next
1540 format_sr_dpo (u8 * s, va_list * args)
1542 index_t index = va_arg (*args, index_t);
1543 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
1545 return (format (s, "SR: localsid_index:[%d]", index));
1548 const static dpo_vft_t sr_loc_vft = {
1549 .dv_lock = sr_dpo_lock,
1550 .dv_unlock = sr_dpo_unlock,
1551 .dv_format = format_sr_dpo,
1554 const static char *const sr_loc_ip6_nodes[] = {
1559 const static char *const *const sr_loc_nodes[DPO_PROTO_NUM] = {
1560 [DPO_PROTO_IP6] = sr_loc_ip6_nodes,
1563 const static char *const sr_loc_d_ip6_nodes[] = {
1568 const static char *const *const sr_loc_d_nodes[DPO_PROTO_NUM] = {
1569 [DPO_PROTO_IP6] = sr_loc_d_ip6_nodes,
1573 /*************************** SR LocalSID plugins ******************************/
1575 * @brief SR LocalSID plugin registry
1578 sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name,
1579 u8 * keyword_str, u8 * def_str,
1580 u8 * params_str, u8 prefix_length,
1582 format_function_t * ls_format,
1583 unformat_function_t * ls_unformat,
1584 sr_plugin_callback_t * creation_fn,
1585 sr_plugin_callback_t * removal_fn)
1587 ip6_sr_main_t *sm = &sr_main;
1590 sr_localsid_fn_registration_t *plugin;
1592 /* Did this function exist? If so update it */
1593 p = hash_get_mem (sm->plugin_functions_by_key, fn_name);
1596 plugin = pool_elt_at_index (sm->plugin_functions, p[0]);
1598 /* Else create a new one and set hash key */
1601 pool_get (sm->plugin_functions, plugin);
1602 hash_set_mem (sm->plugin_functions_by_key, fn_name,
1603 plugin - sm->plugin_functions);
1606 clib_memset (plugin, 0, sizeof (*plugin));
1608 plugin->sr_localsid_function_number = (plugin - sm->plugin_functions);
1609 plugin->sr_localsid_function_number += SR_BEHAVIOR_LAST;
1610 plugin->prefix_length = prefix_length;
1611 plugin->ls_format = ls_format;
1612 plugin->ls_unformat = ls_unformat;
1613 plugin->creation = creation_fn;
1614 plugin->removal = removal_fn;
1615 clib_memcpy (&plugin->dpo, dpo, sizeof (dpo_type_t));
1616 plugin->function_name = format (0, "%s%c", fn_name, 0);
1617 plugin->keyword_str = format (0, "%s%c", keyword_str, 0);
1618 plugin->def_str = format (0, "%s%c", def_str, 0);
1619 plugin->params_str = format (0, "%s%c", params_str, 0);
1621 return plugin->sr_localsid_function_number;
1625 * @brief CLI function to 'show' all available SR LocalSID behaviors
1627 static clib_error_t *
1628 show_sr_localsid_behaviors_command_fn (vlib_main_t * vm,
1629 unformat_input_t * input,
1630 vlib_cli_command_t * cmd)
1632 ip6_sr_main_t *sm = &sr_main;
1633 sr_localsid_fn_registration_t *plugin;
1634 sr_localsid_fn_registration_t **plugins_vec = 0;
1637 vlib_cli_output (vm,
1638 "SR LocalSIDs behaviors:\n-----------------------\n\n");
1641 pool_foreach (plugin, sm->plugin_functions,
1642 ({ vec_add1 (plugins_vec, plugin); }));
1645 /* Print static behaviors */
1646 vlib_cli_output (vm, "Default behaviors:\n"
1647 "\tEnd\t-> Endpoint.\n"
1648 "\tEnd.X\t-> Endpoint with Layer-3 cross-connect.\n"
1649 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1650 "\tEnd.T\t-> Endpoint with specific IPv6 table lookup.\n"
1651 "\t\tParameters: '<fib_table>'\n"
1652 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
1653 "\t\tParameters: '<iface>'\n"
1654 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
1655 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1656 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
1657 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
1658 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
1659 "\t\tParameters: '<ip6_fib_table>'\n"
1660 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
1661 "\t\tParameters: '<ip4_fib_table>'\n");
1662 vlib_cli_output (vm, "Plugin behaviors:\n");
1663 for (i = 0; i < vec_len (plugins_vec); i++)
1665 plugin = plugins_vec[i];
1666 vlib_cli_output (vm, "\t%s\t-> %s.\n", plugin->keyword_str,
1668 vlib_cli_output (vm, "\t\tParameters: '%s'\n", plugin->params_str);
1674 VLIB_CLI_COMMAND (show_sr_localsid_behaviors_command, static) = {
1675 .path = "show sr localsids behaviors",
1676 .short_help = "show sr localsids behaviors",
1677 .function = show_sr_localsid_behaviors_command_fn,
1682 * @brief SR LocalSID initialization
1685 sr_localsids_init (vlib_main_t * vm)
1687 /* Init memory for function keys */
1688 ip6_sr_main_t *sm = &sr_main;
1689 mhash_init (&sm->sr_localsids_index_hash, sizeof (uword),
1690 sizeof (ip6_address_t));
1691 /* Init SR behaviors DPO type */
1692 sr_localsid_dpo_type = dpo_register_new_type (&sr_loc_vft, sr_loc_nodes);
1693 /* Init SR behaviors DPO type */
1694 sr_localsid_d_dpo_type =
1695 dpo_register_new_type (&sr_loc_vft, sr_loc_d_nodes);
1696 /* Init memory for localsid plugins */
1697 sm->plugin_functions_by_key = hash_create_string (0, sizeof (uword));
1701 VLIB_INIT_FUNCTION (sr_localsids_init);
1703 * fd.io coding-style-patch-verification: ON
1706 * eval: (c-set-style "gnu")