blob: c1d036a0e8bcad1d2fb6dec39c820983076365a6 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/adj/adj.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010017#include <vnet/adj/adj_internal.h>
18#include <vnet/adj/adj_glean.h>
19#include <vnet/adj/adj_midchain.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000020#include <vnet/adj/adj_mcast.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#include <vnet/fib/fib_node_list.h>
22
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023/* Adjacency packet/byte counters indexed by adjacency index. */
24vlib_combined_counter_main_t adjacency_counters;
25
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010026/*
27 * the single adj pool
28 */
29ip_adjacency_t *adj_pool;
30
Neale Ranns9c6a6132017-02-21 05:33:14 -080031/**
32 * @brief Global Config for enabling per-adjacency counters.
33 * By default these are disabled.
34 */
35int adj_per_adj_counters;
36
Neale Ranns0bfe5d82016-08-25 15:29:12 +010037always_inline void
38adj_poison (ip_adjacency_t * adj)
39{
40 if (CLIB_DEBUG > 0)
41 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +010042 memset (adj, 0xfe, sizeof (adj[0]));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043 }
44}
45
46ip_adjacency_t *
47adj_alloc (fib_protocol_t proto)
48{
49 ip_adjacency_t *adj;
50
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010051 pool_get(adj_pool, adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010052
53 adj_poison(adj);
54
55 /* Make sure certain fields are always initialized. */
56 /* Validate adjacency counters. */
57 vlib_validate_combined_counter(&adjacency_counters,
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010058 adj_get_index(adj));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010059
60 adj->rewrite_header.sw_if_index = ~0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010061 adj->n_adj = 1;
Neale Ranns177bbdc2016-11-15 09:46:51 +000062 adj->lookup_next_index = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010063
64 fib_node_init(&adj->ia_node,
65 FIB_NODE_TYPE_ADJ);
66 adj->ia_nh_proto = proto;
Neale Ranns19c68d22016-12-07 15:38:14 +000067 adj->ia_flags = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010068
Neale Ranns6c3ebcc2016-10-02 21:20:15 +010069 ip4_main.lookup_main.adjacency_heap = adj_pool;
70 ip6_main.lookup_main.adjacency_heap = adj_pool;
71
Neale Ranns0bfe5d82016-08-25 15:29:12 +010072 return (adj);
73}
74
75static int
76adj_index_is_special (adj_index_t adj_index)
77{
78 if (ADJ_INDEX_INVALID == adj_index)
79 return (!0);
80
81 return (0);
82}
83
84/**
85 * @brief Pretty print helper function for formatting specific adjacencies.
86 * @param s - input string to format
87 * @param args - other args passed to format function such as:
88 * - vnet_main_t
89 * - ip_lookup_main_t
90 * - adj_index
91 */
92u8 *
93format_ip_adjacency (u8 * s, va_list * args)
94{
Neale Rannsb80c5362016-10-08 13:03:40 +010095 format_ip_adjacency_flags_t fiaf;
96 ip_adjacency_t * adj;
97 u32 adj_index;
98
99 adj_index = va_arg (*args, u32);
100 fiaf = va_arg (*args, format_ip_adjacency_flags_t);
101 adj = adj_get(adj_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100102
Neale Rannsb80c5362016-10-08 13:03:40 +0100103 switch (adj->lookup_next_index)
104 {
105 case IP_LOOKUP_NEXT_REWRITE:
106 s = format (s, "%U", format_adj_nbr, adj_index, 0);
107 break;
108 case IP_LOOKUP_NEXT_ARP:
109 s = format (s, "%U", format_adj_nbr_incomplete, adj_index, 0);
110 break;
111 case IP_LOOKUP_NEXT_GLEAN:
112 s = format (s, "%U", format_adj_glean, adj_index, 0);
113 break;
114 case IP_LOOKUP_NEXT_MIDCHAIN:
115 s = format (s, "%U", format_adj_midchain, adj_index, 2);
116 break;
Neale Ranns32e1c012016-11-22 17:07:28 +0000117 case IP_LOOKUP_NEXT_MCAST:
118 s = format (s, "%U", format_adj_mcast, adj_index, 0);
119 break;
Neale Rannsb80c5362016-10-08 13:03:40 +0100120 default:
121 break;
122 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100123
Neale Rannsb80c5362016-10-08 13:03:40 +0100124 if (fiaf & FORMAT_IP_ADJACENCY_DETAIL)
125 {
Neale Ranns044183f2017-01-24 01:34:25 -0800126 vlib_counter_t counts;
127
128 vlib_get_combined_counter(&adjacency_counters, adj_index, &counts);
129 s = format (s, "\n counts:[%Ld:%Ld]", counts.packets, counts.bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100130 s = format (s, "\n locks:%d", adj->ia_node.fn_locks);
Neale Rannsb80c5362016-10-08 13:03:40 +0100131 s = format(s, "\n children:\n ");
132 s = fib_node_children_format(adj->ia_node.fn_children, s);
133 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100134
Neale Rannsb80c5362016-10-08 13:03:40 +0100135 return s;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100136}
137
138/*
139 * adj_last_lock_gone
140 *
141 * last lock/reference to the adj has gone, we no longer need it.
142 */
143static void
144adj_last_lock_gone (ip_adjacency_t *adj)
145{
Neale Rannsb80c5362016-10-08 13:03:40 +0100146 vlib_main_t * vm = vlib_get_main();
147
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100148 ASSERT(0 == fib_node_list_get_size(adj->ia_node.fn_children));
149 ADJ_DBG(adj, "last-lock-gone");
150
Neale Rannsb80c5362016-10-08 13:03:40 +0100151 vlib_worker_thread_barrier_sync (vm);
152
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100153 switch (adj->lookup_next_index)
154 {
155 case IP_LOOKUP_NEXT_MIDCHAIN:
156 dpo_reset(&adj->sub_type.midchain.next_dpo);
157 /* FALL THROUGH */
158 case IP_LOOKUP_NEXT_ARP:
159 case IP_LOOKUP_NEXT_REWRITE:
160 /*
161 * complete and incomplete nbr adjs
162 */
Neale Ranns177bbdc2016-11-15 09:46:51 +0000163 adj_nbr_remove(adj_get_index(adj),
164 adj->ia_nh_proto,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100165 adj->ia_link,
166 &adj->sub_type.nbr.next_hop,
167 adj->rewrite_header.sw_if_index);
168 break;
169 case IP_LOOKUP_NEXT_GLEAN:
170 adj_glean_remove(adj->ia_nh_proto,
171 adj->rewrite_header.sw_if_index);
172 break;
Neale Ranns32e1c012016-11-22 17:07:28 +0000173 case IP_LOOKUP_NEXT_MCAST:
174 adj_mcast_remove(adj->ia_nh_proto,
175 adj->rewrite_header.sw_if_index);
176 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100177 default:
178 /*
179 * type not stored in any DB from which we need to remove it
180 */
181 break;
182 }
183
Neale Rannsb80c5362016-10-08 13:03:40 +0100184 vlib_worker_thread_barrier_release(vm);
185
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100186 fib_node_deinit(&adj->ia_node);
Neale Ranns6c3ebcc2016-10-02 21:20:15 +0100187 pool_put(adj_pool, adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100188}
189
190void
191adj_lock (adj_index_t adj_index)
192{
193 ip_adjacency_t *adj;
194
195 if (adj_index_is_special(adj_index))
196 {
197 return;
198 }
199
200 adj = adj_get(adj_index);
201 ASSERT(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100202
203 ADJ_DBG(adj, "lock");
204 fib_node_lock(&adj->ia_node);
205}
206
207void
208adj_unlock (adj_index_t adj_index)
209{
210 ip_adjacency_t *adj;
211
212 if (adj_index_is_special(adj_index))
213 {
214 return;
215 }
216
217 adj = adj_get(adj_index);
218 ASSERT(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100219
220 ADJ_DBG(adj, "unlock");
221 ASSERT(adj);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100222
223 fib_node_unlock(&adj->ia_node);
224}
225
226u32
227adj_child_add (adj_index_t adj_index,
228 fib_node_type_t child_type,
229 fib_node_index_t child_index)
230{
231 ASSERT(ADJ_INDEX_INVALID != adj_index);
232 if (adj_index_is_special(adj_index))
233 {
234 return (~0);
235 }
236
237 return (fib_node_child_add(FIB_NODE_TYPE_ADJ,
238 adj_index,
239 child_type,
240 child_index));
241}
242
243void
244adj_child_remove (adj_index_t adj_index,
245 u32 sibling_index)
246{
247 if (adj_index_is_special(adj_index))
248 {
249 return;
250 }
251
252 fib_node_child_remove(FIB_NODE_TYPE_ADJ,
253 adj_index,
254 sibling_index);
255}
256
Neale Rannsb069a692017-03-15 12:34:25 -0400257/*
258 * Context for the walk to update the cached feture flags.
259 */
260typedef struct adj_feature_update_t_
261{
262 u8 arc;
263 u8 enable;
264} adj_feature_update_ctx_t;
265
266static adj_walk_rc_t
267adj_feature_update_walk_cb (adj_index_t ai,
268 void *arg)
269{
270 adj_feature_update_ctx_t *ctx = arg;
271 ip_adjacency_t *adj;
272
273 adj = adj_get(ai);
274
275 /*
276 * this ugly mess matches the feature arc that is changing with affected
277 * adjacencies
278 */
279 if (((ctx->arc == ip6_main.lookup_main.output_feature_arc_index) &&
280 (VNET_LINK_IP6 == adj->ia_link)) ||
281 ((ctx->arc == ip4_main.lookup_main.output_feature_arc_index) &&
282 (VNET_LINK_IP4 == adj->ia_link)) ||
283 ((ctx->arc == mpls_main.output_feature_arc_index) &&
284 (VNET_LINK_MPLS == adj->ia_link)))
285 {
286 if (ctx->enable)
287 adj->rewrite_header.flags |= VNET_REWRITE_HAS_FEATURES;
288 else
289 adj->rewrite_header.flags &= ~VNET_REWRITE_HAS_FEATURES;
290 }
291 return (ADJ_WALK_RC_CONTINUE);
292}
293
294void
295adj_feature_update (u32 sw_if_index,
296 u8 arc_index,
297 u8 is_enable)
298{
299 /*
300 * Walk all the adjacencies on the interface to update the cached
301 * 'has-features' flag
302 */
303 adj_feature_update_ctx_t ctx = {
304 .arc = arc_index,
305 .enable = is_enable,
306 };
307 adj_walk (sw_if_index, adj_feature_update_walk_cb, &ctx);
308}
309
310/**
311 * @brief Walk the Adjacencies on a given interface
312 */
313void
314adj_walk (u32 sw_if_index,
315 adj_walk_cb_t cb,
316 void *ctx)
317{
318 /*
319 * walk all the neighbor adjacencies
320 */
321 fib_protocol_t proto;
322
323 FOR_EACH_FIB_IP_PROTOCOL(proto)
324 {
325 adj_nbr_walk(sw_if_index, proto, cb, ctx);
326 }
327}
328
Neale Rannsb80c5362016-10-08 13:03:40 +0100329/**
330 * @brief Return the link type of the adjacency
331 */
332vnet_link_t
333adj_get_link_type (adj_index_t ai)
334{
335 const ip_adjacency_t *adj;
336
337 adj = adj_get(ai);
338
339 return (adj->ia_link);
340}
341
342/**
343 * @brief Return the sw interface index of the adjacency.
344 */
345u32
346adj_get_sw_if_index (adj_index_t ai)
347{
348 const ip_adjacency_t *adj;
349
350 adj = adj_get(ai);
351
352 return (adj->rewrite_header.sw_if_index);
353}
354
355/**
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700356 * @brief Return the rewrite string of the adjacency
Neale Rannsb80c5362016-10-08 13:03:40 +0100357 */
358const u8*
359adj_get_rewrite (adj_index_t ai)
360{
361 vnet_rewrite_header_t *rw;
362 ip_adjacency_t *adj;
363
364 adj = adj_get(ai);
365 rw = &adj->rewrite_header;
366
367 ASSERT (rw->data_bytes != 0xfefe);
368
369 return (rw->data - rw->data_bytes);
370}
371
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100372static fib_node_t *
373adj_get_node (fib_node_index_t index)
374{
375 ip_adjacency_t *adj;
376
377 adj = adj_get(index);
378
379 return (&adj->ia_node);
380}
381
382#define ADJ_FROM_NODE(_node) \
383 ((ip_adjacency_t*)((char*)_node - STRUCT_OFFSET_OF(ip_adjacency_t, ia_node)))
384
385static void
386adj_node_last_lock_gone (fib_node_t *node)
387{
388 adj_last_lock_gone(ADJ_FROM_NODE(node));
389}
390
391static fib_node_back_walk_rc_t
392adj_back_walk_notify (fib_node_t *node,
393 fib_node_back_walk_ctx_t *ctx)
394{
395 /*
396 * Que pasa. yo soj en el final!
397 */
398 ASSERT(0);
399
400 return (FIB_NODE_BACK_WALK_CONTINUE);
401}
402
403/*
404 * Adjacency's graph node virtual function table
405 */
406static const fib_node_vft_t adj_vft = {
407 .fnv_get = adj_get_node,
408 .fnv_last_lock = adj_node_last_lock_gone,
409 .fnv_back_walk = adj_back_walk_notify,
410};
411
412static clib_error_t *
413adj_module_init (vlib_main_t * vm)
414{
415 fib_node_register_type(FIB_NODE_TYPE_ADJ, &adj_vft);
416
417 adj_nbr_module_init();
418 adj_glean_module_init();
419 adj_midchain_module_init();
Neale Ranns32e1c012016-11-22 17:07:28 +0000420 adj_mcast_module_init();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100421
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100422 return (NULL);
423}
424
425VLIB_INIT_FUNCTION (adj_module_init);
426
Neale Rannsb80c5362016-10-08 13:03:40 +0100427static clib_error_t *
428adj_show (vlib_main_t * vm,
429 unformat_input_t * input,
430 vlib_cli_command_t * cmd)
431{
432 adj_index_t ai = ADJ_INDEX_INVALID;
433 u32 sw_if_index = ~0;
Neale Ranns9c6a6132017-02-21 05:33:14 -0800434 int summary = 0;
Neale Rannsb80c5362016-10-08 13:03:40 +0100435
436 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
437 {
438 if (unformat (input, "%d", &ai))
439 ;
Neale Ranns9c6a6132017-02-21 05:33:14 -0800440 else if (unformat (input, "sum"))
441 summary = 1;
442 else if (unformat (input, "summary"))
443 summary = 1;
Neale Rannsb80c5362016-10-08 13:03:40 +0100444 else if (unformat (input, "%U",
445 unformat_vnet_sw_interface, vnet_get_main(),
446 &sw_if_index))
447 ;
448 else
449 break;
450 }
451
Neale Ranns9c6a6132017-02-21 05:33:14 -0800452 if (summary)
Neale Rannsb80c5362016-10-08 13:03:40 +0100453 {
Neale Ranns9c6a6132017-02-21 05:33:14 -0800454 vlib_cli_output (vm, "Number of adjacenies: %d", pool_elts(adj_pool));
455 vlib_cli_output (vm, "Per-adjacency counters: %s",
456 (adj_are_counters_enabled() ?
457 "enabled":
458 "disabled"));
Neale Rannsb80c5362016-10-08 13:03:40 +0100459 }
460 else
461 {
Neale Ranns9c6a6132017-02-21 05:33:14 -0800462 if (ADJ_INDEX_INVALID != ai)
463 {
464 if (pool_is_free_index(adj_pool, ai))
Neale Ranns8b37b872016-11-21 12:25:22 +0000465 {
Neale Ranns9c6a6132017-02-21 05:33:14 -0800466 vlib_cli_output (vm, "adjacency %d invalid", ai);
467 return 0;
468 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100469
Neale Ranns9c6a6132017-02-21 05:33:14 -0800470 vlib_cli_output (vm, "[@%d] %U",
471 ai,
472 format_ip_adjacency, ai,
473 FORMAT_IP_ADJACENCY_DETAIL);
474 }
475 else
476 {
477 /* *INDENT-OFF* */
478 pool_foreach_index(ai, adj_pool,
479 ({
480 if (~0 != sw_if_index &&
481 sw_if_index != adj_get_sw_if_index(ai))
482 {
483 }
484 else
485 {
486 vlib_cli_output (vm, "[@%d] %U",
487 ai,
488 format_ip_adjacency, ai,
489 FORMAT_IP_ADJACENCY_NONE);
490 }
491 }));
492 /* *INDENT-ON* */
493 }
494 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100495 return 0;
496}
497
498/*?
499 * Show all adjacencies.
500 * @cliexpar
501 * @cliexstart{sh adj}
502 * [@0]
503 * [@1] glean: loop0
504 * [@2] ipv4 via 1.0.0.2 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
505 * [@3] mpls via 1.0.0.2 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
506 * [@4] ipv4 via 1.0.0.3 loop0: IP4: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
507 * [@5] mpls via 1.0.0.3 loop0: MPLS_UNICAST: 00:00:22:aa:bb:cc -> 00:00:11:aa:bb:cc
508 * @cliexend
509 ?*/
510VLIB_CLI_COMMAND (adj_show_command, static) = {
511 .path = "show adj",
Neale Ranns9c6a6132017-02-21 05:33:14 -0800512 .short_help = "show adj [<adj_index>] [interface] [summary]",
Neale Rannsb80c5362016-10-08 13:03:40 +0100513 .function = adj_show,
514};
Neale Ranns9c6a6132017-02-21 05:33:14 -0800515
516/**
517 * @brief CLI invoked function to enable/disable per-adj counters
518 */
519static clib_error_t *
520adj_cli_counters_set (vlib_main_t * vm,
521 unformat_input_t * input,
522 vlib_cli_command_t * cmd)
523{
524 clib_error_t *error = NULL;
525 int enable = ~0;
526
527 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
528 {
529 if (unformat (input, "enable"))
530 enable = 1;
531 else if (unformat (input, "disable"))
532 enable = 0;
533 else
534 break;
535 }
536
537 if (enable != ~0)
538 {
539 /* user requested something sensible */
540 adj_per_adj_counters = enable;
541 }
542 else
543 {
544 error = clib_error_return (0, "specify 'enable' or 'disable'");
545 }
546
547 return (error);
548}
549
550/*?
551 * Enabe/disble per-adjacency counters. This is optional because it comes with
552 * a non-negligible performance cost.
553 ?*/
554VLIB_CLI_COMMAND (adj_cli_counters_set_command, static) = {
555 .path = "adjacency counters",
556 .short_help = "adjacency counters [enable|disable]",
557 .function = adj_cli_counters_set,
558};