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 if (localsid_prefix_len != 0)
149 pref_length = localsid_prefix_len;
152 /* Check whether there exists a FIB entry with such address */
154 .fp_proto = FIB_PROTOCOL_IP6,
155 .fp_len = pref_length,
158 pfx.fp_addr.as_u64[0] = localsid_addr->as_u64[0];
159 pfx.fp_addr.as_u64[1] = localsid_addr->as_u64[1];
160 pfx.fp_len = pref_length;
162 /* Lookup the FIB index associated to the table id provided */
163 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP6, fib_table);
167 /* Lookup the localsid in such FIB table */
168 fib_node_index_t fei = fib_table_lookup_exact_match (fib_index, &pfx);
169 if (FIB_NODE_INDEX_INVALID != fei)
170 return -4; //There is an entry for such address (the localsid addr)
172 /* Create a new localsid registry */
173 pool_get (sm->localsids, ls);
174 clib_memset (ls, 0, sizeof (*ls));
176 clib_memcpy (&ls->localsid, localsid_addr, sizeof (ip6_address_t));
177 ls->localsid_prefix_len = pref_length;
178 ls->end_psp = end_psp;
179 ls->behavior = behavior;
180 ls->nh_adj = (u32) ~ 0;
181 ls->fib_table = fib_table;
184 case SR_BEHAVIOR_END:
187 ls->sw_if_index = sw_if_index;
188 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
191 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP6, sw_if_index);
193 case SR_BEHAVIOR_DX4:
194 ls->sw_if_index = sw_if_index;
195 clib_memcpy (&ls->next_hop.ip4, &nh_addr->ip4, sizeof (ip4_address_t));
197 case SR_BEHAVIOR_DX6:
198 ls->sw_if_index = sw_if_index;
199 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
201 case SR_BEHAVIOR_DT6:
202 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP6, sw_if_index);
204 case SR_BEHAVIOR_DT4:
205 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP4, sw_if_index);
207 case SR_BEHAVIOR_DX2:
208 ls->sw_if_index = sw_if_index;
209 ls->vlan_index = vlan_index;
213 /* Figure out the adjacency magic for Xconnect variants */
214 if (ls->behavior == SR_BEHAVIOR_X || ls->behavior == SR_BEHAVIOR_DX4
215 || ls->behavior == SR_BEHAVIOR_DX6)
217 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
219 /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
220 if (ls->behavior == SR_BEHAVIOR_DX6 || ls->behavior == SR_BEHAVIOR_X)
221 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
222 nh_addr, sw_if_index);
224 else if (ls->behavior == SR_BEHAVIOR_DX4)
225 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
226 nh_addr, sw_if_index);
228 /* Check for ADJ creation error. If so panic */
229 if (nh_adj_index == ADJ_INDEX_INVALID)
231 pool_put (sm->localsids, ls);
235 ls->nh_adj = nh_adj_index;
239 if (ls->behavior == SR_BEHAVIOR_END || ls->behavior == SR_BEHAVIOR_X
240 || ls->behavior == SR_BEHAVIOR_T)
241 dpo_set (&dpo, sr_localsid_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
242 else if (ls->behavior > SR_BEHAVIOR_D_FIRST
243 && ls->behavior < SR_BEHAVIOR_LAST)
244 dpo_set (&dpo, sr_localsid_d_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
245 else if (ls->behavior >= SR_BEHAVIOR_LAST)
247 sr_localsid_fn_registration_t *plugin = 0;
248 plugin = pool_elt_at_index (sm->plugin_functions,
249 ls->behavior - SR_BEHAVIOR_LAST);
250 /* Copy the unformat memory result */
251 ls->plugin_mem = ls_plugin_mem;
252 /* Callback plugin creation function */
253 rv = plugin->creation (ls);
256 pool_put (sm->localsids, ls);
259 dpo_set (&dpo, plugin->dpo, DPO_PROTO_IP6, ls - sm->localsids);
262 /* Set hash key for searching localsid by address */
263 mhash_set (&sm->sr_localsids_index_hash, localsid_addr, ls - sm->localsids,
266 fib_table_entry_special_dpo_add (fib_index, &pfx, FIB_SOURCE_SR,
267 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
270 /* Set counter to zero */
271 vlib_validate_combined_counter (&(sm->sr_ls_valid_counters),
273 vlib_validate_combined_counter (&(sm->sr_ls_invalid_counters),
276 vlib_zero_combined_counter (&(sm->sr_ls_valid_counters),
278 vlib_zero_combined_counter (&(sm->sr_ls_invalid_counters),
285 * @brief SR LocalSID CLI function.
287 * @see sr_cli_localsid
289 static clib_error_t *
290 sr_cli_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
291 vlib_cli_command_t * cmd)
293 vnet_main_t *vnm = vnet_get_main ();
294 ip6_sr_main_t *sm = &sr_main;
295 u32 sw_if_index = (u32) ~ 0, vlan_index = (u32) ~ 0, fib_index = 0;
299 ip6_address_t resulting_address;
300 ip46_address_t next_hop;
301 char address_set = 0;
303 void *ls_plugin_mem = 0;
307 clib_memset (&resulting_address, 0, sizeof (ip6_address_t));
308 ip46_address_reset (&next_hop);
310 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
312 if (unformat (input, "del"))
314 else if (!address_set
315 && unformat (input, "address %U/%u", unformat_ip6_address,
316 &resulting_address, &prefix_len))
318 else if (!address_set
319 && unformat (input, "address %U", unformat_ip6_address,
322 else if (!address_set
323 && unformat (input, "addr %U", unformat_ip6_address,
326 else if (unformat (input, "fib-table %u", &fib_index));
327 else if (vlan_index == (u32) ~ 0
328 && unformat (input, "vlan %u", &vlan_index));
329 else if (!behavior && unformat (input, "behavior"))
331 if (unformat (input, "end.x %U %U",
332 unformat_vnet_sw_interface, vnm, &sw_if_index,
333 unformat_ip6_address, &next_hop.ip6))
334 behavior = SR_BEHAVIOR_X;
335 else if (unformat (input, "end.t %u", &sw_if_index))
336 behavior = SR_BEHAVIOR_T;
337 else if (unformat (input, "end.dx6 %U %U",
338 unformat_vnet_sw_interface, vnm, &sw_if_index,
339 unformat_ip6_address, &next_hop.ip6))
340 behavior = SR_BEHAVIOR_DX6;
341 else if (unformat (input, "end.dx4 %U %U",
342 unformat_vnet_sw_interface, vnm, &sw_if_index,
343 unformat_ip4_address, &next_hop.ip4))
344 behavior = SR_BEHAVIOR_DX4;
345 else if (unformat (input, "end.dx2 %U",
346 unformat_vnet_sw_interface, vnm, &sw_if_index))
347 behavior = SR_BEHAVIOR_DX2;
348 else if (unformat (input, "end.dt6 %u", &sw_if_index))
349 behavior = SR_BEHAVIOR_DT6;
350 else if (unformat (input, "end.dt4 %u", &sw_if_index))
351 behavior = SR_BEHAVIOR_DT4;
354 /* Loop over all the plugin behavior format functions */
355 sr_localsid_fn_registration_t *plugin = 0, **vec_plugins = 0;
356 sr_localsid_fn_registration_t **plugin_it = 0;
358 /* Create a vector out of the plugin pool as recommended */
360 pool_foreach (plugin, sm->plugin_functions,
362 vec_add1 (vec_plugins, plugin);
366 vec_foreach (plugin_it, vec_plugins)
369 (input, "%U", (*plugin_it)->ls_unformat, &ls_plugin_mem))
371 behavior = (*plugin_it)->sr_localsid_function_number;
379 if (unformat (input, "end"))
380 behavior = SR_BEHAVIOR_END;
385 else if (!end_psp && unformat (input, "psp"))
391 if (!behavior && end_psp)
392 behavior = SR_BEHAVIOR_END;
395 return clib_error_return (0,
396 "Error: SRv6 LocalSID address is mandatory.");
397 if (!is_del && !behavior)
398 return clib_error_return (0,
399 "Error: SRv6 LocalSID behavior is mandatory.");
400 if (vlan_index != (u32) ~ 0)
401 return clib_error_return (0,
402 "Error: SRv6 End.DX2 with rewrite VLAN tag not supported by now.");
403 if (end_psp && !(behavior == SR_BEHAVIOR_END || behavior == SR_BEHAVIOR_X))
404 return clib_error_return (0,
405 "Error: SRv6 PSP only compatible with End and End.X");
408 sr_cli_localsid (is_del, &resulting_address, prefix_len, end_psp,
409 behavior, sw_if_index, vlan_index, fib_index, &next_hop,
419 return clib_error_return (0,
420 "Identical localsid already exists. Requested localsid not created.");
422 return clib_error_return (0,
423 "The requested localsid could not be deleted. SR localsid not found");
425 return clib_error_return (0, "FIB table %u does not exist", fib_index);
427 return clib_error_return (0, "There is already one FIB entry for the"
428 "requested localsid non segment routing related");
430 return clib_error_return (0,
431 "Could not create ARP/ND entry for such next_hop. Internal error.");
433 return clib_error_return (0,
434 "Error on the plugin based localsid creation.");
436 return clib_error_return (0, "BUG: sr localsid returns %d", rv);
442 VLIB_CLI_COMMAND (sr_localsid_command, static) = {
443 .path = "sr localsid",
444 .short_help = "sr localsid (del) address XX:XX::YY:YY"
445 "(fib-table 8) behavior STRING",
447 "Create SR LocalSID and binds it to a particular behavior\n"
449 "\tlocalSID IPv6_addr(128b) LocalSID IPv6 address\n"
450 "\t(fib-table X) Optional. VRF where to install SRv6 localsid\n"
451 "\tbehavior STRING Specifies the behavior\n"
453 "\tEnd\t-> Endpoint.\n"
454 "\tEnd.X\t-> Endpoint with decapsulation and Layer-3 cross-connect.\n"
455 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
456 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
457 "\t\tParameters: '<iface>'\n"
458 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
459 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
460 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
461 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
462 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
463 "\t\tParameters: '<ip6_fib_table>'\n"
464 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
465 "\t\tParameters: '<ip4_fib_table>'\n",
466 .function = sr_cli_localsid_command_fn,
471 * @brief CLI function to 'show' all SR LocalSIDs on console.
473 static clib_error_t *
474 show_sr_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
475 vlib_cli_command_t * cmd)
477 vnet_main_t *vnm = vnet_get_main ();
478 ip6_sr_main_t *sm = &sr_main;
479 ip6_sr_localsid_t **localsid_list = 0;
480 ip6_sr_localsid_t *ls;
483 vlib_cli_output (vm, "SRv6 - My LocalSID Table:");
484 vlib_cli_output (vm, "=========================");
486 pool_foreach (ls, sm->localsids, ({ vec_add1 (localsid_list, ls); }));
488 for (i = 0; i < vec_len (localsid_list); i++)
490 ls = localsid_list[i];
491 switch (ls->behavior)
493 case SR_BEHAVIOR_END:
494 vlib_cli_output (vm, "\tAddress: \t%U/%u\n\tBehavior: \tEnd",
495 format_ip6_address, &ls->localsid,
496 ls->localsid_prefix_len);
500 "\tAddress: \t%U/%u\n\tBehavior: \tX (Endpoint with Layer-3 cross-connect)"
501 "\n\tIface: \t%U\n\tNext hop: \t%U",
502 format_ip6_address, &ls->localsid,
503 ls->localsid_prefix_len,
504 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
505 format_ip6_address, &ls->next_hop.ip6);
509 "\tAddress: \t%U/%u\n\tBehavior: \tT (Endpoint with specific IPv6 table lookup)"
511 format_ip6_address, &ls->localsid,
512 ls->localsid_prefix_len,
513 fib_table_get_table_id (ls->vrf_index,
516 case SR_BEHAVIOR_DX4:
518 "\tAddress: \t%U/%u\n\tBehavior: \tDX4 (Endpoint with decapsulation and IPv4 cross-connect)"
519 "\n\tIface: \t%U\n\tNext hop: \t%U",
520 format_ip6_address, &ls->localsid,
521 ls->localsid_prefix_len,
522 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
523 format_ip4_address, &ls->next_hop.ip4);
525 case SR_BEHAVIOR_DX6:
527 "\tAddress: \t%U/%u\n\tBehavior: \tDX6 (Endpoint with decapsulation and IPv6 cross-connect)"
528 "\n\tIface: \t%U\n\tNext hop: \t%U",
529 format_ip6_address, &ls->localsid,
530 ls->localsid_prefix_len,
531 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
532 format_ip6_address, &ls->next_hop.ip6);
534 case SR_BEHAVIOR_DX2:
535 if (ls->vlan_index == (u32) ~ 0)
537 "\tAddress: \t%U/%u\n\tBehavior: \tDX2 (Endpoint with decapulation and Layer-2 cross-connect)"
538 "\n\tIface: \t%U", format_ip6_address,
539 &ls->localsid, ls->localsid_prefix_len,
540 format_vnet_sw_if_index_name, vnm,
544 "Unsupported yet. (DX2 with egress VLAN rewrite)");
546 case SR_BEHAVIOR_DT6:
548 "\tAddress: \t%U/%u\n\tBehavior: \tDT6 (Endpoint with decapsulation and specific IPv6 table lookup)"
549 "\n\tTable: %u", format_ip6_address, &ls->localsid,
550 ls->localsid_prefix_len,
551 fib_table_get_table_id (ls->vrf_index,
554 case SR_BEHAVIOR_DT4:
556 "\tAddress: \t%U/%u\n\tBehavior: \tDT4 (Endpoint with decapsulation and specific IPv4 table lookup)"
557 "\n\tTable: \t%u", format_ip6_address,
558 &ls->localsid, ls->localsid_prefix_len,
559 fib_table_get_table_id (ls->vrf_index,
563 if (ls->behavior >= SR_BEHAVIOR_LAST)
565 sr_localsid_fn_registration_t *plugin =
566 pool_elt_at_index (sm->plugin_functions,
567 ls->behavior - SR_BEHAVIOR_LAST);
569 vlib_cli_output (vm, "\tAddress: \t%U/%u\n"
570 "\tBehavior: \t%s (%s)\n\t%U",
571 format_ip6_address, &ls->localsid,
572 ls->localsid_prefix_len, plugin->keyword_str,
573 plugin->def_str, plugin->ls_format,
577 //Should never get here...
578 vlib_cli_output (vm, "Internal error");
582 vlib_cli_output (vm, "\tPSP: \tTrue\n");
585 vlib_counter_t valid, invalid;
586 vlib_get_combined_counter (&(sm->sr_ls_valid_counters), i, &valid);
587 vlib_get_combined_counter (&(sm->sr_ls_invalid_counters), i, &invalid);
588 vlib_cli_output (vm, "\tGood traffic: \t[%Ld packets : %Ld bytes]\n",
589 valid.packets, valid.bytes);
590 vlib_cli_output (vm, "\tBad traffic: \t[%Ld packets : %Ld bytes]\n",
591 invalid.packets, invalid.bytes);
592 vlib_cli_output (vm, "--------------------");
598 VLIB_CLI_COMMAND (show_sr_localsid_command, static) = {
599 .path = "show sr localsids",
600 .short_help = "show sr localsids",
601 .function = show_sr_localsid_command_fn,
606 * @brief Function to 'clear' ALL SR localsid counters
608 static clib_error_t *
609 clear_sr_localsid_counters_command_fn (vlib_main_t * vm,
610 unformat_input_t * input,
611 vlib_cli_command_t * cmd)
613 ip6_sr_main_t *sm = &sr_main;
615 vlib_clear_combined_counters (&(sm->sr_ls_valid_counters));
616 vlib_clear_combined_counters (&(sm->sr_ls_invalid_counters));
622 VLIB_CLI_COMMAND (clear_sr_localsid_counters_command, static) = {
623 .path = "clear sr localsid-counters",
624 .short_help = "clear sr localsid-counters",
625 .function = clear_sr_localsid_counters_command_fn,
629 /************************ SR LocalSID graphs node ****************************/
631 * @brief SR localsid node trace
635 ip6_address_t localsid;
640 } sr_localsid_trace_t;
642 #define foreach_sr_localsid_error \
643 _(NO_INNER_HEADER, "(SR-Error) No inner IP header") \
644 _(NO_MORE_SEGMENTS, "(SR-Error) No more segments") \
645 _(NO_SRH, "(SR-Error) No SR header") \
646 _(NO_PSP, "(SR-Error) PSP Not available (segments left > 0)") \
647 _(NOT_LS, "(SR-Error) Decaps not available (segments left > 0)") \
648 _(L2, "(SR-Error) SRv6 decapsulated a L2 frame without dest")
652 #define _(sym,str) SR_LOCALSID_ERROR_##sym,
653 foreach_sr_localsid_error
656 } sr_localsid_error_t;
658 static char *sr_localsid_error_strings[] = {
659 #define _(sym,string) string,
660 foreach_sr_localsid_error
664 #define foreach_sr_localsid_next \
665 _(ERROR, "error-drop") \
666 _(IP6_LOOKUP, "ip6-lookup") \
667 _(IP4_LOOKUP, "ip4-lookup") \
668 _(IP6_REWRITE, "ip6-rewrite") \
669 _(IP4_REWRITE, "ip4-rewrite") \
670 _(INTERFACE_OUTPUT, "interface-output")
674 #define _(s,n) SR_LOCALSID_NEXT_##s,
675 foreach_sr_localsid_next
678 } sr_localsid_next_t;
681 * @brief SR LocalSID graph node trace function
686 format_sr_localsid_trace (u8 * s, va_list * args)
688 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
689 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
690 sr_localsid_trace_t *t = va_arg (*args, sr_localsid_trace_t *);
693 format (s, "SR-LOCALSID:\n\tLocalsid: %U\n", format_ip6_address,
697 case SR_BEHAVIOR_END:
698 s = format (s, "\tBehavior: End\n");
700 case SR_BEHAVIOR_DX6:
701 s = format (s, "\tBehavior: Decapsulation with IPv6 L3 xconnect\n");
703 case SR_BEHAVIOR_DX4:
704 s = format (s, "\tBehavior: Decapsulation with IPv4 L3 xconnect\n");
707 s = format (s, "\tBehavior: IPv6 L3 xconnect\n");
710 s = format (s, "\tBehavior: IPv6 specific table lookup\n");
712 case SR_BEHAVIOR_DT6:
713 s = format (s, "\tBehavior: Decapsulation with IPv6 Table lookup\n");
715 case SR_BEHAVIOR_DT4:
716 s = format (s, "\tBehavior: Decapsulation with IPv4 Table lookup\n");
718 case SR_BEHAVIOR_DX2:
719 s = format (s, "\tBehavior: Decapsulation with L2 xconnect\n");
722 s = format (s, "\tBehavior: defined in plugin\n"); //TODO
725 if (t->num_segments != 0xFF)
727 if (t->num_segments > 0)
729 s = format (s, "\tSegments left: %d\n", t->segments_left);
730 s = format (s, "\tSID list: [in ietf order]");
732 for (i = 0; i < t->num_segments; i++)
734 s = format (s, "\n\t-> %U", format_ip6_address,
735 (ip6_address_t *) & t->sr[i *
736 sizeof (ip6_address_t)]);
744 * @brief Function doing End processing.
746 static_always_inline void
747 end_srh_processing (vlib_node_runtime_t * node,
750 ip6_sr_header_t * sr0,
751 ip6_sr_localsid_t * ls0,
752 u32 * next0, u8 psp, ip6_ext_header_t * prev0)
754 ip6_address_t *new_dst0;
756 if (PREDICT_TRUE (sr0 && sr0->type == ROUTING_HEADER_TYPE_SR))
758 if (sr0->segments_left == 1 && psp)
761 u64 *copy_dst0, *copy_src0;
762 u32 copy_len_u64s0 = 0;
764 ip0->dst_address.as_u64[0] = sr0->segments->as_u64[0];
765 ip0->dst_address.as_u64[1] = sr0->segments->as_u64[1];
767 /* Remove the SRH taking care of the rest of IPv6 ext header */
769 prev0->next_hdr = sr0->protocol;
771 ip0->protocol = sr0->protocol;
773 sr_len = ip6_ext_header_len (sr0);
774 vlib_buffer_advance (b0, sr_len);
775 new_l0 = clib_net_to_host_u16 (ip0->payload_length) - sr_len;
776 ip0->payload_length = clib_host_to_net_u16 (new_l0);
777 copy_src0 = (u64 *) ip0;
778 copy_dst0 = copy_src0 + (sr0->length + 1);
779 /* number of 8 octet units to copy
780 * By default in absence of extension headers it is equal to length of ip6 header
781 * With extension headers it number of 8 octet units of ext headers preceding
785 (((u8 *) sr0 - (u8 *) ip0) - sizeof (ip6_header_t)) >> 3;
786 copy_dst0[4 + copy_len_u64s0] = copy_src0[4 + copy_len_u64s0];
787 copy_dst0[3 + copy_len_u64s0] = copy_src0[3 + copy_len_u64s0];
788 copy_dst0[2 + copy_len_u64s0] = copy_src0[2 + copy_len_u64s0];
789 copy_dst0[1 + copy_len_u64s0] = copy_src0[1 + copy_len_u64s0];
790 copy_dst0[0 + copy_len_u64s0] = copy_src0[0 + copy_len_u64s0];
793 for (i = copy_len_u64s0 - 1; i >= 0; i--)
795 copy_dst0[i] = copy_src0[i];
798 if (ls0->behavior == SR_BEHAVIOR_X)
800 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
801 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
803 else if (ls0->behavior == SR_BEHAVIOR_T)
805 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
808 else if (PREDICT_TRUE (sr0->segments_left > 0))
810 sr0->segments_left -= 1;
811 new_dst0 = (ip6_address_t *) (sr0->segments);
812 new_dst0 += sr0->segments_left;
813 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
814 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
816 if (ls0->behavior == SR_BEHAVIOR_X)
818 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
819 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
821 else if (ls0->behavior == SR_BEHAVIOR_T)
823 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
828 *next0 = SR_LOCALSID_NEXT_ERROR;
829 b0->error = node->errors[SR_LOCALSID_ERROR_NO_MORE_SEGMENTS];
834 /* Error. Routing header of type != SR */
835 *next0 = SR_LOCALSID_NEXT_ERROR;
836 b0->error = node->errors[SR_LOCALSID_ERROR_NO_SRH];
841 * @brief Function doing SRH processing for D* variants
843 static_always_inline void
844 end_decaps_srh_processing (vlib_node_runtime_t * node,
847 ip6_sr_header_t * sr0,
848 ip6_sr_localsid_t * ls0, u32 * next0)
850 /* Compute the size of the IPv6 header with all Ext. headers */
852 ip6_ext_header_t *next_ext_header;
855 next_proto = ip0->protocol;
856 next_ext_header = (void *) (ip0 + 1);
857 total_size = sizeof (ip6_header_t);
858 while (ip6_ext_hdr (next_proto))
860 total_size += ip6_ext_header_len (next_ext_header);
861 next_proto = next_ext_header->next_hdr;
862 next_ext_header = ip6_ext_next_header (next_ext_header);
865 /* Ensure this is the last segment. Otherwise drop. */
866 if (sr0 && sr0->segments_left != 0)
868 *next0 = SR_LOCALSID_NEXT_ERROR;
869 b0->error = node->errors[SR_LOCALSID_ERROR_NOT_LS];
875 case IP_PROTOCOL_IPV6:
876 /* Encap-End IPv6. Pop outer IPv6 header. */
877 if (ls0->behavior == SR_BEHAVIOR_DX6)
879 vlib_buffer_advance (b0, total_size);
880 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
881 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
884 else if (ls0->behavior == SR_BEHAVIOR_DT6)
886 vlib_buffer_advance (b0, total_size);
887 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
891 case IP_PROTOCOL_IP_IN_IP:
892 /* Encap-End IPv4. Pop outer IPv6 header */
893 if (ls0->behavior == SR_BEHAVIOR_DX4)
895 vlib_buffer_advance (b0, total_size);
896 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
897 *next0 = SR_LOCALSID_NEXT_IP4_REWRITE;
900 else if (ls0->behavior == SR_BEHAVIOR_DT4)
902 vlib_buffer_advance (b0, total_size);
903 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
904 *next0 = SR_LOCALSID_NEXT_IP4_LOOKUP;
908 case IP_PROTOCOL_IP6_ETHERNET:
910 if (ls0->behavior == SR_BEHAVIOR_DX2)
912 vlib_buffer_advance (b0, total_size);
913 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->sw_if_index;
914 *next0 = SR_LOCALSID_NEXT_INTERFACE_OUTPUT;
919 *next0 = SR_LOCALSID_NEXT_ERROR;
920 b0->error = node->errors[SR_LOCALSID_ERROR_NO_INNER_HEADER];
925 * @brief SR LocalSID graph node. Supports all default SR Endpoint variants with decaps
928 sr_localsid_d_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
929 vlib_frame_t * from_frame)
931 u32 n_left_from, next_index, *from, *to_next;
932 ip6_sr_main_t *sm = &sr_main;
933 from = vlib_frame_vector_args (from_frame);
934 n_left_from = from_frame->n_vectors;
935 next_index = node->cached_next_index;
936 u32 thread_index = vm->thread_index;
938 while (n_left_from > 0)
941 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
944 while (n_left_from >= 8 && n_left_to_next >= 4)
946 u32 bi0, bi1, bi2, bi3;
947 vlib_buffer_t *b0, *b1, *b2, *b3;
948 ip6_header_t *ip0, *ip1, *ip2, *ip3;
949 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
950 u32 next0, next1, next2, next3;
951 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
952 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
954 /* Prefetch next iteration. */
956 vlib_buffer_t *p4, *p5, *p6, *p7;
958 p4 = vlib_get_buffer (vm, from[4]);
959 p5 = vlib_get_buffer (vm, from[5]);
960 p6 = vlib_get_buffer (vm, from[6]);
961 p7 = vlib_get_buffer (vm, from[7]);
963 /* Prefetch the buffer header and packet for the N+4 loop iteration */
964 vlib_prefetch_buffer_header (p4, LOAD);
965 vlib_prefetch_buffer_header (p5, LOAD);
966 vlib_prefetch_buffer_header (p6, LOAD);
967 vlib_prefetch_buffer_header (p7, LOAD);
969 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
970 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
971 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
972 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
975 to_next[0] = bi0 = from[0];
976 to_next[1] = bi1 = from[1];
977 to_next[2] = bi2 = from[2];
978 to_next[3] = bi3 = from[3];
984 b0 = vlib_get_buffer (vm, bi0);
985 b1 = vlib_get_buffer (vm, bi1);
986 b2 = vlib_get_buffer (vm, bi2);
987 b3 = vlib_get_buffer (vm, bi3);
990 pool_elt_at_index (sm->localsids,
991 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
993 pool_elt_at_index (sm->localsids,
994 vnet_buffer (b1)->ip.adj_index[VLIB_TX]);
996 pool_elt_at_index (sm->localsids,
997 vnet_buffer (b2)->ip.adj_index[VLIB_TX]);
999 pool_elt_at_index (sm->localsids,
1000 vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
1002 ip0 = vlib_buffer_get_current (b0);
1003 ip1 = vlib_buffer_get_current (b1);
1004 ip2 = vlib_buffer_get_current (b2);
1005 ip3 = vlib_buffer_get_current (b3);
1008 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, NULL);
1010 ip6_ext_header_find (vm, b1, ip1, IP_PROTOCOL_IPV6_ROUTE, NULL);
1012 ip6_ext_header_find (vm, b2, ip2, IP_PROTOCOL_IPV6_ROUTE, NULL);
1014 ip6_ext_header_find (vm, b3, ip3, IP_PROTOCOL_IPV6_ROUTE, NULL);
1016 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1017 end_decaps_srh_processing (node, b1, ip1, sr1, ls1, &next1);
1018 end_decaps_srh_processing (node, b2, ip2, sr2, ls2, &next2);
1019 end_decaps_srh_processing (node, b3, ip3, sr3, ls3, &next3);
1021 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1023 sr_localsid_trace_t *tr =
1024 vlib_add_trace (vm, node, b0, sizeof (*tr));
1025 tr->num_segments = 0;
1026 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1027 sizeof (tr->localsid.as_u8));
1028 tr->behavior = ls0->behavior;
1029 if (ip0 == vlib_buffer_get_current (b0))
1031 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1032 && sr0->type == ROUTING_HEADER_TYPE_SR)
1034 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1036 sr0->length * 8 / sizeof (ip6_address_t);
1037 tr->segments_left = sr0->segments_left;
1041 tr->num_segments = 0xFF;
1044 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
1046 sr_localsid_trace_t *tr =
1047 vlib_add_trace (vm, node, b1, sizeof (*tr));
1048 tr->num_segments = 0;
1049 clib_memcpy (tr->localsid.as_u8, ls1->localsid.as_u8,
1050 sizeof (tr->localsid.as_u8));
1051 tr->behavior = ls1->behavior;
1052 if (ip1 == vlib_buffer_get_current (b1))
1054 if (ip1->protocol == IP_PROTOCOL_IPV6_ROUTE
1055 && sr1->type == ROUTING_HEADER_TYPE_SR)
1057 clib_memcpy (tr->sr, sr1->segments, sr1->length * 8);
1059 sr1->length * 8 / sizeof (ip6_address_t);
1060 tr->segments_left = sr1->segments_left;
1064 tr->num_segments = 0xFF;
1067 if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
1069 sr_localsid_trace_t *tr =
1070 vlib_add_trace (vm, node, b2, sizeof (*tr));
1071 tr->num_segments = 0;
1072 clib_memcpy (tr->localsid.as_u8, ls2->localsid.as_u8,
1073 sizeof (tr->localsid.as_u8));
1074 tr->behavior = ls2->behavior;
1075 if (ip2 == vlib_buffer_get_current (b2))
1077 if (ip2->protocol == IP_PROTOCOL_IPV6_ROUTE
1078 && sr2->type == ROUTING_HEADER_TYPE_SR)
1080 clib_memcpy (tr->sr, sr2->segments, sr2->length * 8);
1082 sr2->length * 8 / sizeof (ip6_address_t);
1083 tr->segments_left = sr2->segments_left;
1087 tr->num_segments = 0xFF;
1090 if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
1092 sr_localsid_trace_t *tr =
1093 vlib_add_trace (vm, node, b3, sizeof (*tr));
1094 tr->num_segments = 0;
1095 clib_memcpy (tr->localsid.as_u8, ls3->localsid.as_u8,
1096 sizeof (tr->localsid.as_u8));
1097 tr->behavior = ls3->behavior;
1098 if (ip3 == vlib_buffer_get_current (b3))
1100 if (ip3->protocol == IP_PROTOCOL_IPV6_ROUTE
1101 && sr3->type == ROUTING_HEADER_TYPE_SR)
1103 clib_memcpy (tr->sr, sr3->segments, sr3->length * 8);
1105 sr3->length * 8 / sizeof (ip6_address_t);
1106 tr->segments_left = sr3->segments_left;
1110 tr->num_segments = 0xFF;
1113 vlib_increment_combined_counter
1115 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1116 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1117 1, vlib_buffer_length_in_chain (vm, b0));
1119 vlib_increment_combined_counter
1121 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1122 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1123 1, vlib_buffer_length_in_chain (vm, b1));
1125 vlib_increment_combined_counter
1127 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1128 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1129 1, vlib_buffer_length_in_chain (vm, b2));
1131 vlib_increment_combined_counter
1133 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1134 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1135 1, vlib_buffer_length_in_chain (vm, b3));
1137 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1138 n_left_to_next, bi0, bi1, bi2, bi3,
1139 next0, next1, next2, next3);
1142 /* Single loop for potentially the last three packets */
1143 while (n_left_from > 0 && n_left_to_next > 0)
1148 ip6_sr_header_t *sr0;
1149 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1150 ip6_sr_localsid_t *ls0;
1157 n_left_to_next -= 1;
1159 b0 = vlib_get_buffer (vm, bi0);
1160 ip0 = vlib_buffer_get_current (b0);
1162 /* Lookup the SR End behavior based on IP DA (adj) */
1164 pool_elt_at_index (sm->localsids,
1165 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1167 /* Find SRH as well as previous header */
1169 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, NULL);
1171 /* SRH processing and End variants */
1172 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1174 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1176 sr_localsid_trace_t *tr =
1177 vlib_add_trace (vm, node, b0, sizeof (*tr));
1178 tr->num_segments = 0;
1179 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1180 sizeof (tr->localsid.as_u8));
1181 tr->behavior = ls0->behavior;
1182 if (ip0 == vlib_buffer_get_current (b0))
1184 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1185 && sr0->type == ROUTING_HEADER_TYPE_SR)
1187 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1189 sr0->length * 8 / sizeof (ip6_address_t);
1190 tr->segments_left = sr0->segments_left;
1194 tr->num_segments = 0xFF;
1197 /* Increase the counters */
1198 vlib_increment_combined_counter
1200 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1201 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1202 1, vlib_buffer_length_in_chain (vm, b0));
1204 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1205 n_left_to_next, bi0, next0);
1207 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1209 return from_frame->n_vectors;
1213 VLIB_REGISTER_NODE (sr_localsid_d_node) = {
1214 .function = sr_localsid_d_fn,
1215 .name = "sr-localsid-d",
1216 .vector_size = sizeof (u32),
1217 .format_trace = format_sr_localsid_trace,
1218 .type = VLIB_NODE_TYPE_INTERNAL,
1219 .n_errors = SR_LOCALSID_N_ERROR,
1220 .error_strings = sr_localsid_error_strings,
1221 .n_next_nodes = SR_LOCALSID_N_NEXT,
1223 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1224 foreach_sr_localsid_next
1231 * @brief SR LocalSID graph node. Supports all default SR Endpoint without decaps
1234 sr_localsid_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1235 vlib_frame_t * from_frame)
1237 u32 n_left_from, next_index, *from, *to_next;
1238 ip6_sr_main_t *sm = &sr_main;
1239 from = vlib_frame_vector_args (from_frame);
1240 n_left_from = from_frame->n_vectors;
1241 next_index = node->cached_next_index;
1242 u32 thread_index = vm->thread_index;
1244 while (n_left_from > 0)
1247 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1250 while (n_left_from >= 8 && n_left_to_next >= 4)
1252 u32 bi0, bi1, bi2, bi3;
1253 vlib_buffer_t *b0, *b1, *b2, *b3;
1254 ip6_header_t *ip0, *ip1, *ip2, *ip3;
1255 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
1256 ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
1257 u32 next0, next1, next2, next3;
1258 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1259 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
1261 /* Prefetch next iteration. */
1263 vlib_buffer_t *p4, *p5, *p6, *p7;
1265 p4 = vlib_get_buffer (vm, from[4]);
1266 p5 = vlib_get_buffer (vm, from[5]);
1267 p6 = vlib_get_buffer (vm, from[6]);
1268 p7 = vlib_get_buffer (vm, from[7]);
1270 /* Prefetch the buffer header and packet for the N+2 loop iteration */
1271 vlib_prefetch_buffer_header (p4, LOAD);
1272 vlib_prefetch_buffer_header (p5, LOAD);
1273 vlib_prefetch_buffer_header (p6, LOAD);
1274 vlib_prefetch_buffer_header (p7, LOAD);
1276 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
1277 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
1278 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
1279 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
1282 to_next[0] = bi0 = from[0];
1283 to_next[1] = bi1 = from[1];
1284 to_next[2] = bi2 = from[2];
1285 to_next[3] = bi3 = from[3];
1289 n_left_to_next -= 4;
1291 b0 = vlib_get_buffer (vm, bi0);
1292 b1 = vlib_get_buffer (vm, bi1);
1293 b2 = vlib_get_buffer (vm, bi2);
1294 b3 = vlib_get_buffer (vm, bi3);
1296 ip0 = vlib_buffer_get_current (b0);
1297 ip1 = vlib_buffer_get_current (b1);
1298 ip2 = vlib_buffer_get_current (b2);
1299 ip3 = vlib_buffer_get_current (b3);
1302 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, &prev0);
1304 ip6_ext_header_find (vm, b1, ip1, IP_PROTOCOL_IPV6_ROUTE, &prev1);
1306 ip6_ext_header_find (vm, b2, ip2, IP_PROTOCOL_IPV6_ROUTE, &prev2);
1308 ip6_ext_header_find (vm, b3, ip3, IP_PROTOCOL_IPV6_ROUTE, &prev3);
1311 pool_elt_at_index (sm->localsids,
1312 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1314 pool_elt_at_index (sm->localsids,
1315 vnet_buffer (b1)->ip.adj_index[VLIB_TX]);
1317 pool_elt_at_index (sm->localsids,
1318 vnet_buffer (b2)->ip.adj_index[VLIB_TX]);
1320 pool_elt_at_index (sm->localsids,
1321 vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
1323 end_srh_processing (node, b0, ip0, sr0, ls0, &next0, ls0->end_psp,
1325 end_srh_processing (node, b1, ip1, sr1, ls1, &next1, ls1->end_psp,
1327 end_srh_processing (node, b2, ip2, sr2, ls2, &next2, ls2->end_psp,
1329 end_srh_processing (node, b3, ip3, sr3, ls3, &next3, ls3->end_psp,
1332 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1334 sr_localsid_trace_t *tr =
1335 vlib_add_trace (vm, node, b0, sizeof (*tr));
1336 tr->num_segments = 0;
1337 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1338 sizeof (tr->localsid.as_u8));
1339 tr->behavior = ls0->behavior;
1340 if (ip0 == vlib_buffer_get_current (b0))
1342 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1343 && sr0->type == ROUTING_HEADER_TYPE_SR)
1345 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1347 sr0->length * 8 / sizeof (ip6_address_t);
1348 tr->segments_left = sr0->segments_left;
1352 tr->num_segments = 0xFF;
1355 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
1357 sr_localsid_trace_t *tr =
1358 vlib_add_trace (vm, node, b1, sizeof (*tr));
1359 tr->num_segments = 0;
1360 clib_memcpy (tr->localsid.as_u8, ls1->localsid.as_u8,
1361 sizeof (tr->localsid.as_u8));
1362 tr->behavior = ls1->behavior;
1363 if (ip1 == vlib_buffer_get_current (b1))
1365 if (ip1->protocol == IP_PROTOCOL_IPV6_ROUTE
1366 && sr1->type == ROUTING_HEADER_TYPE_SR)
1368 clib_memcpy (tr->sr, sr1->segments, sr1->length * 8);
1370 sr1->length * 8 / sizeof (ip6_address_t);
1371 tr->segments_left = sr1->segments_left;
1375 tr->num_segments = 0xFF;
1378 if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
1380 sr_localsid_trace_t *tr =
1381 vlib_add_trace (vm, node, b2, sizeof (*tr));
1382 tr->num_segments = 0;
1383 clib_memcpy (tr->localsid.as_u8, ls2->localsid.as_u8,
1384 sizeof (tr->localsid.as_u8));
1385 tr->behavior = ls2->behavior;
1386 if (ip2 == vlib_buffer_get_current (b2))
1388 if (ip2->protocol == IP_PROTOCOL_IPV6_ROUTE
1389 && sr2->type == ROUTING_HEADER_TYPE_SR)
1391 clib_memcpy (tr->sr, sr2->segments, sr2->length * 8);
1393 sr2->length * 8 / sizeof (ip6_address_t);
1394 tr->segments_left = sr2->segments_left;
1398 tr->num_segments = 0xFF;
1401 if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
1403 sr_localsid_trace_t *tr =
1404 vlib_add_trace (vm, node, b3, sizeof (*tr));
1405 tr->num_segments = 0;
1406 clib_memcpy (tr->localsid.as_u8, ls3->localsid.as_u8,
1407 sizeof (tr->localsid.as_u8));
1408 tr->behavior = ls3->behavior;
1409 if (ip3 == vlib_buffer_get_current (b3))
1411 if (ip3->protocol == IP_PROTOCOL_IPV6_ROUTE
1412 && sr3->type == ROUTING_HEADER_TYPE_SR)
1414 clib_memcpy (tr->sr, sr3->segments, sr3->length * 8);
1416 sr3->length * 8 / sizeof (ip6_address_t);
1417 tr->segments_left = sr3->segments_left;
1421 tr->num_segments = 0xFF;
1424 vlib_increment_combined_counter
1426 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1427 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1428 1, vlib_buffer_length_in_chain (vm, b0));
1430 vlib_increment_combined_counter
1432 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1433 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1434 1, vlib_buffer_length_in_chain (vm, b1));
1436 vlib_increment_combined_counter
1438 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1439 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1440 1, vlib_buffer_length_in_chain (vm, b2));
1442 vlib_increment_combined_counter
1444 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1445 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1446 1, vlib_buffer_length_in_chain (vm, b3));
1448 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1449 n_left_to_next, bi0, bi1, bi2, bi3,
1450 next0, next1, next2, next3);
1453 /* Single loop for potentially the last three packets */
1454 while (n_left_from > 0 && n_left_to_next > 0)
1458 ip6_header_t *ip0 = 0;
1459 ip6_ext_header_t *prev0;
1460 ip6_sr_header_t *sr0;
1461 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1462 ip6_sr_localsid_t *ls0;
1469 n_left_to_next -= 1;
1471 b0 = vlib_get_buffer (vm, bi0);
1472 ip0 = vlib_buffer_get_current (b0);
1474 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, &prev0);
1476 /* Lookup the SR End behavior based on IP DA (adj) */
1478 pool_elt_at_index (sm->localsids,
1479 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1481 /* SRH processing */
1482 end_srh_processing (node, b0, ip0, sr0, ls0, &next0, ls0->end_psp,
1485 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1487 sr_localsid_trace_t *tr =
1488 vlib_add_trace (vm, node, b0, sizeof (*tr));
1489 tr->num_segments = 0;
1490 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1491 sizeof (tr->localsid.as_u8));
1492 tr->behavior = ls0->behavior;
1493 if (ip0 == vlib_buffer_get_current (b0))
1495 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1496 && sr0->type == ROUTING_HEADER_TYPE_SR)
1498 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1500 sr0->length * 8 / sizeof (ip6_address_t);
1501 tr->segments_left = sr0->segments_left;
1505 tr->num_segments = 0xFF;
1508 vlib_increment_combined_counter
1510 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
1511 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1512 1, vlib_buffer_length_in_chain (vm, b0));
1514 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1515 n_left_to_next, bi0, next0);
1517 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1519 return from_frame->n_vectors;
1523 VLIB_REGISTER_NODE (sr_localsid_node) = {
1524 .function = sr_localsid_fn,
1525 .name = "sr-localsid",
1526 .vector_size = sizeof (u32),
1527 .format_trace = format_sr_localsid_trace,
1528 .type = VLIB_NODE_TYPE_INTERNAL,
1529 .n_errors = SR_LOCALSID_N_ERROR,
1530 .error_strings = sr_localsid_error_strings,
1531 .n_next_nodes = SR_LOCALSID_N_NEXT,
1533 #define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1534 foreach_sr_localsid_next
1541 format_sr_dpo (u8 * s, va_list * args)
1543 index_t index = va_arg (*args, index_t);
1544 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
1546 return (format (s, "SR: localsid_index:[%d]", index));
1549 const static dpo_vft_t sr_loc_vft = {
1550 .dv_lock = sr_dpo_lock,
1551 .dv_unlock = sr_dpo_unlock,
1552 .dv_format = format_sr_dpo,
1555 const static char *const sr_loc_ip6_nodes[] = {
1560 const static char *const *const sr_loc_nodes[DPO_PROTO_NUM] = {
1561 [DPO_PROTO_IP6] = sr_loc_ip6_nodes,
1564 const static char *const sr_loc_d_ip6_nodes[] = {
1569 const static char *const *const sr_loc_d_nodes[DPO_PROTO_NUM] = {
1570 [DPO_PROTO_IP6] = sr_loc_d_ip6_nodes,
1574 /*************************** SR LocalSID plugins ******************************/
1576 * @brief SR LocalSID plugin registry
1579 sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name,
1580 u8 * keyword_str, u8 * def_str,
1581 u8 * params_str, u8 prefix_length,
1583 format_function_t * ls_format,
1584 unformat_function_t * ls_unformat,
1585 sr_plugin_callback_t * creation_fn,
1586 sr_plugin_callback_t * removal_fn)
1588 ip6_sr_main_t *sm = &sr_main;
1591 sr_localsid_fn_registration_t *plugin;
1593 /* Did this function exist? If so update it */
1594 p = hash_get_mem (sm->plugin_functions_by_key, fn_name);
1597 plugin = pool_elt_at_index (sm->plugin_functions, p[0]);
1599 /* Else create a new one and set hash key */
1602 pool_get (sm->plugin_functions, plugin);
1603 hash_set_mem (sm->plugin_functions_by_key, fn_name,
1604 plugin - sm->plugin_functions);
1607 clib_memset (plugin, 0, sizeof (*plugin));
1609 plugin->sr_localsid_function_number = (plugin - sm->plugin_functions);
1610 plugin->sr_localsid_function_number += SR_BEHAVIOR_LAST;
1611 plugin->prefix_length = prefix_length;
1612 plugin->ls_format = ls_format;
1613 plugin->ls_unformat = ls_unformat;
1614 plugin->creation = creation_fn;
1615 plugin->removal = removal_fn;
1616 clib_memcpy (&plugin->dpo, dpo, sizeof (dpo_type_t));
1617 plugin->function_name = format (0, "%s%c", fn_name, 0);
1618 plugin->keyword_str = format (0, "%s%c", keyword_str, 0);
1619 plugin->def_str = format (0, "%s%c", def_str, 0);
1620 plugin->params_str = format (0, "%s%c", params_str, 0);
1622 return plugin->sr_localsid_function_number;
1626 * @brief CLI function to 'show' all available SR LocalSID behaviors
1628 static clib_error_t *
1629 show_sr_localsid_behaviors_command_fn (vlib_main_t * vm,
1630 unformat_input_t * input,
1631 vlib_cli_command_t * cmd)
1633 ip6_sr_main_t *sm = &sr_main;
1634 sr_localsid_fn_registration_t *plugin;
1635 sr_localsid_fn_registration_t **plugins_vec = 0;
1638 vlib_cli_output (vm,
1639 "SR LocalSIDs behaviors:\n-----------------------\n\n");
1642 pool_foreach (plugin, sm->plugin_functions,
1643 ({ vec_add1 (plugins_vec, plugin); }));
1646 /* Print static behaviors */
1647 vlib_cli_output (vm, "Default behaviors:\n"
1648 "\tEnd\t-> Endpoint.\n"
1649 "\tEnd.X\t-> Endpoint with Layer-3 cross-connect.\n"
1650 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1651 "\tEnd.T\t-> Endpoint with specific IPv6 table lookup.\n"
1652 "\t\tParameters: '<fib_table>'\n"
1653 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
1654 "\t\tParameters: '<iface>'\n"
1655 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
1656 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1657 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
1658 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
1659 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
1660 "\t\tParameters: '<ip6_fib_table>'\n"
1661 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
1662 "\t\tParameters: '<ip4_fib_table>'\n");
1663 vlib_cli_output (vm, "Plugin behaviors:\n");
1664 for (i = 0; i < vec_len (plugins_vec); i++)
1666 plugin = plugins_vec[i];
1667 vlib_cli_output (vm, "\t%s\t-> %s.\n", plugin->keyword_str,
1669 vlib_cli_output (vm, "\t\tParameters: '%s'\n", plugin->params_str);
1675 VLIB_CLI_COMMAND (show_sr_localsid_behaviors_command, static) = {
1676 .path = "show sr localsids behaviors",
1677 .short_help = "show sr localsids behaviors",
1678 .function = show_sr_localsid_behaviors_command_fn,
1683 * @brief SR LocalSID initialization
1686 sr_localsids_init (vlib_main_t * vm)
1688 /* Init memory for function keys */
1689 ip6_sr_main_t *sm = &sr_main;
1690 mhash_init (&sm->sr_localsids_index_hash, sizeof (uword),
1691 sizeof (ip6_address_t));
1692 /* Init SR behaviors DPO type */
1693 sr_localsid_dpo_type = dpo_register_new_type (&sr_loc_vft, sr_loc_nodes);
1694 /* Init SR behaviors DPO type */
1695 sr_localsid_d_dpo_type =
1696 dpo_register_new_type (&sr_loc_vft, sr_loc_d_nodes);
1697 /* Init memory for localsid plugins */
1698 sm->plugin_functions_by_key = hash_create_string (0, sizeof (uword));
1702 VLIB_INIT_FUNCTION (sr_localsids_init);
1704 * fd.io coding-style-patch-verification: ON
1707 * eval: (c-set-style "gnu")