blob: 6af6b7497bd6d99c46f85113634ee2b89ca971c9 [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>
Pablo Camarillo5d73eec2017-04-24 17:51:56 +020032#include <vnet/srv6/sr.h>
Pablo Camarillofb380952016-12-07 18:34:18 +010033#include <vnet/ip/ip.h>
Pablo Camarillo5d73eec2017-04-24 17:51:56 +020034#include <vnet/srv6/sr_packet.h>
Pablo Camarillofb380952016-12-07 18:34:18 +010035#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
Pablo Camarillo79bfd272019-12-18 17:13:13 +000066sr_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)
Pablo Camarillofb380952016-12-07 18:34:18 +010070{
71 ip6_sr_main_t *sm = &sr_main;
72 uword *p;
73 int rv;
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -080074 u8 pref_length = 128;
75 sr_localsid_fn_registration_t *plugin = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +010076
77 ip6_sr_localsid_t *ls = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +010078
79 dpo_id_t dpo = DPO_INVALID;
80
81 /* Search for the item */
Pablo Camarillo4521afa2017-03-16 10:43:05 +010082 p = mhash_get (&sm->sr_localsids_index_hash, localsid_addr);
Pablo Camarillofb380952016-12-07 18:34:18 +010083
84 if (p)
85 {
86 if (is_del)
87 {
Pablo Camarillofb380952016-12-07 18:34:18 +010088 /* Retrieve localsid */
89 ls = pool_elt_at_index (sm->localsids, p[0]);
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -080090 if (ls->behavior >= SR_BEHAVIOR_LAST)
91 {
92 plugin = pool_elt_at_index (sm->plugin_functions,
93 ls->behavior - SR_BEHAVIOR_LAST);
94 pref_length = plugin->prefix_length;
95 }
96
Pablo Camarillo79bfd272019-12-18 17:13:13 +000097 if (localsid_prefix_len != 0)
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -080098 {
Pablo Camarillo79bfd272019-12-18 17:13:13 +000099 pref_length = localsid_prefix_len;
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -0800100 }
101
Pablo Camarillofb380952016-12-07 18:34:18 +0100102 /* Delete FIB entry */
103 fib_prefix_t pfx = {
104 .fp_proto = FIB_PROTOCOL_IP6,
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -0800105 .fp_len = pref_length,
Pablo Camarillofb380952016-12-07 18:34:18 +0100106 .fp_addr = {
107 .ip6 = *localsid_addr,
108 }
109 };
110
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -0800111 fib_table_entry_delete (fib_table_find
112 (FIB_PROTOCOL_IP6, fib_table), &pfx,
113 FIB_SOURCE_SR);
Pablo Camarillofb380952016-12-07 18:34:18 +0100114
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);
119
120 if (ls->behavior >= SR_BEHAVIOR_LAST)
121 {
Pablo Camarillofb380952016-12-07 18:34:18 +0100122 /* Callback plugin removal function */
123 rv = plugin->removal (ls);
124 }
125
126 /* Delete localsid registry */
127 pool_put (sm->localsids, ls);
Pablo Camarillo4521afa2017-03-16 10:43:05 +0100128 mhash_unset (&sm->sr_localsids_index_hash, localsid_addr, NULL);
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200129 return 0;
Pablo Camarillofb380952016-12-07 18:34:18 +0100130 }
131 else /* create with function already existing; complain */
132 return -1;
133 }
134 else
135 /* delete; localsid does not exist; complain */
136 if (is_del)
137 return -2;
138
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -0800139 if (behavior >= SR_BEHAVIOR_LAST)
140 {
141 sr_localsid_fn_registration_t *plugin = 0;
142 plugin =
143 pool_elt_at_index (sm->plugin_functions, behavior - SR_BEHAVIOR_LAST);
144 pref_length = plugin->prefix_length;
145 }
146
Tetsuya Murakami0d90ed92020-03-21 14:47:02 -0700147 if (localsid_prefix_len != 0)
148 {
149 pref_length = localsid_prefix_len;
150 }
151
Pablo Camarillofb380952016-12-07 18:34:18 +0100152 /* Check whether there exists a FIB entry with such address */
153 fib_prefix_t pfx = {
154 .fp_proto = FIB_PROTOCOL_IP6,
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -0800155 .fp_len = pref_length,
Pablo Camarillofb380952016-12-07 18:34:18 +0100156 };
157
158 pfx.fp_addr.as_u64[0] = localsid_addr->as_u64[0];
159 pfx.fp_addr.as_u64[1] = localsid_addr->as_u64[1];
Tetsuya Murakami0d90ed92020-03-21 14:47:02 -0700160 pfx.fp_len = pref_length;
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -0800161
Pablo Camarillofb380952016-12-07 18:34:18 +0100162 /* Lookup the FIB index associated to the table id provided */
Neale Ranns107e7d42017-04-11 09:55:19 -0700163 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP6, fib_table);
Pablo Camarillofb380952016-12-07 18:34:18 +0100164 if (fib_index == ~0)
165 return -3;
166
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)
171
172 /* Create a new localsid registry */
173 pool_get (sm->localsids, ls);
Dave Barachb7b92992018-10-17 10:38:51 -0400174 clib_memset (ls, 0, sizeof (*ls));
Pablo Camarillofb380952016-12-07 18:34:18 +0100175
176 clib_memcpy (&ls->localsid, localsid_addr, sizeof (ip6_address_t));
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000177 ls->localsid_prefix_len = pref_length;
Pablo Camarillofb380952016-12-07 18:34:18 +0100178 ls->end_psp = end_psp;
179 ls->behavior = behavior;
180 ls->nh_adj = (u32) ~ 0;
181 ls->fib_table = fib_table;
182 switch (behavior)
183 {
184 case SR_BEHAVIOR_END:
185 break;
186 case SR_BEHAVIOR_X:
187 ls->sw_if_index = sw_if_index;
188 clib_memcpy (&ls->next_hop.ip6, &nh_addr->ip6, sizeof (ip6_address_t));
189 break;
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200190 case SR_BEHAVIOR_T:
Neale Ranns10e36992018-02-25 12:39:37 -0800191 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP6, sw_if_index);
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200192 break;
Pablo Camarillofb380952016-12-07 18:34:18 +0100193 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));
196 break;
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));
200 break;
201 case SR_BEHAVIOR_DT6:
Neale Ranns10e36992018-02-25 12:39:37 -0800202 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP6, sw_if_index);
Pablo Camarillofb380952016-12-07 18:34:18 +0100203 break;
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200204 case SR_BEHAVIOR_DT4:
Neale Ranns10e36992018-02-25 12:39:37 -0800205 ls->vrf_index = fib_table_find (FIB_PROTOCOL_IP4, sw_if_index);
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200206 break;
Pablo Camarillofb380952016-12-07 18:34:18 +0100207 case SR_BEHAVIOR_DX2:
208 ls->sw_if_index = sw_if_index;
209 ls->vlan_index = vlan_index;
210 break;
211 }
212
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)
216 {
217 adj_index_t nh_adj_index = ADJ_INDEX_INVALID;
218
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);
223
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);
227
228 /* Check for ADJ creation error. If so panic */
229 if (nh_adj_index == ADJ_INDEX_INVALID)
230 {
231 pool_put (sm->localsids, ls);
232 return -5;
233 }
234
235 ls->nh_adj = nh_adj_index;
236 }
237
238 /* Set DPO */
Ahmed Abdelsalam0ae1d802019-07-25 13:45:55 -0500239 if (ls->behavior == SR_BEHAVIOR_END || ls->behavior == SR_BEHAVIOR_X
240 || ls->behavior == SR_BEHAVIOR_T)
Pablo Camarillofb380952016-12-07 18:34:18 +0100241 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)
246 {
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);
254 if (rv)
255 {
256 pool_put (sm->localsids, ls);
257 return -6;
258 }
259 dpo_set (&dpo, plugin->dpo, DPO_PROTO_IP6, ls - sm->localsids);
260 }
261
262 /* Set hash key for searching localsid by address */
Pablo Camarillo4521afa2017-03-16 10:43:05 +0100263 mhash_set (&sm->sr_localsids_index_hash, localsid_addr, ls - sm->localsids,
264 NULL);
Pablo Camarillofb380952016-12-07 18:34:18 +0100265
266 fib_table_entry_special_dpo_add (fib_index, &pfx, FIB_SOURCE_SR,
267 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
268 dpo_reset (&dpo);
269
270 /* Set counter to zero */
271 vlib_validate_combined_counter (&(sm->sr_ls_valid_counters),
272 ls - sm->localsids);
273 vlib_validate_combined_counter (&(sm->sr_ls_invalid_counters),
274 ls - sm->localsids);
275
276 vlib_zero_combined_counter (&(sm->sr_ls_valid_counters),
277 ls - sm->localsids);
278 vlib_zero_combined_counter (&(sm->sr_ls_invalid_counters),
279 ls - sm->localsids);
280
281 return 0;
282}
283
284/**
285 * @brief SR LocalSID CLI function.
286 *
287 * @see sr_cli_localsid
288 */
289static clib_error_t *
290sr_cli_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
291 vlib_cli_command_t * cmd)
292{
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;
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -0800296 u16 prefix_len = 0;
Pablo Camarillofb380952016-12-07 18:34:18 +0100297 int is_del = 0;
298 int end_psp = 0;
299 ip6_address_t resulting_address;
300 ip46_address_t next_hop;
301 char address_set = 0;
302 char behavior = 0;
303 void *ls_plugin_mem = 0;
304
305 int rv;
306
Dave Barachb7b92992018-10-17 10:38:51 -0400307 clib_memset (&resulting_address, 0, sizeof (ip6_address_t));
Pablo Camarillofb380952016-12-07 18:34:18 +0100308 ip46_address_reset (&next_hop);
309
310 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
311 {
312 if (unformat (input, "del"))
313 is_del = 1;
314 else if (!address_set
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000315 && unformat (input, "address %U/%u", unformat_ip6_address,
316 &resulting_address, &prefix_len))
Pablo Camarillofb380952016-12-07 18:34:18 +0100317 address_set = 1;
318 else if (!address_set
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000319 && unformat (input, "address %U", unformat_ip6_address,
320 &resulting_address))
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -0800321 address_set = 1;
322 else if (!address_set
Pablo Camarillofb380952016-12-07 18:34:18 +0100323 && unformat (input, "addr %U", unformat_ip6_address,
324 &resulting_address))
325 address_set = 1;
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"))
330 {
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;
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200335 else if (unformat (input, "end.t %u", &sw_if_index))
336 behavior = SR_BEHAVIOR_T;
Pablo Camarillofb380952016-12-07 18:34:18 +0100337 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;
352 else
353 {
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;
357
358 /* Create a vector out of the plugin pool as recommended */
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -0800359 /* *INDENT-OFF* */
360 pool_foreach (plugin, sm->plugin_functions,
361 {
362 vec_add1 (vec_plugins, plugin);
363 });
364 /* *INDENT-ON* */
Pablo Camarillofb380952016-12-07 18:34:18 +0100365
366 vec_foreach (plugin_it, vec_plugins)
367 {
368 if (unformat
369 (input, "%U", (*plugin_it)->ls_unformat, &ls_plugin_mem))
370 {
371 behavior = (*plugin_it)->sr_localsid_function_number;
372 break;
373 }
374 }
375 }
376
377 if (!behavior)
378 {
379 if (unformat (input, "end"))
380 behavior = SR_BEHAVIOR_END;
381 else
382 break;
383 }
384 }
385 else if (!end_psp && unformat (input, "psp"))
386 end_psp = 1;
387 else
388 break;
389 }
390
391 if (!behavior && end_psp)
392 behavior = SR_BEHAVIOR_END;
393
394 if (!address_set)
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");
406
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -0800407 rv =
408 sr_cli_localsid (is_del, &resulting_address, prefix_len, end_psp,
409 behavior, sw_if_index, vlan_index, fib_index, &next_hop,
410 ls_plugin_mem);
Pablo Camarillofb380952016-12-07 18:34:18 +0100411
412 switch (rv)
413 {
414 case 0:
415 break;
416 case 1:
417 return 0;
418 case -1:
419 return clib_error_return (0,
420 "Identical localsid already exists. Requested localsid not created.");
421 case -2:
422 return clib_error_return (0,
423 "The requested localsid could not be deleted. SR localsid not found");
424 case -3:
425 return clib_error_return (0, "FIB table %u does not exist", fib_index);
426 case -4:
427 return clib_error_return (0, "There is already one FIB entry for the"
428 "requested localsid non segment routing related");
429 case -5:
430 return clib_error_return (0,
431 "Could not create ARP/ND entry for such next_hop. Internal error.");
432 case -6:
433 return clib_error_return (0,
434 "Error on the plugin based localsid creation.");
435 default:
436 return clib_error_return (0, "BUG: sr localsid returns %d", rv);
437 }
438 return 0;
439}
440
441/* *INDENT-OFF* */
442VLIB_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",
446 .long_help =
447 "Create SR LocalSID and binds it to a particular behavior\n"
448 "Arguments:\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"
452 "\n\tBehaviors:\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,
467};
468/* *INDENT-ON* */
469
470/**
471 * @brief CLI function to 'show' all SR LocalSIDs on console.
472 */
473static clib_error_t *
474show_sr_localsid_command_fn (vlib_main_t * vm, unformat_input_t * input,
475 vlib_cli_command_t * cmd)
476{
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;
481 int i;
482
483 vlib_cli_output (vm, "SRv6 - My LocalSID Table:");
484 vlib_cli_output (vm, "=========================");
485 /* *INDENT-OFF* */
486 pool_foreach (ls, sm->localsids, ({ vec_add1 (localsid_list, ls); }));
487 /* *INDENT-ON* */
488 for (i = 0; i < vec_len (localsid_list); i++)
489 {
490 ls = localsid_list[i];
491 switch (ls->behavior)
492 {
493 case SR_BEHAVIOR_END:
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000494 vlib_cli_output (vm, "\tAddress: \t%U/%u\n\tBehavior: \tEnd",
495 format_ip6_address, &ls->localsid,
496 ls->localsid_prefix_len);
Pablo Camarillofb380952016-12-07 18:34:18 +0100497 break;
498 case SR_BEHAVIOR_X:
499 vlib_cli_output (vm,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000500 "\tAddress: \t%U/%u\n\tBehavior: \tX (Endpoint with Layer-3 cross-connect)"
Pablo Camarillofb380952016-12-07 18:34:18 +0100501 "\n\tIface: \t%U\n\tNext hop: \t%U",
502 format_ip6_address, &ls->localsid,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000503 ls->localsid_prefix_len,
Pablo Camarillofb380952016-12-07 18:34:18 +0100504 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
505 format_ip6_address, &ls->next_hop.ip6);
506 break;
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200507 case SR_BEHAVIOR_T:
508 vlib_cli_output (vm,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000509 "\tAddress: \t%U/%u\n\tBehavior: \tT (Endpoint with specific IPv6 table lookup)"
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200510 "\n\tTable: \t%u",
Ahmed Abdelsalam13e6fce2019-12-08 12:58:27 +0100511 format_ip6_address, &ls->localsid,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000512 ls->localsid_prefix_len,
Ahmed Abdelsalam13e6fce2019-12-08 12:58:27 +0100513 fib_table_get_table_id (ls->vrf_index,
514 FIB_PROTOCOL_IP6));
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200515 break;
Pablo Camarillofb380952016-12-07 18:34:18 +0100516 case SR_BEHAVIOR_DX4:
517 vlib_cli_output (vm,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000518 "\tAddress: \t%U/%u\n\tBehavior: \tDX4 (Endpoint with decapsulation and IPv4 cross-connect)"
Pablo Camarillofb380952016-12-07 18:34:18 +0100519 "\n\tIface: \t%U\n\tNext hop: \t%U",
520 format_ip6_address, &ls->localsid,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000521 ls->localsid_prefix_len,
Pablo Camarillofb380952016-12-07 18:34:18 +0100522 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
523 format_ip4_address, &ls->next_hop.ip4);
524 break;
525 case SR_BEHAVIOR_DX6:
526 vlib_cli_output (vm,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000527 "\tAddress: \t%U/%u\n\tBehavior: \tDX6 (Endpoint with decapsulation and IPv6 cross-connect)"
Pablo Camarillofb380952016-12-07 18:34:18 +0100528 "\n\tIface: \t%U\n\tNext hop: \t%U",
529 format_ip6_address, &ls->localsid,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000530 ls->localsid_prefix_len,
Pablo Camarillofb380952016-12-07 18:34:18 +0100531 format_vnet_sw_if_index_name, vnm, ls->sw_if_index,
532 format_ip6_address, &ls->next_hop.ip6);
533 break;
534 case SR_BEHAVIOR_DX2:
535 if (ls->vlan_index == (u32) ~ 0)
536 vlib_cli_output (vm,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000537 "\tAddress: \t%U/%u\n\tBehavior: \tDX2 (Endpoint with decapulation and Layer-2 cross-connect)"
Pablo Camarillofb380952016-12-07 18:34:18 +0100538 "\n\tIface: \t%U", format_ip6_address,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000539 &ls->localsid, ls->localsid_prefix_len,
540 format_vnet_sw_if_index_name, vnm,
Pablo Camarillofb380952016-12-07 18:34:18 +0100541 ls->sw_if_index);
542 else
543 vlib_cli_output (vm,
544 "Unsupported yet. (DX2 with egress VLAN rewrite)");
545 break;
546 case SR_BEHAVIOR_DT6:
547 vlib_cli_output (vm,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000548 "\tAddress: \t%U/%u\n\tBehavior: \tDT6 (Endpoint with decapsulation and specific IPv6 table lookup)"
Pablo Camarillofb380952016-12-07 18:34:18 +0100549 "\n\tTable: %u", format_ip6_address, &ls->localsid,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000550 ls->localsid_prefix_len,
Ahmed Abdelsalam13e6fce2019-12-08 12:58:27 +0100551 fib_table_get_table_id (ls->vrf_index,
552 FIB_PROTOCOL_IP6));
Pablo Camarillofb380952016-12-07 18:34:18 +0100553 break;
554 case SR_BEHAVIOR_DT4:
555 vlib_cli_output (vm,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000556 "\tAddress: \t%U/%u\n\tBehavior: \tDT4 (Endpoint with decapsulation and specific IPv4 table lookup)"
Pablo Camarillofb380952016-12-07 18:34:18 +0100557 "\n\tTable: \t%u", format_ip6_address,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000558 &ls->localsid, ls->localsid_prefix_len,
Ahmed Abdelsalam13e6fce2019-12-08 12:58:27 +0100559 fib_table_get_table_id (ls->vrf_index,
560 FIB_PROTOCOL_IP4));
Pablo Camarillofb380952016-12-07 18:34:18 +0100561 break;
562 default:
563 if (ls->behavior >= SR_BEHAVIOR_LAST)
564 {
565 sr_localsid_fn_registration_t *plugin =
566 pool_elt_at_index (sm->plugin_functions,
567 ls->behavior - SR_BEHAVIOR_LAST);
568
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000569 vlib_cli_output (vm, "\tAddress: \t%U/%u\n"
Pablo Camarillofb380952016-12-07 18:34:18 +0100570 "\tBehavior: \t%s (%s)\n\t%U",
571 format_ip6_address, &ls->localsid,
Pablo Camarillo79bfd272019-12-18 17:13:13 +0000572 ls->localsid_prefix_len, plugin->keyword_str,
573 plugin->def_str, plugin->ls_format,
574 ls->plugin_mem);
Pablo Camarillofb380952016-12-07 18:34:18 +0100575 }
576 else
577 //Should never get here...
578 vlib_cli_output (vm, "Internal error");
579 break;
580 }
581 if (ls->end_psp)
582 vlib_cli_output (vm, "\tPSP: \tTrue\n");
583
584 /* Print counters */
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, "--------------------");
593 }
594 return 0;
595}
596
597/* *INDENT-OFF* */
598VLIB_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,
602};
603/* *INDENT-ON* */
604
605/**
606 * @brief Function to 'clear' ALL SR localsid counters
607 */
608static clib_error_t *
609clear_sr_localsid_counters_command_fn (vlib_main_t * vm,
610 unformat_input_t * input,
611 vlib_cli_command_t * cmd)
612{
613 ip6_sr_main_t *sm = &sr_main;
614
615 vlib_clear_combined_counters (&(sm->sr_ls_valid_counters));
616 vlib_clear_combined_counters (&(sm->sr_ls_invalid_counters));
617
618 return 0;
619}
620
621/* *INDENT-OFF* */
622VLIB_CLI_COMMAND (clear_sr_localsid_counters_command, static) = {
Francois Clad4abc5c32018-02-01 14:51:33 +0100623 .path = "clear sr localsid-counters",
624 .short_help = "clear sr localsid-counters",
Pablo Camarillofb380952016-12-07 18:34:18 +0100625 .function = clear_sr_localsid_counters_command_fn,
626};
627/* *INDENT-ON* */
628
629/************************ SR LocalSID graphs node ****************************/
630/**
631 * @brief SR localsid node trace
632 */
633typedef struct
634{
Kris Michielsen91074432017-06-22 13:00:20 +0200635 ip6_address_t localsid;
636 u16 behavior;
Pablo Camarillofb380952016-12-07 18:34:18 +0100637 u8 sr[256];
638 u8 num_segments;
639 u8 segments_left;
Pablo Camarillofb380952016-12-07 18:34:18 +0100640} sr_localsid_trace_t;
641
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")
649
650typedef enum
651{
652#define _(sym,str) SR_LOCALSID_ERROR_##sym,
653 foreach_sr_localsid_error
654#undef _
655 SR_LOCALSID_N_ERROR,
656} sr_localsid_error_t;
657
658static char *sr_localsid_error_strings[] = {
659#define _(sym,string) string,
660 foreach_sr_localsid_error
661#undef _
662};
663
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")
671
672typedef enum
673{
674#define _(s,n) SR_LOCALSID_NEXT_##s,
675 foreach_sr_localsid_next
676#undef _
677 SR_LOCALSID_N_NEXT,
678} sr_localsid_next_t;
679
680/**
681 * @brief SR LocalSID graph node trace function
682 *
683 * @see sr_localsid
684 */
685u8 *
686format_sr_localsid_trace (u8 * s, va_list * args)
687{
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 *);
Pablo Camarillofb380952016-12-07 18:34:18 +0100690 sr_localsid_trace_t *t = va_arg (*args, sr_localsid_trace_t *);
691
Pablo Camarillofb380952016-12-07 18:34:18 +0100692 s =
693 format (s, "SR-LOCALSID:\n\tLocalsid: %U\n", format_ip6_address,
Kris Michielsen91074432017-06-22 13:00:20 +0200694 &t->localsid);
695 switch (t->behavior)
Pablo Camarillofb380952016-12-07 18:34:18 +0100696 {
697 case SR_BEHAVIOR_END:
698 s = format (s, "\tBehavior: End\n");
699 break;
700 case SR_BEHAVIOR_DX6:
701 s = format (s, "\tBehavior: Decapsulation with IPv6 L3 xconnect\n");
702 break;
703 case SR_BEHAVIOR_DX4:
704 s = format (s, "\tBehavior: Decapsulation with IPv4 L3 xconnect\n");
705 break;
706 case SR_BEHAVIOR_X:
707 s = format (s, "\tBehavior: IPv6 L3 xconnect\n");
708 break;
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200709 case SR_BEHAVIOR_T:
710 s = format (s, "\tBehavior: IPv6 specific table lookup\n");
711 break;
Pablo Camarillofb380952016-12-07 18:34:18 +0100712 case SR_BEHAVIOR_DT6:
713 s = format (s, "\tBehavior: Decapsulation with IPv6 Table lookup\n");
714 break;
715 case SR_BEHAVIOR_DT4:
716 s = format (s, "\tBehavior: Decapsulation with IPv4 Table lookup\n");
717 break;
718 case SR_BEHAVIOR_DX2:
719 s = format (s, "\tBehavior: Decapsulation with L2 xconnect\n");
720 break;
721 default:
722 s = format (s, "\tBehavior: defined in plugin\n"); //TODO
723 break;
724 }
725 if (t->num_segments != 0xFF)
726 {
727 if (t->num_segments > 0)
728 {
Kris Michielsen91074432017-06-22 13:00:20 +0200729 s = format (s, "\tSegments left: %d\n", t->segments_left);
Pablo Camarillofb380952016-12-07 18:34:18 +0100730 s = format (s, "\tSID list: [in ietf order]");
731 int i = 0;
732 for (i = 0; i < t->num_segments; i++)
733 {
734 s = format (s, "\n\t-> %U", format_ip6_address,
735 (ip6_address_t *) & t->sr[i *
736 sizeof (ip6_address_t)]);
737 }
738 }
739 }
740 return s;
741}
742
743/**
744 * @brief Function doing End processing.
745 */
746static_always_inline void
747end_srh_processing (vlib_node_runtime_t * node,
748 vlib_buffer_t * b0,
749 ip6_header_t * ip0,
750 ip6_sr_header_t * sr0,
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200751 ip6_sr_localsid_t * ls0,
752 u32 * next0, u8 psp, ip6_ext_header_t * prev0)
Pablo Camarillofb380952016-12-07 18:34:18 +0100753{
754 ip6_address_t *new_dst0;
755
Ignas Baciusbd5c49a2020-01-03 15:05:46 +0200756 if (PREDICT_TRUE (sr0 && sr0->type == ROUTING_HEADER_TYPE_SR))
Pablo Camarillofb380952016-12-07 18:34:18 +0100757 {
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200758 if (sr0->segments_left == 1 && psp)
759 {
760 u32 new_l0, sr_len;
761 u64 *copy_dst0, *copy_src0;
762 u32 copy_len_u64s0 = 0;
763
764 ip0->dst_address.as_u64[0] = sr0->segments->as_u64[0];
765 ip0->dst_address.as_u64[1] = sr0->segments->as_u64[1];
766
767 /* Remove the SRH taking care of the rest of IPv6 ext header */
768 if (prev0)
769 prev0->next_hdr = sr0->protocol;
770 else
771 ip0->protocol = sr0->protocol;
772
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
782 * SR header
783 */
784 copy_len_u64s0 =
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];
791
792 int i;
793 for (i = copy_len_u64s0 - 1; i >= 0; i--)
794 {
795 copy_dst0[i] = copy_src0[i];
796 }
797
798 if (ls0->behavior == SR_BEHAVIOR_X)
799 {
800 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
801 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
802 }
803 else if (ls0->behavior == SR_BEHAVIOR_T)
804 {
805 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
806 }
807 }
808 else if (PREDICT_TRUE (sr0->segments_left > 0))
Pablo Camarillofb380952016-12-07 18:34:18 +0100809 {
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];
815
816 if (ls0->behavior == SR_BEHAVIOR_X)
817 {
818 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = ls0->nh_adj;
819 *next0 = SR_LOCALSID_NEXT_IP6_REWRITE;
820 }
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200821 else if (ls0->behavior == SR_BEHAVIOR_T)
822 {
823 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
824 }
Pablo Camarillofb380952016-12-07 18:34:18 +0100825 }
826 else
827 {
828 *next0 = SR_LOCALSID_NEXT_ERROR;
829 b0->error = node->errors[SR_LOCALSID_ERROR_NO_MORE_SEGMENTS];
830 }
831 }
832 else
833 {
834 /* Error. Routing header of type != SR */
835 *next0 = SR_LOCALSID_NEXT_ERROR;
836 b0->error = node->errors[SR_LOCALSID_ERROR_NO_SRH];
837 }
838}
839
840/*
841 * @brief Function doing SRH processing for D* variants
842 */
Pablo Camarillofb380952016-12-07 18:34:18 +0100843static_always_inline void
844end_decaps_srh_processing (vlib_node_runtime_t * node,
845 vlib_buffer_t * b0,
846 ip6_header_t * ip0,
847 ip6_sr_header_t * sr0,
848 ip6_sr_localsid_t * ls0, u32 * next0)
849{
850 /* Compute the size of the IPv6 header with all Ext. headers */
851 u8 next_proto;
852 ip6_ext_header_t *next_ext_header;
853 u16 total_size = 0;
854
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))
859 {
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);
863 }
864
865 /* Ensure this is the last segment. Otherwise drop. */
866 if (sr0 && sr0->segments_left != 0)
867 {
868 *next0 = SR_LOCALSID_NEXT_ERROR;
869 b0->error = node->errors[SR_LOCALSID_ERROR_NOT_LS];
870 return;
871 }
872
873 switch (next_proto)
874 {
875 case IP_PROTOCOL_IPV6:
876 /* Encap-End IPv6. Pop outer IPv6 header. */
877 if (ls0->behavior == SR_BEHAVIOR_DX6)
878 {
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;
882 return;
883 }
884 else if (ls0->behavior == SR_BEHAVIOR_DT6)
885 {
886 vlib_buffer_advance (b0, total_size);
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200887 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
Pablo Camarillofb380952016-12-07 18:34:18 +0100888 return;
889 }
890 break;
891 case IP_PROTOCOL_IP_IN_IP:
892 /* Encap-End IPv4. Pop outer IPv6 header */
893 if (ls0->behavior == SR_BEHAVIOR_DX4)
894 {
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;
898 return;
899 }
900 else if (ls0->behavior == SR_BEHAVIOR_DT4)
901 {
902 vlib_buffer_advance (b0, total_size);
Pablo Camarillo7a4e0922017-06-06 15:18:12 +0200903 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ls0->vrf_index;
Pablo Camarillofb380952016-12-07 18:34:18 +0100904 *next0 = SR_LOCALSID_NEXT_IP4_LOOKUP;
905 return;
906 }
907 break;
pcamaril30e76712020-02-04 08:36:51 +0100908 case IP_PROTOCOL_IP6_ETHERNET:
Pablo Camarillofb380952016-12-07 18:34:18 +0100909 /* L2 encaps */
910 if (ls0->behavior == SR_BEHAVIOR_DX2)
911 {
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;
915 return;
916 }
917 break;
918 }
919 *next0 = SR_LOCALSID_NEXT_ERROR;
920 b0->error = node->errors[SR_LOCALSID_ERROR_NO_INNER_HEADER];
921 return;
922}
923
924/**
Kris Michielsen91074432017-06-22 13:00:20 +0200925 * @brief SR LocalSID graph node. Supports all default SR Endpoint variants with decaps
Pablo Camarillofb380952016-12-07 18:34:18 +0100926 */
927static uword
928sr_localsid_d_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
929 vlib_frame_t * from_frame)
930{
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;
Damjan Marion067cd622018-07-11 12:47:43 +0200936 u32 thread_index = vm->thread_index;
Pablo Camarillofb380952016-12-07 18:34:18 +0100937
938 while (n_left_from > 0)
939 {
940 u32 n_left_to_next;
941 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
942
943 /* Quad - Loop */
944 while (n_left_from >= 8 && n_left_to_next >= 4)
945 {
946 u32 bi0, bi1, bi2, bi3;
947 vlib_buffer_t *b0, *b1, *b2, *b3;
948 ip6_header_t *ip0, *ip1, *ip2, *ip3;
Pablo Camarillofb380952016-12-07 18:34:18 +0100949 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;
953
954 /* Prefetch next iteration. */
955 {
956 vlib_buffer_t *p4, *p5, *p6, *p7;
957
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]);
962
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);
968
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);
973 }
974
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];
979 from += 4;
980 to_next += 4;
981 n_left_from -= 4;
982 n_left_to_next -= 4;
983
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);
988
989 ls0 =
990 pool_elt_at_index (sm->localsids,
991 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
992 ls1 =
993 pool_elt_at_index (sm->localsids,
Francois Cladc82b3582018-07-12 09:56:23 +0200994 vnet_buffer (b1)->ip.adj_index[VLIB_TX]);
Pablo Camarillofb380952016-12-07 18:34:18 +0100995 ls2 =
996 pool_elt_at_index (sm->localsids,
Francois Cladc82b3582018-07-12 09:56:23 +0200997 vnet_buffer (b2)->ip.adj_index[VLIB_TX]);
Pablo Camarillofb380952016-12-07 18:34:18 +0100998 ls3 =
999 pool_elt_at_index (sm->localsids,
Francois Cladc82b3582018-07-12 09:56:23 +02001000 vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
Pablo Camarillofb380952016-12-07 18:34:18 +01001001
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);
1006
Klement Sekera769145c2019-03-06 11:59:57 +01001007 sr0 =
1008 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, NULL);
1009 sr1 =
1010 ip6_ext_header_find (vm, b1, ip1, IP_PROTOCOL_IPV6_ROUTE, NULL);
1011 sr2 =
1012 ip6_ext_header_find (vm, b2, ip2, IP_PROTOCOL_IPV6_ROUTE, NULL);
1013 sr3 =
1014 ip6_ext_header_find (vm, b3, ip3, IP_PROTOCOL_IPV6_ROUTE, NULL);
Pablo Camarillofb380952016-12-07 18:34:18 +01001015
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);
1020
Kris Michielsen91074432017-06-22 13:00:20 +02001021 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1022 {
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))
1030 {
1031 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1032 && sr0->type == ROUTING_HEADER_TYPE_SR)
1033 {
1034 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1035 tr->num_segments =
1036 sr0->length * 8 / sizeof (ip6_address_t);
1037 tr->segments_left = sr0->segments_left;
1038 }
1039 }
1040 else
1041 tr->num_segments = 0xFF;
1042 }
1043
1044 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
1045 {
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))
1053 {
1054 if (ip1->protocol == IP_PROTOCOL_IPV6_ROUTE
1055 && sr1->type == ROUTING_HEADER_TYPE_SR)
1056 {
1057 clib_memcpy (tr->sr, sr1->segments, sr1->length * 8);
1058 tr->num_segments =
1059 sr1->length * 8 / sizeof (ip6_address_t);
1060 tr->segments_left = sr1->segments_left;
1061 }
1062 }
1063 else
1064 tr->num_segments = 0xFF;
1065 }
1066
1067 if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
1068 {
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))
1076 {
1077 if (ip2->protocol == IP_PROTOCOL_IPV6_ROUTE
1078 && sr2->type == ROUTING_HEADER_TYPE_SR)
1079 {
1080 clib_memcpy (tr->sr, sr2->segments, sr2->length * 8);
1081 tr->num_segments =
1082 sr2->length * 8 / sizeof (ip6_address_t);
1083 tr->segments_left = sr2->segments_left;
1084 }
1085 }
1086 else
1087 tr->num_segments = 0xFF;
1088 }
1089
1090 if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
1091 {
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))
1099 {
1100 if (ip3->protocol == IP_PROTOCOL_IPV6_ROUTE
1101 && sr3->type == ROUTING_HEADER_TYPE_SR)
1102 {
1103 clib_memcpy (tr->sr, sr3->segments, sr3->length * 8);
1104 tr->num_segments =
1105 sr3->length * 8 / sizeof (ip6_address_t);
1106 tr->segments_left = sr3->segments_left;
1107 }
1108 }
1109 else
1110 tr->num_segments = 0xFF;
1111 }
Pablo Camarillofb380952016-12-07 18:34:18 +01001112
1113 vlib_increment_combined_counter
1114 (((next0 ==
1115 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001116 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1117 1, vlib_buffer_length_in_chain (vm, b0));
Pablo Camarillofb380952016-12-07 18:34:18 +01001118
1119 vlib_increment_combined_counter
1120 (((next1 ==
1121 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001122 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1123 1, vlib_buffer_length_in_chain (vm, b1));
Pablo Camarillofb380952016-12-07 18:34:18 +01001124
1125 vlib_increment_combined_counter
1126 (((next2 ==
1127 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001128 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1129 1, vlib_buffer_length_in_chain (vm, b2));
Pablo Camarillofb380952016-12-07 18:34:18 +01001130
1131 vlib_increment_combined_counter
1132 (((next3 ==
1133 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001134 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1135 1, vlib_buffer_length_in_chain (vm, b3));
Pablo Camarillofb380952016-12-07 18:34:18 +01001136
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);
1140 }
1141
1142 /* Single loop for potentially the last three packets */
1143 while (n_left_from > 0 && n_left_to_next > 0)
1144 {
1145 u32 bi0;
1146 vlib_buffer_t *b0;
1147 ip6_header_t *ip0;
Pablo Camarillofb380952016-12-07 18:34:18 +01001148 ip6_sr_header_t *sr0;
1149 u32 next0 = SR_LOCALSID_NEXT_IP6_LOOKUP;
1150 ip6_sr_localsid_t *ls0;
1151
1152 bi0 = from[0];
1153 to_next[0] = bi0;
1154 from += 1;
1155 to_next += 1;
1156 n_left_from -= 1;
1157 n_left_to_next -= 1;
1158
1159 b0 = vlib_get_buffer (vm, bi0);
1160 ip0 = vlib_buffer_get_current (b0);
1161
1162 /* Lookup the SR End behavior based on IP DA (adj) */
1163 ls0 =
1164 pool_elt_at_index (sm->localsids,
1165 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1166
1167 /* Find SRH as well as previous header */
Klement Sekera769145c2019-03-06 11:59:57 +01001168 sr0 =
1169 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, NULL);
Pablo Camarillofb380952016-12-07 18:34:18 +01001170
1171 /* SRH processing and End variants */
1172 end_decaps_srh_processing (node, b0, ip0, sr0, ls0, &next0);
1173
1174 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1175 {
1176 sr_localsid_trace_t *tr =
1177 vlib_add_trace (vm, node, b0, sizeof (*tr));
1178 tr->num_segments = 0;
Kris Michielsen91074432017-06-22 13:00:20 +02001179 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1180 sizeof (tr->localsid.as_u8));
1181 tr->behavior = ls0->behavior;
Pablo Camarillofb380952016-12-07 18:34:18 +01001182 if (ip0 == vlib_buffer_get_current (b0))
1183 {
Pablo Camarillofb380952016-12-07 18:34:18 +01001184 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1185 && sr0->type == ROUTING_HEADER_TYPE_SR)
1186 {
1187 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1188 tr->num_segments =
1189 sr0->length * 8 / sizeof (ip6_address_t);
1190 tr->segments_left = sr0->segments_left;
1191 }
1192 }
1193 else
1194 tr->num_segments = 0xFF;
1195 }
1196
1197 /* Increase the counters */
1198 vlib_increment_combined_counter
1199 (((next0 ==
1200 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001201 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1202 1, vlib_buffer_length_in_chain (vm, b0));
Pablo Camarillofb380952016-12-07 18:34:18 +01001203
1204 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1205 n_left_to_next, bi0, next0);
1206 }
1207 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1208 }
1209 return from_frame->n_vectors;
1210}
1211
1212/* *INDENT-OFF* */
1213VLIB_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,
1222 .next_nodes = {
1223#define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1224 foreach_sr_localsid_next
1225#undef _
1226 },
1227};
1228/* *INDENT-ON* */
1229
1230/**
Kris Michielsen91074432017-06-22 13:00:20 +02001231 * @brief SR LocalSID graph node. Supports all default SR Endpoint without decaps
Pablo Camarillofb380952016-12-07 18:34:18 +01001232 */
1233static uword
1234sr_localsid_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1235 vlib_frame_t * from_frame)
1236{
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;
Damjan Marion067cd622018-07-11 12:47:43 +02001242 u32 thread_index = vm->thread_index;
Pablo Camarillofb380952016-12-07 18:34:18 +01001243
1244 while (n_left_from > 0)
1245 {
1246 u32 n_left_to_next;
1247 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1248
1249 /* Quad - Loop */
1250 while (n_left_from >= 8 && n_left_to_next >= 4)
1251 {
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;
1260
1261 /* Prefetch next iteration. */
1262 {
1263 vlib_buffer_t *p4, *p5, *p6, *p7;
1264
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]);
1269
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);
1275
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);
1280 }
1281
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];
1286 from += 4;
1287 to_next += 4;
1288 n_left_from -= 4;
1289 n_left_to_next -= 4;
1290
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);
1295
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);
1300
Klement Sekera769145c2019-03-06 11:59:57 +01001301 sr0 =
1302 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, &prev0);
1303 sr1 =
1304 ip6_ext_header_find (vm, b1, ip1, IP_PROTOCOL_IPV6_ROUTE, &prev1);
1305 sr2 =
1306 ip6_ext_header_find (vm, b2, ip2, IP_PROTOCOL_IPV6_ROUTE, &prev2);
1307 sr3 =
1308 ip6_ext_header_find (vm, b3, ip3, IP_PROTOCOL_IPV6_ROUTE, &prev3);
Pablo Camarillofb380952016-12-07 18:34:18 +01001309
1310 ls0 =
1311 pool_elt_at_index (sm->localsids,
1312 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1313 ls1 =
1314 pool_elt_at_index (sm->localsids,
Francois Cladc82b3582018-07-12 09:56:23 +02001315 vnet_buffer (b1)->ip.adj_index[VLIB_TX]);
Pablo Camarillofb380952016-12-07 18:34:18 +01001316 ls2 =
1317 pool_elt_at_index (sm->localsids,
Francois Cladc82b3582018-07-12 09:56:23 +02001318 vnet_buffer (b2)->ip.adj_index[VLIB_TX]);
Pablo Camarillofb380952016-12-07 18:34:18 +01001319 ls3 =
1320 pool_elt_at_index (sm->localsids,
Francois Cladc82b3582018-07-12 09:56:23 +02001321 vnet_buffer (b3)->ip.adj_index[VLIB_TX]);
Pablo Camarillofb380952016-12-07 18:34:18 +01001322
Pablo Camarillo7a4e0922017-06-06 15:18:12 +02001323 end_srh_processing (node, b0, ip0, sr0, ls0, &next0, ls0->end_psp,
1324 prev0);
1325 end_srh_processing (node, b1, ip1, sr1, ls1, &next1, ls1->end_psp,
1326 prev1);
1327 end_srh_processing (node, b2, ip2, sr2, ls2, &next2, ls2->end_psp,
1328 prev2);
1329 end_srh_processing (node, b3, ip3, sr3, ls3, &next3, ls3->end_psp,
1330 prev3);
Pablo Camarillofb380952016-12-07 18:34:18 +01001331
Kris Michielsen91074432017-06-22 13:00:20 +02001332 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1333 {
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))
1341 {
1342 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1343 && sr0->type == ROUTING_HEADER_TYPE_SR)
1344 {
1345 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1346 tr->num_segments =
1347 sr0->length * 8 / sizeof (ip6_address_t);
1348 tr->segments_left = sr0->segments_left;
1349 }
1350 }
1351 else
1352 tr->num_segments = 0xFF;
1353 }
1354
1355 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
1356 {
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))
1364 {
1365 if (ip1->protocol == IP_PROTOCOL_IPV6_ROUTE
1366 && sr1->type == ROUTING_HEADER_TYPE_SR)
1367 {
1368 clib_memcpy (tr->sr, sr1->segments, sr1->length * 8);
1369 tr->num_segments =
1370 sr1->length * 8 / sizeof (ip6_address_t);
1371 tr->segments_left = sr1->segments_left;
1372 }
1373 }
1374 else
1375 tr->num_segments = 0xFF;
1376 }
1377
1378 if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
1379 {
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))
1387 {
1388 if (ip2->protocol == IP_PROTOCOL_IPV6_ROUTE
1389 && sr2->type == ROUTING_HEADER_TYPE_SR)
1390 {
1391 clib_memcpy (tr->sr, sr2->segments, sr2->length * 8);
1392 tr->num_segments =
1393 sr2->length * 8 / sizeof (ip6_address_t);
1394 tr->segments_left = sr2->segments_left;
1395 }
1396 }
1397 else
1398 tr->num_segments = 0xFF;
1399 }
1400
1401 if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
1402 {
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))
1410 {
1411 if (ip3->protocol == IP_PROTOCOL_IPV6_ROUTE
1412 && sr3->type == ROUTING_HEADER_TYPE_SR)
1413 {
1414 clib_memcpy (tr->sr, sr3->segments, sr3->length * 8);
1415 tr->num_segments =
1416 sr3->length * 8 / sizeof (ip6_address_t);
1417 tr->segments_left = sr3->segments_left;
1418 }
1419 }
1420 else
1421 tr->num_segments = 0xFF;
1422 }
Pablo Camarillofb380952016-12-07 18:34:18 +01001423
1424 vlib_increment_combined_counter
1425 (((next0 ==
1426 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001427 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1428 1, vlib_buffer_length_in_chain (vm, b0));
Pablo Camarillofb380952016-12-07 18:34:18 +01001429
1430 vlib_increment_combined_counter
1431 (((next1 ==
1432 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001433 &(sm->sr_ls_valid_counters)), thread_index, ls1 - sm->localsids,
1434 1, vlib_buffer_length_in_chain (vm, b1));
Pablo Camarillofb380952016-12-07 18:34:18 +01001435
1436 vlib_increment_combined_counter
1437 (((next2 ==
1438 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001439 &(sm->sr_ls_valid_counters)), thread_index, ls2 - sm->localsids,
1440 1, vlib_buffer_length_in_chain (vm, b2));
Pablo Camarillofb380952016-12-07 18:34:18 +01001441
1442 vlib_increment_combined_counter
1443 (((next3 ==
1444 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001445 &(sm->sr_ls_valid_counters)), thread_index, ls3 - sm->localsids,
1446 1, vlib_buffer_length_in_chain (vm, b3));
Pablo Camarillofb380952016-12-07 18:34:18 +01001447
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);
1451 }
1452
1453 /* Single loop for potentially the last three packets */
1454 while (n_left_from > 0 && n_left_to_next > 0)
1455 {
1456 u32 bi0;
1457 vlib_buffer_t *b0;
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;
1463
1464 bi0 = from[0];
1465 to_next[0] = bi0;
1466 from += 1;
1467 to_next += 1;
1468 n_left_from -= 1;
1469 n_left_to_next -= 1;
1470
1471 b0 = vlib_get_buffer (vm, bi0);
1472 ip0 = vlib_buffer_get_current (b0);
Klement Sekera769145c2019-03-06 11:59:57 +01001473 sr0 =
1474 ip6_ext_header_find (vm, b0, ip0, IP_PROTOCOL_IPV6_ROUTE, &prev0);
Pablo Camarillofb380952016-12-07 18:34:18 +01001475
1476 /* Lookup the SR End behavior based on IP DA (adj) */
1477 ls0 =
1478 pool_elt_at_index (sm->localsids,
1479 vnet_buffer (b0)->ip.adj_index[VLIB_TX]);
1480
1481 /* SRH processing */
Pablo Camarillo7a4e0922017-06-06 15:18:12 +02001482 end_srh_processing (node, b0, ip0, sr0, ls0, &next0, ls0->end_psp,
1483 prev0);
Pablo Camarillofb380952016-12-07 18:34:18 +01001484
1485 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1486 {
1487 sr_localsid_trace_t *tr =
1488 vlib_add_trace (vm, node, b0, sizeof (*tr));
1489 tr->num_segments = 0;
Kris Michielsen91074432017-06-22 13:00:20 +02001490 clib_memcpy (tr->localsid.as_u8, ls0->localsid.as_u8,
1491 sizeof (tr->localsid.as_u8));
1492 tr->behavior = ls0->behavior;
Pablo Camarillofb380952016-12-07 18:34:18 +01001493 if (ip0 == vlib_buffer_get_current (b0))
1494 {
Pablo Camarillofb380952016-12-07 18:34:18 +01001495 if (ip0->protocol == IP_PROTOCOL_IPV6_ROUTE
1496 && sr0->type == ROUTING_HEADER_TYPE_SR)
1497 {
1498 clib_memcpy (tr->sr, sr0->segments, sr0->length * 8);
1499 tr->num_segments =
1500 sr0->length * 8 / sizeof (ip6_address_t);
1501 tr->segments_left = sr0->segments_left;
1502 }
1503 }
1504 else
Kris Michielsen91074432017-06-22 13:00:20 +02001505 tr->num_segments = 0xFF;
Pablo Camarillofb380952016-12-07 18:34:18 +01001506 }
1507
1508 vlib_increment_combined_counter
1509 (((next0 ==
1510 SR_LOCALSID_NEXT_ERROR) ? &(sm->sr_ls_invalid_counters) :
Damjan Marion586afd72017-04-05 19:18:20 +02001511 &(sm->sr_ls_valid_counters)), thread_index, ls0 - sm->localsids,
1512 1, vlib_buffer_length_in_chain (vm, b0));
Pablo Camarillofb380952016-12-07 18:34:18 +01001513
1514 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1515 n_left_to_next, bi0, next0);
1516 }
1517 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1518 }
1519 return from_frame->n_vectors;
1520}
1521
1522/* *INDENT-OFF* */
1523VLIB_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,
1532 .next_nodes = {
1533#define _(s,n) [SR_LOCALSID_NEXT_##s] = n,
1534 foreach_sr_localsid_next
1535#undef _
1536 },
1537};
1538/* *INDENT-ON* */
1539
1540static u8 *
1541format_sr_dpo (u8 * s, va_list * args)
1542{
1543 index_t index = va_arg (*args, index_t);
1544 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
1545
1546 return (format (s, "SR: localsid_index:[%d]", index));
1547}
1548
1549const 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,
1553};
1554
1555const static char *const sr_loc_ip6_nodes[] = {
1556 "sr-localsid",
1557 NULL,
1558};
1559
1560const static char *const *const sr_loc_nodes[DPO_PROTO_NUM] = {
1561 [DPO_PROTO_IP6] = sr_loc_ip6_nodes,
1562};
1563
1564const static char *const sr_loc_d_ip6_nodes[] = {
1565 "sr-localsid-d",
1566 NULL,
1567};
1568
1569const static char *const *const sr_loc_d_nodes[DPO_PROTO_NUM] = {
1570 [DPO_PROTO_IP6] = sr_loc_d_ip6_nodes,
1571};
1572
1573
1574/*************************** SR LocalSID plugins ******************************/
1575/**
1576 * @brief SR LocalSID plugin registry
1577 */
1578int
1579sr_localsid_register_function (vlib_main_t * vm, u8 * fn_name,
1580 u8 * keyword_str, u8 * def_str,
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -08001581 u8 * params_str, u8 prefix_length,
1582 dpo_type_t * dpo,
Pablo Camarillofb380952016-12-07 18:34:18 +01001583 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)
1587{
1588 ip6_sr_main_t *sm = &sr_main;
1589 uword *p;
1590
1591 sr_localsid_fn_registration_t *plugin;
1592
1593 /* Did this function exist? If so update it */
1594 p = hash_get_mem (sm->plugin_functions_by_key, fn_name);
1595 if (p)
1596 {
1597 plugin = pool_elt_at_index (sm->plugin_functions, p[0]);
1598 }
1599 /* Else create a new one and set hash key */
1600 else
1601 {
1602 pool_get (sm->plugin_functions, plugin);
1603 hash_set_mem (sm->plugin_functions_by_key, fn_name,
1604 plugin - sm->plugin_functions);
1605 }
1606
Dave Barachb7b92992018-10-17 10:38:51 -04001607 clib_memset (plugin, 0, sizeof (*plugin));
Pablo Camarillofb380952016-12-07 18:34:18 +01001608
1609 plugin->sr_localsid_function_number = (plugin - sm->plugin_functions);
1610 plugin->sr_localsid_function_number += SR_BEHAVIOR_LAST;
Tetsuya Murakami1b81e6e2019-11-06 11:05:51 -08001611 plugin->prefix_length = prefix_length;
Pablo Camarillofb380952016-12-07 18:34:18 +01001612 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);
1621
1622 return plugin->sr_localsid_function_number;
1623}
1624
1625/**
1626 * @brief CLI function to 'show' all available SR LocalSID behaviors
1627 */
1628static clib_error_t *
1629show_sr_localsid_behaviors_command_fn (vlib_main_t * vm,
1630 unformat_input_t * input,
1631 vlib_cli_command_t * cmd)
1632{
1633 ip6_sr_main_t *sm = &sr_main;
1634 sr_localsid_fn_registration_t *plugin;
1635 sr_localsid_fn_registration_t **plugins_vec = 0;
1636 int i;
1637
1638 vlib_cli_output (vm,
1639 "SR LocalSIDs behaviors:\n-----------------------\n\n");
1640
1641 /* *INDENT-OFF* */
1642 pool_foreach (plugin, sm->plugin_functions,
1643 ({ vec_add1 (plugins_vec, plugin); }));
1644 /* *INDENT-ON* */
1645
1646 /* Print static behaviors */
1647 vlib_cli_output (vm, "Default behaviors:\n"
1648 "\tEnd\t-> Endpoint.\n"
Pablo Camarillo7a4e0922017-06-06 15:18:12 +02001649 "\tEnd.X\t-> Endpoint with Layer-3 cross-connect.\n"
Pablo Camarillofb380952016-12-07 18:34:18 +01001650 "\t\tParameters: '<iface> <ip6_next_hop>'\n"
Pablo Camarillo7a4e0922017-06-06 15:18:12 +02001651 "\tEnd.T\t-> Endpoint with specific IPv6 table lookup.\n"
1652 "\t\tParameters: '<fib_table>'\n"
Pablo Camarillofb380952016-12-07 18:34:18 +01001653 "\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++)
1665 {
1666 plugin = plugins_vec[i];
1667 vlib_cli_output (vm, "\t%s\t-> %s.\n", plugin->keyword_str,
1668 plugin->def_str);
1669 vlib_cli_output (vm, "\t\tParameters: '%s'\n", plugin->params_str);
1670 }
1671 return 0;
1672}
1673
1674/* *INDENT-OFF* */
1675VLIB_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,
1679};
1680/* *INDENT-ON* */
1681
1682/**
1683 * @brief SR LocalSID initialization
1684 */
1685clib_error_t *
1686sr_localsids_init (vlib_main_t * vm)
1687{
1688 /* Init memory for function keys */
1689 ip6_sr_main_t *sm = &sr_main;
Pablo Camarillo4521afa2017-03-16 10:43:05 +01001690 mhash_init (&sm->sr_localsids_index_hash, sizeof (uword),
1691 sizeof (ip6_address_t));
Pablo Camarillofb380952016-12-07 18:34:18 +01001692 /* 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));
1699 return 0;
1700}
1701
1702VLIB_INIT_FUNCTION (sr_localsids_init);
1703/*
1704* fd.io coding-style-patch-verification: ON
1705*
1706* Local Variables:
1707* eval: (c-set-style "gnu")
1708* End:
1709*/