blob: 6d72a50608873b26ad8253780f6c35feca833ee2 [file] [log] [blame]
Pablo Camarillofb380952016-12-07 18:34:18 +01001/*
2 * sr_localsid.c: ipv6 segment routing Endpoint behaviors
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18/**
19 * @file
20 * @brief Processing of packets with a SRH
21 *
22 * CLI to define new Segment Routing End processing functions.
23 * Graph node to support such functions.
24 *
25 * Each function associates an SRv6 segment (IPv6 address) with an specific
26 * Segment Routing function.
27 *
28 */
29
30#include <vlib/vlib.h>
31#include <vnet/vnet.h>
32#include <vnet/sr/sr.h>
33#include <vnet/ip/ip.h>
34#include <vnet/sr/sr_packet.h>
35#include <vnet/ip/ip6_packet.h>
36#include <vnet/fib/ip6_fib.h>
37#include <vnet/dpo/dpo.h>
38#include <vnet/adj/adj.h>
39
40#include <vppinfra/error.h>
41#include <vppinfra/elog.h>
42
43/**
44 * @brief Dynamically added SR localsid DPO type
45 */
46static dpo_type_t sr_localsid_dpo_type;
47static dpo_type_t sr_localsid_d_dpo_type;
48
49/**
50 * @brief SR localsid add/del
51 *
52 * Function to add or delete SR LocalSIDs.
53 *
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.
62 *
63 * @return 0 on success, error otherwise.
64 */
65int
66sr_cli_localsid (char is_del, ip6_address_t * localsid_addr,
67 char end_psp, u8 behavior, u32 sw_if_index, u32 vlan_index,
68 u32 fib_table, ip46_address_t * nh_addr, void *ls_plugin_mem)
69{
70 ip6_sr_main_t *sm = &sr_main;
71 uword *p;
72 int rv;
73
74 ip6_sr_localsid_t *ls = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +010075
76 dpo_id_t dpo = DPO_INVALID;
77
78 /* Search for the item */
Pablo Camarillo4521afa2017-03-16 10:43:05 +010079 p = mhash_get (&sm->sr_localsids_index_hash, localsid_addr);
Pablo Camarillofb380952016-12-07 18:34:18 +010080
81 if (p)
82 {
83 if (is_del)
84 {
Pablo Camarillofb380952016-12-07 18:34:18 +010085 /* Retrieve localsid */
86 ls = pool_elt_at_index (sm->localsids, p[0]);
87 /* Delete FIB entry */
88 fib_prefix_t pfx = {
89 .fp_proto = FIB_PROTOCOL_IP6,
90 .fp_len = 128,
91 .fp_addr = {
92 .ip6 = *localsid_addr,
93 }
94 };
95
96 fib_table_entry_delete (fib_table_id_find_fib_index
97 (FIB_PROTOCOL_IP6, fib_table), &pfx,
98 FIB_SOURCE_SR);
99
100 /* In case it is a Xconnect iface remove the (OIF, NHOP) adj */
101 if (ls->behavior == SR_BEHAVIOR_X || ls->behavior == SR_BEHAVIOR_DX6
102 || ls->behavior == SR_BEHAVIOR_DX4)
103 adj_unlock (ls->nh_adj);
104
105 if (ls->behavior >= SR_BEHAVIOR_LAST)
106 {
107 sr_localsid_fn_registration_t *plugin = 0;
108 plugin = pool_elt_at_index (sm->plugin_functions,
109 ls->behavior - SR_BEHAVIOR_LAST);
110
111 /* Callback plugin removal function */
112 rv = plugin->removal (ls);
113 }
114
115 /* Delete localsid registry */
116 pool_put (sm->localsids, ls);
Pablo Camarillo4521afa2017-03-16 10:43:05 +0100117 mhash_unset (&sm->sr_localsids_index_hash, localsid_addr, NULL);
Pablo Camarillofb380952016-12-07 18:34:18 +0100118 return 1;
119 }
120 else /* create with function already existing; complain */
121 return -1;
122 }
123 else
124 /* delete; localsid does not exist; complain */
125 if (is_del)
126 return -2;
127
128 /* Check whether there exists a FIB entry with such address */
129 fib_prefix_t pfx = {
130 .fp_proto = FIB_PROTOCOL_IP6,
131 .fp_len = 128,
132 };
133
134 pfx.fp_addr.as_u64[0] = localsid_addr->as_u64[0];
135 pfx.fp_addr.as_u64[1] = localsid_addr->as_u64[1];
136
137 /* Lookup the FIB index associated to the table id provided */
138 u32 fib_index = fib_table_id_find_fib_index (FIB_PROTOCOL_IP6, fib_table);
139 if (fib_index == ~0)
140 return -3;
141
142 /* Lookup the localsid in such FIB table */
143 fib_node_index_t fei = fib_table_lookup_exact_match (fib_index, &pfx);
144 if (FIB_NODE_INDEX_INVALID != fei)
145 return -4; //There is an entry for such address (the localsid addr)
146
147 /* Create a new localsid registry */
148 pool_get (sm->localsids, ls);
149 memset (ls, 0, sizeof (*ls));
150
151 clib_memcpy (&ls->localsid, localsid_addr, sizeof (ip6_address_t));
152 ls->end_psp = end_psp;
153 ls->behavior = behavior;
154 ls->nh_adj = (u32) ~ 0;
155 ls->fib_table = fib_table;
156 switch (behavior)
157 {
158 case SR_BEHAVIOR_END:
159 break;
160 case SR_BEHAVIOR_X:
161 ls->sw_if_index = sw_if_index;
162 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
163 break;
164 case SR_BEHAVIOR_DX4:
165 ls->sw_if_index = sw_if_index;
166 clib_memcpy (&ls->next_hop.ip4, &nh_addr->ip4, sizeof (ip4_address_t));
167 break;
168 case SR_BEHAVIOR_DX6:
169 ls->sw_if_index = sw_if_index;
170 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
171 break;
172 case SR_BEHAVIOR_DT6:
173 ls->vrf_index = sw_if_index;
174 break;
175 case SR_BEHAVIOR_DX2:
176 ls->sw_if_index = sw_if_index;
177 ls->vlan_index = vlan_index;
178 break;
179 }
180
181 /* Figure out the adjacency magic for Xconnect variants */
182 if (ls->behavior == SR_BEHAVIOR_X || ls->behavior == SR_BEHAVIOR_DX4
183 || ls->behavior == SR_BEHAVIOR_DX6)
184 {
185 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
186
187 /* Retrieve the adjacency corresponding to the (OIF, next_hop) */
188 if (ls->behavior == SR_BEHAVIOR_DX6 || ls->behavior == SR_BEHAVIOR_X)
189 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
190 nh_addr, sw_if_index);
191
192 else if (ls->behavior == SR_BEHAVIOR_DX4)
193 nh_adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
194 nh_addr, sw_if_index);
195
196 /* Check for ADJ creation error. If so panic */
197 if (nh_adj_index == ADJ_INDEX_INVALID)
198 {
199 pool_put (sm->localsids, ls);
200 return -5;
201 }
202
203 ls->nh_adj = nh_adj_index;
204 }
205
206 /* Set DPO */
207 if (ls->behavior == SR_BEHAVIOR_END || ls->behavior == SR_BEHAVIOR_X)
208 dpo_set (&dpo, sr_localsid_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
209 else if (ls->behavior > SR_BEHAVIOR_D_FIRST
210 && ls->behavior < SR_BEHAVIOR_LAST)
211 dpo_set (&dpo, sr_localsid_d_dpo_type, DPO_PROTO_IP6, ls - sm->localsids);
212 else if (ls->behavior >= SR_BEHAVIOR_LAST)
213 {
214 sr_localsid_fn_registration_t *plugin = 0;
215 plugin = pool_elt_at_index (sm->plugin_functions,
216 ls->behavior - SR_BEHAVIOR_LAST);
217 /* Copy the unformat memory result */
218 ls->plugin_mem = ls_plugin_mem;
219 /* Callback plugin creation function */
220 rv = plugin->creation (ls);
221 if (rv)
222 {
223 pool_put (sm->localsids, ls);
224 return -6;
225 }
226 dpo_set (&dpo, plugin->dpo, DPO_PROTO_IP6, ls - sm->localsids);
227 }
228
229 /* Set hash key for searching localsid by address */
Pablo Camarillo4521afa2017-03-16 10:43:05 +0100230 mhash_set (&sm->sr_localsids_index_hash, localsid_addr, ls - sm->localsids,
231 NULL);
Pablo Camarillofb380952016-12-07 18:34:18 +0100232
233 fib_table_entry_special_dpo_add (fib_index, &pfx, FIB_SOURCE_SR,
234 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
235 dpo_reset (&dpo);
236
237 /* Set counter to zero */
238 vlib_validate_combined_counter (&(sm->sr_ls_valid_counters),
239 ls - sm->localsids);
240 vlib_validate_combined_counter (&(sm->sr_ls_invalid_counters),
241 ls - sm->localsids);
242
243 vlib_zero_combined_counter (&(sm->sr_ls_valid_counters),
244 ls - sm->localsids);
245 vlib_zero_combined_counter (&(sm->sr_ls_invalid_counters),
246 ls - sm->localsids);
247
248 return 0;
249}
250
251/**
252 * @brief SR LocalSID CLI function.
253 *
254 * @see sr_cli_localsid
255 */
256static clib_error_t *
257sr_cli_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
258 vlib_cli_command_t * cmd)
259{
260 vnet_main_t *vnm = vnet_get_main ();
261 ip6_sr_main_t *sm = &sr_main;
262 u32 sw_if_index = (u32) ~ 0, vlan_index = (u32) ~ 0, fib_index = 0;
263 int is_del = 0;
264 int end_psp = 0;
265 ip6_address_t resulting_address;
266 ip46_address_t next_hop;
267 char address_set = 0;
268 char behavior = 0;
269 void *ls_plugin_mem = 0;
270
271 int rv;
272
273 memset (&resulting_address, 0, sizeof (ip6_address_t));
274 ip46_address_reset (&next_hop);
275
276 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
277 {
278 if (unformat (input, "del"))
279 is_del = 1;
280 else if (!address_set
281 && unformat (input, "address %U", unformat_ip6_address,
282 &resulting_address))
283 address_set = 1;
284 else if (!address_set
285 && unformat (input, "addr %U", unformat_ip6_address,
286 &resulting_address))
287 address_set = 1;
288 else if (unformat (input, "fib-table %u", &fib_index));
289 else if (vlan_index == (u32) ~ 0
290 && unformat (input, "vlan %u", &vlan_index));
291 else if (!behavior && unformat (input, "behavior"))
292 {
293 if (unformat (input, "end.x %U %U",
294 unformat_vnet_sw_interface, vnm, &sw_if_index,
295 unformat_ip6_address, &next_hop.ip6))
296 behavior = SR_BEHAVIOR_X;
297 else if (unformat (input, "end.dx6 %U %U",
298 unformat_vnet_sw_interface, vnm, &sw_if_index,
299 unformat_ip6_address, &next_hop.ip6))
300 behavior = SR_BEHAVIOR_DX6;
301 else if (unformat (input, "end.dx4 %U %U",
302 unformat_vnet_sw_interface, vnm, &sw_if_index,
303 unformat_ip4_address, &next_hop.ip4))
304 behavior = SR_BEHAVIOR_DX4;
305 else if (unformat (input, "end.dx2 %U",
306 unformat_vnet_sw_interface, vnm, &sw_if_index))
307 behavior = SR_BEHAVIOR_DX2;
308 else if (unformat (input, "end.dt6 %u", &sw_if_index))
309 behavior = SR_BEHAVIOR_DT6;
310 else if (unformat (input, "end.dt4 %u", &sw_if_index))
311 behavior = SR_BEHAVIOR_DT4;
312 else
313 {
314 /* Loop over all the plugin behavior format functions */
315 sr_localsid_fn_registration_t *plugin = 0, **vec_plugins = 0;
316 sr_localsid_fn_registration_t **plugin_it = 0;
317
318 /* Create a vector out of the plugin pool as recommended */
319 /* *INDENT-OFF* */
320 pool_foreach (plugin, sm->plugin_functions,
321 {
322 vec_add1 (vec_plugins, plugin);
323 });
324 /* *INDENT-ON* */
325
326 vec_foreach (plugin_it, vec_plugins)
327 {
328 if (unformat
329 (input, "%U", (*plugin_it)->ls_unformat, &ls_plugin_mem))
330 {
331 behavior = (*plugin_it)->sr_localsid_function_number;
332 break;
333 }
334 }
335 }
336
337 if (!behavior)
338 {
339 if (unformat (input, "end"))
340 behavior = SR_BEHAVIOR_END;
341 else
342 break;
343 }
344 }
345 else if (!end_psp && unformat (input, "psp"))
346 end_psp = 1;
347 else
348 break;
349 }
350
351 if (!behavior && end_psp)
352 behavior = SR_BEHAVIOR_END;
353
354 if (!address_set)
355 return clib_error_return (0,
356 "Error: SRv6 LocalSID address is mandatory.");
357 if (!is_del && !behavior)
358 return clib_error_return (0,
359 "Error: SRv6 LocalSID behavior is mandatory.");
360 if (vlan_index != (u32) ~ 0)
361 return clib_error_return (0,
362 "Error: SRv6 End.DX2 with rewrite VLAN tag not supported by now.");
363 if (end_psp && !(behavior == SR_BEHAVIOR_END || behavior == SR_BEHAVIOR_X))
364 return clib_error_return (0,
365 "Error: SRv6 PSP only compatible with End and End.X");
366
367 rv = sr_cli_localsid (is_del, &resulting_address, end_psp, behavior,
368 sw_if_index, vlan_index, fib_index, &next_hop,
369 ls_plugin_mem);
370
371 switch (rv)
372 {
373 case 0:
374 break;
375 case 1:
376 return 0;
377 case -1:
378 return clib_error_return (0,
379 "Identical localsid already exists. Requested localsid not created.");
380 case -2:
381 return clib_error_return (0,
382 "The requested localsid could not be deleted. SR localsid not found");
383 case -3:
384 return clib_error_return (0, "FIB table %u does not exist", fib_index);
385 case -4:
386 return clib_error_return (0, "There is already one FIB entry for the"
387 "requested localsid non segment routing related");
388 case -5:
389 return clib_error_return (0,
390 "Could not create ARP/ND entry for such next_hop. Internal error.");
391 case -6:
392 return clib_error_return (0,
393 "Error on the plugin based localsid creation.");
394 default:
395 return clib_error_return (0, "BUG: sr localsid returns %d", rv);
396 }
397 return 0;
398}
399
400/* *INDENT-OFF* */
401VLIB_CLI_COMMAND (sr_localsid_command, static) = {
402 .path = "sr localsid",
403 .short_help = "sr localsid (del) address XX:XX::YY:YY"
404 "(fib-table 8) behavior STRING",
405 .long_help =
406 "Create SR LocalSID and binds it to a particular behavior\n"
407 "Arguments:\n"
408 "\tlocalSID IPv6_addr(128b) LocalSID IPv6 address\n"
409 "\t(fib-table X) Optional. VRF where to install SRv6 localsid\n"
410 "\tbehavior STRING Specifies the behavior\n"
411 "\n\tBehaviors:\n"
412 "\tEnd\t-> Endpoint.\n"
413 "\tEnd.X\t-> Endpoint with decapsulation and Layer-3 cross-connect.\n"
414 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
415 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
416 "\t\tParameters: '<iface>'\n"
417 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
418 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
419 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
420 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
421 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
422 "\t\tParameters: '<ip6_fib_table>'\n"
423 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
424 "\t\tParameters: '<ip4_fib_table>'\n",
425 .function = sr_cli_localsid_command_fn,
426};
427/* *INDENT-ON* */
428
429/**
430 * @brief CLI function to 'show' all SR LocalSIDs on console.
431 */
432static clib_error_t *
433show_sr_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
434 vlib_cli_command_t * cmd)
435{
436 vnet_main_t *vnm = vnet_get_main ();
437 ip6_sr_main_t *sm = &sr_main;
438 ip6_sr_localsid_t **localsid_list = 0;
439 ip6_sr_localsid_t *ls;
440 int i;
441
442 vlib_cli_output (vm, "SRv6 - My LocalSID Table:");
443 vlib_cli_output (vm, "=========================");
444 /* *INDENT-OFF* */
445 pool_foreach (ls, sm->localsids, ({ vec_add1 (localsid_list, ls); }));
446 /* *INDENT-ON* */
447 for (i = 0; i < vec_len (localsid_list); i++)
448 {
449 ls = localsid_list[i];
450 switch (ls->behavior)
451 {
452 case SR_BEHAVIOR_END:
453 vlib_cli_output (vm, "\tAddress: \t%U\n\tBehavior: \tEnd",
454 format_ip6_address, &ls->localsid);
455 break;
456 case SR_BEHAVIOR_X:
457 vlib_cli_output (vm,
458 "\tAddress: \t%U\n\tBehavior: \tX (Endpoint with Layer-3 cross-connect)"
459 "\n\tIface: \t%U\n\tNext hop: \t%U",
460 format_ip6_address, &ls->localsid,
461 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
462 format_ip6_address, &ls->next_hop.ip6);
463 break;
464 case SR_BEHAVIOR_DX4:
465 vlib_cli_output (vm,
466 "\tAddress: \t%U\n\tBehavior: \tDX4 (Endpoint with decapsulation and IPv4 cross-connect)"
467 "\n\tIface: \t%U\n\tNext hop: \t%U",
468 format_ip6_address, &ls->localsid,
469 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
470 format_ip4_address, &ls->next_hop.ip4);
471 break;
472 case SR_BEHAVIOR_DX6:
473 vlib_cli_output (vm,
474 "\tAddress: \t%U\n\tBehavior: \tDX6 (Endpoint with decapsulation and IPv6 cross-connect)"
475 "\n\tIface: \t%U\n\tNext hop: \t%U",
476 format_ip6_address, &ls->localsid,
477 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
478 format_ip6_address, &ls->next_hop.ip6);
479 break;
480 case SR_BEHAVIOR_DX2:
481 if (ls->vlan_index == (u32) ~ 0)
482 vlib_cli_output (vm,
483 "\tAddress: \t%U\n\tBehavior: \tDX2 (Endpoint with decapulation and Layer-2 cross-connect)"
484 "\n\tIface: \t%U", format_ip6_address,
485 &ls->localsid, format_vnet_sw_if_index_name, vnm,
486 ls->sw_if_index);
487 else
488 vlib_cli_output (vm,
489 "Unsupported yet. (DX2 with egress VLAN rewrite)");
490 break;
491 case SR_BEHAVIOR_DT6:
492 vlib_cli_output (vm,
493 "\tAddress: \t%U\n\tBehavior: \tDT6 (Endpoint with decapsulation and specific IPv6 table lookup)"
494 "\n\tTable: %u", format_ip6_address, &ls->localsid,
495 ls->fib_table);
496 break;
497 case SR_BEHAVIOR_DT4:
498 vlib_cli_output (vm,
499 "\tAddress: \t%U\n\tBehavior: \tDT4 (Endpoint with decapsulation and specific IPv4 table lookup)"
500 "\n\tTable: \t%u", format_ip6_address,
501 &ls->localsid, ls->fib_table);
502 break;
503 default:
504 if (ls->behavior >= SR_BEHAVIOR_LAST)
505 {
506 sr_localsid_fn_registration_t *plugin =
507 pool_elt_at_index (sm->plugin_functions,
508 ls->behavior - SR_BEHAVIOR_LAST);
509
510 vlib_cli_output (vm, "\tAddress: \t%U\n"
511 "\tBehavior: \t%s (%s)\n\t%U",
512 format_ip6_address, &ls->localsid,
513 plugin->keyword_str, plugin->def_str,
514 plugin->ls_format, ls->plugin_mem);
515 }
516 else
517 //Should never get here...
518 vlib_cli_output (vm, "Internal error");
519 break;
520 }
521 if (ls->end_psp)
522 vlib_cli_output (vm, "\tPSP: \tTrue\n");
523
524 /* Print counters */
525 vlib_counter_t valid, invalid;
526 vlib_get_combined_counter (&(sm->sr_ls_valid_counters), i, &valid);
527 vlib_get_combined_counter (&(sm->sr_ls_invalid_counters), i, &invalid);
528 vlib_cli_output (vm, "\tGood traffic: \t[%Ld packets : %Ld bytes]\n",
529 valid.packets, valid.bytes);
530 vlib_cli_output (vm, "\tBad traffic: \t[%Ld packets : %Ld bytes]\n",
531 invalid.packets, invalid.bytes);
532 vlib_cli_output (vm, "--------------------");
533 }
534 return 0;
535}
536
537/* *INDENT-OFF* */
538VLIB_CLI_COMMAND (show_sr_localsid_command, static) = {
539 .path = "show sr localsids",
540 .short_help = "show sr localsids",
541 .function = show_sr_localsid_command_fn,
542};
543/* *INDENT-ON* */
544
545/**
546 * @brief Function to 'clear' ALL SR localsid counters
547 */
548static clib_error_t *
549clear_sr_localsid_counters_command_fn (vlib_main_t * vm,
550 unformat_input_t * input,
551 vlib_cli_command_t * cmd)
552{
553 ip6_sr_main_t *sm = &sr_main;
554
555 vlib_clear_combined_counters (&(sm->sr_ls_valid_counters));
556 vlib_clear_combined_counters (&(sm->sr_ls_invalid_counters));
557
558 return 0;
559}
560
561/* *INDENT-OFF* */
562VLIB_CLI_COMMAND (clear_sr_localsid_counters_command, static) = {
563 .path = "clear sr localsid counters",
564 .short_help = "clear sr localsid counters",
565 .function = clear_sr_localsid_counters_command_fn,
566};
567/* *INDENT-ON* */
568
569/************************ SR LocalSID graphs node ****************************/
570/**
571 * @brief SR localsid node trace
572 */
573typedef struct
574{
575 u32 localsid_index;
576 ip6_address_t src, out_dst;
577 u8 sr[256];
578 u8 num_segments;
579 u8 segments_left;
580 //With SRv6 header update include flags here.
581} sr_localsid_trace_t;
582
583#define foreach_sr_localsid_error \
584_(NO_INNER_HEADER, "(SR-Error) No inner IP header") \
585_(NO_MORE_SEGMENTS, "(SR-Error) No more segments") \
586_(NO_SRH, "(SR-Error) No SR header") \
587_(NO_PSP, "(SR-Error) PSP Not available (segments left > 0)") \
588_(NOT_LS, "(SR-Error) Decaps not available (segments left > 0)") \
589_(L2, "(SR-Error) SRv6 decapsulated a L2 frame without dest")
590
591typedef enum
592{
593#define _(sym,str) SR_LOCALSID_ERROR_##sym,
594 foreach_sr_localsid_error
595#undef _
596 SR_LOCALSID_N_ERROR,
597} sr_localsid_error_t;
598
599static char *sr_localsid_error_strings[] = {
600#define _(sym,string) string,
601 foreach_sr_localsid_error
602#undef _
603};
604
605#define foreach_sr_localsid_next \
606_(ERROR, "error-drop") \
607_(IP6_LOOKUP, "ip6-lookup") \
608_(IP4_LOOKUP, "ip4-lookup") \
609_(IP6_REWRITE, "ip6-rewrite") \
610_(IP4_REWRITE, "ip4-rewrite") \
611_(INTERFACE_OUTPUT, "interface-output")
612
613typedef enum
614{
615#define _(s,n) SR_LOCALSID_NEXT_##s,
616 foreach_sr_localsid_next
617#undef _
618 SR_LOCALSID_N_NEXT,
619} sr_localsid_next_t;
620
621/**
622 * @brief SR LocalSID graph node trace function
623 *
624 * @see sr_localsid
625 */
626u8 *
627format_sr_localsid_trace (u8 * s, va_list * args)
628{
629 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
630 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
631 ip6_sr_main_t *sm = &sr_main;
632 sr_localsid_trace_t *t = va_arg (*args, sr_localsid_trace_t *);
633
634 ip6_sr_localsid_t *ls =
635 pool_elt_at_index (sm->localsids, t->localsid_index);
636
637 s =
638 format (s, "SR-LOCALSID:\n\tLocalsid: %U\n", format_ip6_address,
639 &ls->localsid);
640 switch (ls->behavior)
641 {
642 case SR_BEHAVIOR_END:
643 s = format (s, "\tBehavior: End\n");
644 break;
645 case SR_BEHAVIOR_DX6:
646 s = format (s, "\tBehavior: Decapsulation with IPv6 L3 xconnect\n");
647 break;
648 case SR_BEHAVIOR_DX4:
649 s = format (s, "\tBehavior: Decapsulation with IPv4 L3 xconnect\n");
650 break;
651 case SR_BEHAVIOR_X:
652 s = format (s, "\tBehavior: IPv6 L3 xconnect\n");
653 break;
654 case SR_BEHAVIOR_DT6:
655 s = format (s, "\tBehavior: Decapsulation with IPv6 Table lookup\n");
656 break;
657 case SR_BEHAVIOR_DT4:
658 s = format (s, "\tBehavior: Decapsulation with IPv4 Table lookup\n");
659 break;
660 case SR_BEHAVIOR_DX2:
661 s = format (s, "\tBehavior: Decapsulation with L2 xconnect\n");
662 break;
663 default:
664 s = format (s, "\tBehavior: defined in plugin\n"); //TODO
665 break;
666 }
667 if (t->num_segments != 0xFF)
668 {
669 if (t->num_segments > 0)
670 {
671 s = format (s, "\tSegments left: %d\n", t->num_segments);
672 s = format (s, "\tSID list: [in ietf order]");
673 int i = 0;
674 for (i = 0; i < t->num_segments; i++)
675 {
676 s = format (s, "\n\t-> %U", format_ip6_address,
677 (ip6_address_t *) & t->sr[i *
678 sizeof (ip6_address_t)]);
679 }
680 }
681 }
682 return s;
683}
684
685/**
686 * @brief Function doing End processing.
687 */
688static_always_inline void
689end_srh_processing (vlib_node_runtime_t * node,
690 vlib_buffer_t * b0,
691 ip6_header_t * ip0,
692 ip6_sr_header_t * sr0,
693 ip6_sr_localsid_t * ls0, u32 * next0)
694{
695 ip6_address_t *new_dst0;
696
697 if (PREDICT_TRUE (sr0->type == ROUTING_HEADER_TYPE_SR))
698 {
699 if (PREDICT_TRUE (sr0->segments_left != 0))
700 {
701 sr0->segments_left -= 1;
702 new_dst0 = (ip6_address_t *) (sr0->segments);
703 new_dst0 += sr0->segments_left;
704 ip0->dst_address.as_u64[0] = new_dst0->as_u64[0];
705 ip0->dst_address.as_u64[1] = new_dst0->as_u64[1];
706
707 if (ls0->behavior == SR_BEHAVIOR_X)
708 {
709 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
710 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
711 }
712 }
713 else
714 {
715 *next0 = SR_LOCALSID_NEXT_ERROR;
716 b0->error = node->errors[SR_LOCALSID_ERROR_NO_MORE_SEGMENTS];
717 }
718 }
719 else
720 {
721 /* Error. Routing header of type != SR */
722 *next0 = SR_LOCALSID_NEXT_ERROR;
723 b0->error = node->errors[SR_LOCALSID_ERROR_NO_SRH];
724 }
725}
726
727/*
728 * @brief Function doing SRH processing for D* variants
729 */
730//FixME. I must crosscheck that next_proto matches the localsid
731static_always_inline void
732end_decaps_srh_processing (vlib_node_runtime_t * node,
733 vlib_buffer_t * b0,
734 ip6_header_t * ip0,
735 ip6_sr_header_t * sr0,
736 ip6_sr_localsid_t * ls0, u32 * next0)
737{
738 /* Compute the size of the IPv6 header with all Ext. headers */
739 u8 next_proto;
740 ip6_ext_header_t *next_ext_header;
741 u16 total_size = 0;
742
743 next_proto = ip0->protocol;
744 next_ext_header = (void *) (ip0 + 1);
745 total_size = sizeof (ip6_header_t);
746 while (ip6_ext_hdr (next_proto))
747 {
748 total_size += ip6_ext_header_len (next_ext_header);
749 next_proto = next_ext_header->next_hdr;
750 next_ext_header = ip6_ext_next_header (next_ext_header);
751 }
752
753 /* Ensure this is the last segment. Otherwise drop. */
754 if (sr0 && sr0->segments_left != 0)
755 {
756 *next0 = SR_LOCALSID_NEXT_ERROR;
757 b0->error = node->errors[SR_LOCALSID_ERROR_NOT_LS];
758 return;
759 }
760
761 switch (next_proto)
762 {
763 case IP_PROTOCOL_IPV6:
764 /* Encap-End IPv6. Pop outer IPv6 header. */
765 if (ls0->behavior == SR_BEHAVIOR_DX6)
766 {
767 vlib_buffer_advance (b0, total_size);
768 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
769 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
770 return;
771 }
772 else if (ls0->behavior == SR_BEHAVIOR_DT6)
773 {
774 vlib_buffer_advance (b0, total_size);
775 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->fib_table;
776 return;
777 }
778 break;
779 case IP_PROTOCOL_IP_IN_IP:
780 /* Encap-End IPv4. Pop outer IPv6 header */
781 if (ls0->behavior == SR_BEHAVIOR_DX4)
782 {
783 vlib_buffer_advance (b0, total_size);
784 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
785 *next0 = SR_LOCALSID_NEXT_IP4_REWRITE;
786 return;
787 }
788 else if (ls0->behavior == SR_BEHAVIOR_DT4)
789 {
790 vlib_buffer_advance (b0, total_size);
791 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->fib_table;
792 *next0 = SR_LOCALSID_NEXT_IP4_LOOKUP;
793 return;
794 }
795 break;
796 case IP_PROTOCOL_IP6_NONXT:
797 /* L2 encaps */
798 if (ls0->behavior == SR_BEHAVIOR_DX2)
799 {
800 vlib_buffer_advance (b0, total_size);
801 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->sw_if_index;
802 *next0 = SR_LOCALSID_NEXT_INTERFACE_OUTPUT;
803 return;
804 }
805 break;
806 }
807 *next0 = SR_LOCALSID_NEXT_ERROR;
808 b0->error = node->errors[SR_LOCALSID_ERROR_NO_INNER_HEADER];
809 return;
810}
811
812/**
813 * @brief Function doing End processing with PSP
814 */
815static_always_inline void
816end_psp_srh_processing (vlib_node_runtime_t * node,
817 vlib_buffer_t * b0,
818 ip6_header_t * ip0,
819 ip6_ext_header_t * prev0,
820 ip6_sr_header_t * sr0,
821 ip6_sr_localsid_t * ls0, u32 * next0)
822{
823 u32 new_l0, sr_len;
shwethabe146f132017-03-09 16:58:26 +0000824 u64 *copy_dst0, *copy_src0;
825 u32 copy_len_u64s0 = 0;
826 int i;
Pablo Camarillofb380952016-12-07 18:34:18 +0100827
828 if (PREDICT_TRUE (sr0->type == ROUTING_HEADER_TYPE_SR))
829 {
830 if (PREDICT_TRUE (sr0->segments_left == 1))
831 {
832 ip0->dst_address.as_u64[0] = sr0->segments->as_u64[0];
833 ip0->dst_address.as_u64[1] = sr0->segments->as_u64[1];
834
835 /* Remove the SRH taking care of the rest of IPv6 ext header */
836 if (prev0)
837 prev0->next_hdr = sr0->protocol;
838 else
839 ip0->protocol = sr0->protocol;
840
841 sr_len = ip6_ext_header_len (sr0);
842 vlib_buffer_advance (b0, sr_len);
843 new_l0 = clib_net_to_host_u16 (ip0->payload_length) - sr_len;
844 ip0->payload_length = clib_host_to_net_u16 (new_l0);
shwethabe146f132017-03-09 16:58:26 +0000845 copy_src0 = (u64 *) ip0;
846 copy_dst0 = copy_src0 + (sr0->length + 1);
847 /* number of 8 octet units to copy
848 * By default in absence of extension headers it is equal to length of ip6 header
849 * With extension headers it number of 8 octet units of ext headers preceding
850 * SR header
851 */
852 copy_len_u64s0 =
853 (((u8 *) sr0 - (u8 *) ip0) - sizeof (ip6_header_t)) >> 3;
854 copy_dst0[4 + copy_len_u64s0] = copy_src0[4 + copy_len_u64s0];
855 copy_dst0[3 + copy_len_u64s0] = copy_src0[3 + copy_len_u64s0];
856 copy_dst0[2 + copy_len_u64s0] = copy_src0[2 + copy_len_u64s0];
857 copy_dst0[1 + copy_len_u64s0] = copy_src0[1 + copy_len_u64s0];
858 copy_dst0[0 + copy_len_u64s0] = copy_src0[0 + copy_len_u64s0];
859
860 for (i = copy_len_u64s0 - 1; i >= 0; i--)
861 {
862 copy_dst0[i] = copy_src0[i];
863 }
Pablo Camarillofb380952016-12-07 18:34:18 +0100864
865 if (ls0->behavior == SR_BEHAVIOR_X)
866 {
867 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
868 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
869 }
870 return;
871 }
872 }
873 /* Error. Routing header of type != SR */
874 *next0 = SR_LOCALSID_NEXT_ERROR;
875 b0->error = node->errors[SR_LOCALSID_ERROR_NO_PSP];
876}
877
878/**
879 * @brief SR LocalSID graph node. Supports all default SR Endpoint variants
880 */
881static uword
882sr_localsid_d_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
883 vlib_frame_t * from_frame)
884{
885 u32 n_left_from, next_index, *from, *to_next;
886 ip6_sr_main_t *sm = &sr_main;
887 from = vlib_frame_vector_args (from_frame);
888 n_left_from = from_frame->n_vectors;
889 next_index = node->cached_next_index;
Damjan Marion586afd72017-04-05 19:18:20 +0200890 u32 thread_index = vlib_get_thread_index ();
Pablo Camarillofb380952016-12-07 18:34:18 +0100891
892 while (n_left_from > 0)
893 {
894 u32 n_left_to_next;
895 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
896
897 /* Quad - Loop */
898 while (n_left_from >= 8 && n_left_to_next >= 4)
899 {
900 u32 bi0, bi1, bi2, bi3;
901 vlib_buffer_t *b0, *b1, *b2, *b3;
902 ip6_header_t *ip0, *ip1, *ip2, *ip3;
903 ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
904 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
905 u32 next0, next1, next2, next3;
906 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
907 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
908
909 /* Prefetch next iteration. */
910 {
911 vlib_buffer_t *p4, *p5, *p6, *p7;
912
913 p4 = vlib_get_buffer (vm, from[4]);
914 p5 = vlib_get_buffer (vm, from[5]);
915 p6 = vlib_get_buffer (vm, from[6]);
916 p7 = vlib_get_buffer (vm, from[7]);
917
918 /* Prefetch the buffer header and packet for the N+4 loop iteration */
919 vlib_prefetch_buffer_header (p4, LOAD);
920 vlib_prefetch_buffer_header (p5, LOAD);
921 vlib_prefetch_buffer_header (p6, LOAD);
922 vlib_prefetch_buffer_header (p7, LOAD);
923
924 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
925 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
926 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
927 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
928 }
929
930 to_next[0] = bi0 = from[0];
931 to_next[1] = bi1 = from[1];
932 to_next[2] = bi2 = from[2];
933 to_next[3] = bi3 = from[3];
934 from += 4;
935 to_next += 4;
936 n_left_from -= 4;
937 n_left_to_next -= 4;
938
939 b0 = vlib_get_buffer (vm, bi0);
940 b1 = vlib_get_buffer (vm, bi1);
941 b2 = vlib_get_buffer (vm, bi2);
942 b3 = vlib_get_buffer (vm, bi3);
943
944 ls0 =
945 pool_elt_at_index (sm->localsids,
946 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
947 ls1 =
948 pool_elt_at_index (sm->localsids,
949 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
950 ls2 =
951 pool_elt_at_index (sm->localsids,
952 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
953 ls3 =
954 pool_elt_at_index (sm->localsids,
955 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
956
957 ip0 = vlib_buffer_get_current (b0);
958 ip1 = vlib_buffer_get_current (b1);
959 ip2 = vlib_buffer_get_current (b2);
960 ip3 = vlib_buffer_get_current (b3);
961
962 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
963 ip6_ext_header_find_t (ip1, prev1, sr1, IP_PROTOCOL_IPV6_ROUTE);
964 ip6_ext_header_find_t (ip2, prev2, sr2, IP_PROTOCOL_IPV6_ROUTE);
965 ip6_ext_header_find_t (ip3, prev3, sr3, IP_PROTOCOL_IPV6_ROUTE);
966
967 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
968 end_decaps_srh_processing (node, b1, ip1, sr1, ls1, &next1);
969 end_decaps_srh_processing (node, b2, ip2, sr2, ls2, &next2);
970 end_decaps_srh_processing (node, b3, ip3, sr3, ls3, &next3);
971
972 //TODO: trace.
973
974 vlib_increment_combined_counter
975 (((next0 ==
976 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +0200977 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
978 1, vlib_buffer_length_in_chain (vm, b0));
Pablo Camarillofb380952016-12-07 18:34:18 +0100979
980 vlib_increment_combined_counter
981 (((next1 ==
982 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +0200983 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
984 1, vlib_buffer_length_in_chain (vm, b1));
Pablo Camarillofb380952016-12-07 18:34:18 +0100985
986 vlib_increment_combined_counter
987 (((next2 ==
988 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +0200989 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
990 1, vlib_buffer_length_in_chain (vm, b2));
Pablo Camarillofb380952016-12-07 18:34:18 +0100991
992 vlib_increment_combined_counter
993 (((next3 ==
994 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +0200995 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
996 1, vlib_buffer_length_in_chain (vm, b3));
Pablo Camarillofb380952016-12-07 18:34:18 +0100997
998 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
999 n_left_to_next, bi0, bi1, bi2, bi3,
1000 next0, next1, next2, next3);
1001 }
1002
1003 /* Single loop for potentially the last three packets */
1004 while (n_left_from > 0 && n_left_to_next > 0)
1005 {
1006 u32 bi0;
1007 vlib_buffer_t *b0;
1008 ip6_header_t *ip0;
1009 ip6_ext_header_t *prev0;
1010 ip6_sr_header_t *sr0;
1011 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1012 ip6_sr_localsid_t *ls0;
1013
1014 bi0 = from[0];
1015 to_next[0] = bi0;
1016 from += 1;
1017 to_next += 1;
1018 n_left_from -= 1;
1019 n_left_to_next -= 1;
1020
1021 b0 = vlib_get_buffer (vm, bi0);
1022 ip0 = vlib_buffer_get_current (b0);
1023
1024 /* Lookup the SR End behavior based on IP DA (adj) */
1025 ls0 =
1026 pool_elt_at_index (sm->localsids,
1027 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1028
1029 /* Find SRH as well as previous header */
1030 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1031
1032 /* SRH processing and End variants */
1033 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1034
1035 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1036 {
1037 sr_localsid_trace_t *tr =
1038 vlib_add_trace (vm, node, b0, sizeof (*tr));
1039 tr->num_segments = 0;
1040 tr->localsid_index = ls0 - sm->localsids;
1041
1042 if (ip0 == vlib_buffer_get_current (b0))
1043 {
1044 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
1045 sizeof (tr->src.as_u8));
1046 clib_memcpy (tr->out_dst.as_u8, ip0->dst_address.as_u8,
1047 sizeof (tr->out_dst.as_u8));
1048 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1049 && sr0->type == ROUTING_HEADER_TYPE_SR)
1050 {
1051 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1052 tr->num_segments =
1053 sr0->length * 8 / sizeof (ip6_address_t);
1054 tr->segments_left = sr0->segments_left;
1055 }
1056 }
1057 else
1058 tr->num_segments = 0xFF;
1059 }
1060
1061 /* Increase the counters */
1062 vlib_increment_combined_counter
1063 (((next0 ==
1064 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001065 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1066 1, vlib_buffer_length_in_chain (vm, b0));
Pablo Camarillofb380952016-12-07 18:34:18 +01001067
1068 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1069 n_left_to_next, bi0, next0);
1070 }
1071 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1072 }
1073 return from_frame->n_vectors;
1074}
1075
1076/* *INDENT-OFF* */
1077VLIB_REGISTER_NODE (sr_localsid_d_node) = {
1078 .function = sr_localsid_d_fn,
1079 .name = "sr-localsid-d",
1080 .vector_size = sizeof (u32),
1081 .format_trace = format_sr_localsid_trace,
1082 .type = VLIB_NODE_TYPE_INTERNAL,
1083 .n_errors = SR_LOCALSID_N_ERROR,
1084 .error_strings = sr_localsid_error_strings,
1085 .n_next_nodes = SR_LOCALSID_N_NEXT,
1086 .next_nodes = {
1087#define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1088 foreach_sr_localsid_next
1089#undef _
1090 },
1091};
1092/* *INDENT-ON* */
1093
1094/**
1095 * @brief SR LocalSID graph node. Supports all default SR Endpoint variants
1096 */
1097static uword
1098sr_localsid_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1099 vlib_frame_t * from_frame)
1100{
1101 u32 n_left_from, next_index, *from, *to_next;
1102 ip6_sr_main_t *sm = &sr_main;
1103 from = vlib_frame_vector_args (from_frame);
1104 n_left_from = from_frame->n_vectors;
1105 next_index = node->cached_next_index;
Damjan Marion586afd72017-04-05 19:18:20 +02001106 u32 thread_index = vlib_get_thread_index ();
Pablo Camarillofb380952016-12-07 18:34:18 +01001107
1108 while (n_left_from > 0)
1109 {
1110 u32 n_left_to_next;
1111 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1112
1113 /* Quad - Loop */
1114 while (n_left_from >= 8 && n_left_to_next >= 4)
1115 {
1116 u32 bi0, bi1, bi2, bi3;
1117 vlib_buffer_t *b0, *b1, *b2, *b3;
1118 ip6_header_t *ip0, *ip1, *ip2, *ip3;
1119 ip6_sr_header_t *sr0, *sr1, *sr2, *sr3;
1120 ip6_ext_header_t *prev0, *prev1, *prev2, *prev3;
1121 u32 next0, next1, next2, next3;
1122 next0 = next1 = next2 = next3 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1123 ip6_sr_localsid_t *ls0, *ls1, *ls2, *ls3;
1124
1125 /* Prefetch next iteration. */
1126 {
1127 vlib_buffer_t *p4, *p5, *p6, *p7;
1128
1129 p4 = vlib_get_buffer (vm, from[4]);
1130 p5 = vlib_get_buffer (vm, from[5]);
1131 p6 = vlib_get_buffer (vm, from[6]);
1132 p7 = vlib_get_buffer (vm, from[7]);
1133
1134 /* Prefetch the buffer header and packet for the N+2 loop iteration */
1135 vlib_prefetch_buffer_header (p4, LOAD);
1136 vlib_prefetch_buffer_header (p5, LOAD);
1137 vlib_prefetch_buffer_header (p6, LOAD);
1138 vlib_prefetch_buffer_header (p7, LOAD);
1139
1140 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
1141 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
1142 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
1143 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
1144 }
1145
1146 to_next[0] = bi0 = from[0];
1147 to_next[1] = bi1 = from[1];
1148 to_next[2] = bi2 = from[2];
1149 to_next[3] = bi3 = from[3];
1150 from += 4;
1151 to_next += 4;
1152 n_left_from -= 4;
1153 n_left_to_next -= 4;
1154
1155 b0 = vlib_get_buffer (vm, bi0);
1156 b1 = vlib_get_buffer (vm, bi1);
1157 b2 = vlib_get_buffer (vm, bi2);
1158 b3 = vlib_get_buffer (vm, bi3);
1159
1160 ip0 = vlib_buffer_get_current (b0);
1161 ip1 = vlib_buffer_get_current (b1);
1162 ip2 = vlib_buffer_get_current (b2);
1163 ip3 = vlib_buffer_get_current (b3);
1164
1165 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1166 ip6_ext_header_find_t (ip1, prev1, sr1, IP_PROTOCOL_IPV6_ROUTE);
1167 ip6_ext_header_find_t (ip2, prev2, sr2, IP_PROTOCOL_IPV6_ROUTE);
1168 ip6_ext_header_find_t (ip3, prev3, sr3, IP_PROTOCOL_IPV6_ROUTE);
1169
1170 ls0 =
1171 pool_elt_at_index (sm->localsids,
1172 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1173 ls1 =
1174 pool_elt_at_index (sm->localsids,
1175 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1176 ls2 =
1177 pool_elt_at_index (sm->localsids,
1178 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1179 ls3 =
1180 pool_elt_at_index (sm->localsids,
1181 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1182
1183 if (ls0->end_psp)
1184 end_psp_srh_processing (node, b0, ip0, prev0, sr0, ls0, &next0);
1185 else
1186 end_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1187
1188 if (ls1->end_psp)
1189 end_psp_srh_processing (node, b1, ip1, prev1, sr1, ls1, &next1);
1190 else
1191 end_srh_processing (node, b1, ip1, sr1, ls1, &next1);
1192
1193 if (ls2->end_psp)
1194 end_psp_srh_processing (node, b2, ip2, prev2, sr2, ls2, &next2);
1195 else
1196 end_srh_processing (node, b2, ip2, sr2, ls2, &next2);
1197
1198 if (ls3->end_psp)
1199 end_psp_srh_processing (node, b3, ip3, prev3, sr3, ls3, &next3);
1200 else
1201 end_srh_processing (node, b3, ip3, sr3, ls3, &next3);
1202
1203 //TODO: proper trace.
1204
1205 vlib_increment_combined_counter
1206 (((next0 ==
1207 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001208 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1209 1, vlib_buffer_length_in_chain (vm, b0));
Pablo Camarillofb380952016-12-07 18:34:18 +01001210
1211 vlib_increment_combined_counter
1212 (((next1 ==
1213 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001214 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1215 1, vlib_buffer_length_in_chain (vm, b1));
Pablo Camarillofb380952016-12-07 18:34:18 +01001216
1217 vlib_increment_combined_counter
1218 (((next2 ==
1219 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001220 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1221 1, vlib_buffer_length_in_chain (vm, b2));
Pablo Camarillofb380952016-12-07 18:34:18 +01001222
1223 vlib_increment_combined_counter
1224 (((next3 ==
1225 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001226 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1227 1, vlib_buffer_length_in_chain (vm, b3));
Pablo Camarillofb380952016-12-07 18:34:18 +01001228
1229 vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
1230 n_left_to_next, bi0, bi1, bi2, bi3,
1231 next0, next1, next2, next3);
1232 }
1233
1234 /* Single loop for potentially the last three packets */
1235 while (n_left_from > 0 && n_left_to_next > 0)
1236 {
1237 u32 bi0;
1238 vlib_buffer_t *b0;
1239 ip6_header_t *ip0 = 0;
1240 ip6_ext_header_t *prev0;
1241 ip6_sr_header_t *sr0;
1242 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1243 ip6_sr_localsid_t *ls0;
1244
1245 bi0 = from[0];
1246 to_next[0] = bi0;
1247 from += 1;
1248 to_next += 1;
1249 n_left_from -= 1;
1250 n_left_to_next -= 1;
1251
1252 b0 = vlib_get_buffer (vm, bi0);
1253 ip0 = vlib_buffer_get_current (b0);
1254 ip6_ext_header_find_t (ip0, prev0, sr0, IP_PROTOCOL_IPV6_ROUTE);
1255
1256 /* Lookup the SR End behavior based on IP DA (adj) */
1257 ls0 =
1258 pool_elt_at_index (sm->localsids,
1259 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1260
1261 /* SRH processing */
1262 if (ls0->end_psp)
1263 end_psp_srh_processing (node, b0, ip0, prev0, sr0, ls0, &next0);
1264 else
1265 end_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1266
1267 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1268 {
1269 sr_localsid_trace_t *tr =
1270 vlib_add_trace (vm, node, b0, sizeof (*tr));
1271 tr->num_segments = 0;
1272 tr->localsid_index = ls0 - sm->localsids;
1273
1274 if (ip0 == vlib_buffer_get_current (b0))
1275 {
1276 clib_memcpy (tr->src.as_u8, ip0->src_address.as_u8,
1277 sizeof (tr->src.as_u8));
1278 clib_memcpy (tr->out_dst.as_u8, ip0->dst_address.as_u8,
1279 sizeof (tr->out_dst.as_u8));
1280 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1281 && sr0->type == ROUTING_HEADER_TYPE_SR)
1282 {
1283 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1284 tr->num_segments =
1285 sr0->length * 8 / sizeof (ip6_address_t);
1286 tr->segments_left = sr0->segments_left;
1287 }
1288 }
1289 else
1290 {
1291 tr->num_segments = 0xFF;
1292 }
1293 }
1294
1295 vlib_increment_combined_counter
1296 (((next0 ==
1297 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001298 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1299 1, vlib_buffer_length_in_chain (vm, b0));
Pablo Camarillofb380952016-12-07 18:34:18 +01001300
1301 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1302 n_left_to_next, bi0, next0);
1303 }
1304 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1305 }
1306 return from_frame->n_vectors;
1307}
1308
1309/* *INDENT-OFF* */
1310VLIB_REGISTER_NODE (sr_localsid_node) = {
1311 .function = sr_localsid_fn,
1312 .name = "sr-localsid",
1313 .vector_size = sizeof (u32),
1314 .format_trace = format_sr_localsid_trace,
1315 .type = VLIB_NODE_TYPE_INTERNAL,
1316 .n_errors = SR_LOCALSID_N_ERROR,
1317 .error_strings = sr_localsid_error_strings,
1318 .n_next_nodes = SR_LOCALSID_N_NEXT,
1319 .next_nodes = {
1320#define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1321 foreach_sr_localsid_next
1322#undef _
1323 },
1324};
1325/* *INDENT-ON* */
1326
1327static u8 *
1328format_sr_dpo (u8 * s, va_list * args)
1329{
1330 index_t index = va_arg (*args, index_t);
1331 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
1332
1333 return (format (s, "SR: localsid_index:[%d]", index));
1334}
1335
1336const static dpo_vft_t sr_loc_vft = {
1337 .dv_lock = sr_dpo_lock,
1338 .dv_unlock = sr_dpo_unlock,
1339 .dv_format = format_sr_dpo,
1340};
1341
1342const static char *const sr_loc_ip6_nodes[] = {
1343 "sr-localsid",
1344 NULL,
1345};
1346
1347const static char *const *const sr_loc_nodes[DPO_PROTO_NUM] = {
1348 [DPO_PROTO_IP6] = sr_loc_ip6_nodes,
1349};
1350
1351const static char *const sr_loc_d_ip6_nodes[] = {
1352 "sr-localsid-d",
1353 NULL,
1354};
1355
1356const static char *const *const sr_loc_d_nodes[DPO_PROTO_NUM] = {
1357 [DPO_PROTO_IP6] = sr_loc_d_ip6_nodes,
1358};
1359
1360
1361/*************************** SR LocalSID plugins ******************************/
1362/**
1363 * @brief SR LocalSID plugin registry
1364 */
1365int
1366sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name,
1367 u8 * keyword_str, u8 * def_str,
1368 u8 * params_str, dpo_type_t * dpo,
1369 format_function_t * ls_format,
1370 unformat_function_t * ls_unformat,
1371 sr_plugin_callback_t * creation_fn,
1372 sr_plugin_callback_t * removal_fn)
1373{
1374 ip6_sr_main_t *sm = &sr_main;
1375 uword *p;
1376
1377 sr_localsid_fn_registration_t *plugin;
1378
1379 /* Did this function exist? If so update it */
1380 p = hash_get_mem (sm->plugin_functions_by_key, fn_name);
1381 if (p)
1382 {
1383 plugin = pool_elt_at_index (sm->plugin_functions, p[0]);
1384 }
1385 /* Else create a new one and set hash key */
1386 else
1387 {
1388 pool_get (sm->plugin_functions, plugin);
1389 hash_set_mem (sm->plugin_functions_by_key, fn_name,
1390 plugin - sm->plugin_functions);
1391 }
1392
1393 memset (plugin, 0, sizeof (*plugin));
1394
1395 plugin->sr_localsid_function_number = (plugin - sm->plugin_functions);
1396 plugin->sr_localsid_function_number += SR_BEHAVIOR_LAST;
1397 plugin->ls_format = ls_format;
1398 plugin->ls_unformat = ls_unformat;
1399 plugin->creation = creation_fn;
1400 plugin->removal = removal_fn;
1401 clib_memcpy (&plugin->dpo, dpo, sizeof (dpo_type_t));
1402 plugin->function_name = format (0, "%s%c", fn_name, 0);
1403 plugin->keyword_str = format (0, "%s%c", keyword_str, 0);
1404 plugin->def_str = format (0, "%s%c", def_str, 0);
1405 plugin->params_str = format (0, "%s%c", params_str, 0);
1406
1407 return plugin->sr_localsid_function_number;
1408}
1409
1410/**
1411 * @brief CLI function to 'show' all available SR LocalSID behaviors
1412 */
1413static clib_error_t *
1414show_sr_localsid_behaviors_command_fn (vlib_main_t * vm,
1415 unformat_input_t * input,
1416 vlib_cli_command_t * cmd)
1417{
1418 ip6_sr_main_t *sm = &sr_main;
1419 sr_localsid_fn_registration_t *plugin;
1420 sr_localsid_fn_registration_t **plugins_vec = 0;
1421 int i;
1422
1423 vlib_cli_output (vm,
1424 "SR LocalSIDs behaviors:\n-----------------------\n\n");
1425
1426 /* *INDENT-OFF* */
1427 pool_foreach (plugin, sm->plugin_functions,
1428 ({ vec_add1 (plugins_vec, plugin); }));
1429 /* *INDENT-ON* */
1430
1431 /* Print static behaviors */
1432 vlib_cli_output (vm, "Default behaviors:\n"
1433 "\tEnd\t-> Endpoint.\n"
1434 "\tEnd.X\t-> Endpoint with decapsulation and Layer-3 cross-connect.\n"
1435 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1436 "\tEnd.DX2\t-> Endpoint with decapsulation and Layer-2 cross-connect.\n"
1437 "\t\tParameters: '<iface>'\n"
1438 "\tEnd.DX6\t-> Endpoint with decapsulation and IPv6 cross-connect.\n"
1439 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
1440 "\tEnd.DX4\t-> Endpoint with decapsulation and IPv4 cross-connect.\n"
1441 "\t\tParameters: '<iface> <ip4_next_hop>'\n"
1442 "\tEnd.DT6\t-> Endpoint with decapsulation and specific IPv6 table lookup.\n"
1443 "\t\tParameters: '<ip6_fib_table>'\n"
1444 "\tEnd.DT4\t-> Endpoint with decapsulation and specific IPv4 table lookup.\n"
1445 "\t\tParameters: '<ip4_fib_table>'\n");
1446 vlib_cli_output (vm, "Plugin behaviors:\n");
1447 for (i = 0; i < vec_len (plugins_vec); i++)
1448 {
1449 plugin = plugins_vec[i];
1450 vlib_cli_output (vm, "\t%s\t-> %s.\n", plugin->keyword_str,
1451 plugin->def_str);
1452 vlib_cli_output (vm, "\t\tParameters: '%s'\n", plugin->params_str);
1453 }
1454 return 0;
1455}
1456
1457/* *INDENT-OFF* */
1458VLIB_CLI_COMMAND (show_sr_localsid_behaviors_command, static) = {
1459 .path = "show sr localsids behaviors",
1460 .short_help = "show sr localsids behaviors",
1461 .function = show_sr_localsid_behaviors_command_fn,
1462};
1463/* *INDENT-ON* */
1464
1465/**
1466 * @brief SR LocalSID initialization
1467 */
1468clib_error_t *
1469sr_localsids_init (vlib_main_t * vm)
1470{
1471 /* Init memory for function keys */
1472 ip6_sr_main_t *sm = &sr_main;
Pablo Camarillo4521afa2017-03-16 10:43:05 +01001473 mhash_init (&sm->sr_localsids_index_hash, sizeof (uword),
1474 sizeof (ip6_address_t));
Pablo Camarillofb380952016-12-07 18:34:18 +01001475 /* Init SR behaviors DPO type */
1476 sr_localsid_dpo_type = dpo_register_new_type (&sr_loc_vft, sr_loc_nodes);
1477 /* Init SR behaviors DPO type */
1478 sr_localsid_d_dpo_type =
1479 dpo_register_new_type (&sr_loc_vft, sr_loc_d_nodes);
1480 /* Init memory for localsid plugins */
1481 sm->plugin_functions_by_key = hash_create_string (0, sizeof (uword));
1482 return 0;
1483}
1484
1485VLIB_INIT_FUNCTION (sr_localsids_init);
1486/*
1487* fd.io coding-style-patch-verification: ON
1488*
1489* Local Variables:
1490* eval: (c-set-style "gnu")
1491* End:
1492*/