2 * Copyright (c) 2015 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * node_cli.c: node CLI
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <sys/types.h>
43 #include <vlib/vlib.h>
44 #include <vlib/threads.h>
47 node_cmp (void *a1, void *a2)
49 vlib_node_t **n1 = a1;
50 vlib_node_t **n2 = a2;
52 return vec_cmp (n1[0]->name, n2[0]->name);
56 show_node_graph (vlib_main_t * vm,
57 unformat_input_t * input, vlib_cli_command_t * cmd)
59 vlib_node_main_t *nm = &vm->node_main;
63 vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, 0);
65 if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
67 n = vlib_get_node (vm, node_index);
68 vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, n);
72 vlib_node_t **nodes = vec_dup (nm->nodes);
75 vec_sort_with_function (nodes, node_cmp);
77 for (i = 0; i < vec_len (nodes); i++)
78 vlib_cli_output (vm, "%U\n\n", format_vlib_node_graph, nm, nodes[i]);
87 VLIB_CLI_COMMAND (show_node_graph_command, static) = {
88 .path = "show vlib graph",
89 .short_help = "Show packet processing node graph",
90 .function = show_node_graph,
95 show_node_graphviz (vlib_main_t * vm,
96 unformat_input_t * input, vlib_cli_command_t * cmd)
98 clib_error_t *error = 0;
99 vlib_node_main_t *nm = &vm->node_main;
100 u8 *chroot_filename = 0;
102 vlib_node_t **nodes = 0;
105 if (!unformat_user (input, unformat_vlib_tmpfile, &chroot_filename))
112 open ((char *) chroot_filename, O_CREAT | O_TRUNC | O_WRONLY, 0664);
115 #define format__(vm__, fd__, ...) \
118 vlib_cli_output((vm__), ## __VA_ARGS__); \
122 fdformat((fd__), ## __VA_ARGS__); \
125 format__ (vm, fd, "%s", "digraph {\n");
127 nodes = vec_dup (nm->nodes);
128 vec_sort_with_function (nodes, node_cmp);
130 for (i = 0; i < vec_len (nodes); i++)
132 for (j = 0; j < vec_len (nodes[i]->next_nodes); j++)
136 if (nodes[i]->next_nodes[j] == VLIB_INVALID_NODE_INDEX)
139 x = vec_elt (nm->nodes, nodes[i]->next_nodes[j]);
140 format__ (vm, fd, " \"%v\" -> \"%v\"\n", nodes[i]->name, x->name);
144 format__ (vm, fd, "%s", "}");
149 "vlib graph dumped into `%s'. Run eg. `fdp -Tsvg -O %s'.",
150 chroot_filename, chroot_filename);
154 vec_free (chroot_filename);
162 VLIB_CLI_COMMAND (show_node_graphviz_command, static) = {
163 .path = "show vlib graphviz",
164 .short_help = "Dump packet processing node graph as a graphviz dotfile",
165 .function = show_node_graphviz,
170 format_vlib_node_state (u8 * s, va_list * va)
172 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
173 vlib_node_t *n = va_arg (*va, vlib_node_t *);
177 if (n->type == VLIB_NODE_TYPE_PROCESS)
179 vlib_process_t *p = vlib_get_process_from_node (vm, n);
181 switch (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
182 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
185 if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
189 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
193 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
194 state = "event wait";
197 case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
203 else if (n->type != VLIB_NODE_TYPE_INTERNAL)
206 if (n->state == VLIB_NODE_STATE_DISABLED)
208 else if (n->state == VLIB_NODE_STATE_INTERRUPT)
209 state = "interrupt wait";
212 return format (s, "%s", state);
216 format_vlib_node_stats (u8 * s, va_list * va)
218 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
219 vlib_node_t *n = va_arg (*va, vlib_node_t *);
220 int max = va_arg (*va, int);
234 "%=30s%=17s%=16s%=16s%=16s%=16s",
235 "Name", "Max Node Clocks", "Vectors at Max",
236 "Max Clocks", "Avg Clocks", "Avg Vectors/Call");
239 "%=30s%=12s%=16s%=16s%=16s%=16s%=16s",
240 "Name", "State", "Calls", "Vectors", "Suspends",
241 "Clocks", "Vectors/Call");
245 indent = format_get_indent (s);
247 l = n->stats_total.clocks - n->stats_last_clear.clocks;
248 c = n->stats_total.calls - n->stats_last_clear.calls;
249 p = n->stats_total.vectors - n->stats_last_clear.vectors;
250 d = n->stats_total.suspends - n->stats_last_clear.suspends;
251 maxc = (f64) n->stats_total.max_clock;
252 maxn = n->stats_total.max_clock_n;
253 if (n->stats_total.max_clock_n)
254 maxcn = (f64) n->stats_total.max_clock / (f64) maxn;
258 /* Clocks per packet, per call or per suspend. */
261 x = (f64) l / (f64) p;
263 x = (f64) l / (f64) c;
265 x = (f64) l / (f64) d;
268 v = (double) p / (double) c;
272 if (n->type == VLIB_NODE_TYPE_PROCESS)
274 vlib_process_t *p = vlib_get_process_from_node (vm, n);
276 /* Show processes with events pending. This helps spot bugs where events are not
278 if (!clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
279 misc_info = format (misc_info, "events pending, ");
284 s = format (s, "%-30v%=17.2e%=16d%=16.2e%=16.2e%=16.2e",
285 ns, maxc, maxn, maxcn, x, v);
287 s = format (s, "%-30v%=12U%16Ld%16Ld%16Ld%16.2e%16.2f", ns,
288 format_vlib_node_state, vm, n, c, p, d, x, v);
295 s = format (s, "\n%U%v", format_white_space, indent + 4, misc_info);
296 vec_free (misc_info);
302 f64 vlib_get_stat_segment_update_rate (void) __attribute__ ((weak));
304 vlib_get_stat_segment_update_rate (void)
309 static clib_error_t *
310 show_node_runtime (vlib_main_t * vm,
311 unformat_input_t * input, vlib_cli_command_t * cmd)
313 vlib_node_main_t *nm = &vm->node_main;
317 vlib_node_t ***node_dups = 0;
318 f64 *internal_node_vector_rates = 0;
320 time_now = vlib_time_now (vm);
322 if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
324 n = vlib_get_node (vm, node_index);
325 vlib_node_sync_stats (vm, n);
326 vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, 0, 0);
327 vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, n, 0);
334 u64 n_input, n_output, n_drop, n_punt;
335 u64 n_internal_vectors, n_internal_calls;
336 u64 n_clocks, l, v, c, d;
339 vlib_main_t **stat_vms = 0, *stat_vm;
341 /* Suppress nodes with zero calls since last clear */
342 if (unformat (input, "brief") || unformat (input, "b"))
344 if (unformat (input, "verbose") || unformat (input, "v"))
346 if (unformat (input, "max") || unformat (input, "m"))
349 for (i = 0; i < vec_len (vlib_mains); i++)
351 stat_vm = vlib_mains[i];
353 vec_add1 (stat_vms, stat_vm);
357 * Barrier sync across stats scraping.
358 * Otherwise, the counts will be grossly inaccurate.
360 vlib_worker_thread_barrier_sync (vm);
362 for (j = 0; j < vec_len (stat_vms); j++)
364 stat_vm = stat_vms[j];
365 nm = &stat_vm->node_main;
367 for (i = 0; i < vec_len (nm->nodes); i++)
370 vlib_node_sync_stats (stat_vm, n);
373 nodes = vec_dup (nm->nodes);
375 vec_add1 (node_dups, nodes);
376 vec_add1 (internal_node_vector_rates,
377 vlib_internal_node_vector_rate (stat_vm));
379 vlib_worker_thread_barrier_release (vm);
382 for (j = 0; j < vec_len (stat_vms); j++)
384 stat_vm = stat_vms[j];
385 nodes = node_dups[j];
387 vec_sort_with_function (nodes, node_cmp);
389 n_input = n_output = n_drop = n_punt = n_clocks = 0;
390 n_internal_vectors = n_internal_calls = 0;
391 for (i = 0; i < vec_len (nodes); i++)
395 l = n->stats_total.clocks - n->stats_last_clear.clocks;
398 v = n->stats_total.vectors - n->stats_last_clear.vectors;
399 c = n->stats_total.calls - n->stats_last_clear.calls;
406 case VLIB_NODE_TYPE_INTERNAL:
407 n_output += (n->flags & VLIB_NODE_FLAG_IS_OUTPUT) ? v : 0;
408 n_drop += (n->flags & VLIB_NODE_FLAG_IS_DROP) ? v : 0;
409 n_punt += (n->flags & VLIB_NODE_FLAG_IS_PUNT) ? v : 0;
410 if (!(n->flags & VLIB_NODE_FLAG_IS_OUTPUT))
412 n_internal_vectors += v;
413 n_internal_calls += c;
415 if (n->flags & VLIB_NODE_FLAG_IS_HANDOFF)
419 case VLIB_NODE_TYPE_INPUT:
425 if (vec_len (vlib_mains) > 1)
427 vlib_worker_thread_t *w = vlib_worker_threads + j;
429 vlib_cli_output (vm, "---------------");
432 vlib_cli_output (vm, "Thread %d %s (lcore %u)", j, w->name,
435 vlib_cli_output (vm, "Thread %d %s", j, w->name);
438 dt = time_now - nm->time_last_runtime_stats_clear;
441 "Time %.1f, %f sec internal node vector rate %.2f \n"
442 " vector rates in %.4e, out %.4e, drop %.4e, punt %.4e",
444 vlib_get_stat_segment_update_rate (),
445 internal_node_vector_rates[j],
447 (f64) n_output / dt, (f64) n_drop / dt, (f64) n_punt / dt);
449 vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm, 0, max);
450 for (i = 0; i < vec_len (nodes); i++)
453 nodes[i]->stats_total.calls -
454 nodes[i]->stats_last_clear.calls;
456 nodes[i]->stats_total.suspends -
457 nodes[i]->stats_last_clear.suspends;
458 if (c || d || !brief)
460 vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm,
467 vec_free (node_dups);
468 vec_free (internal_node_vector_rates);
475 VLIB_CLI_COMMAND (show_node_runtime_command, static) = {
476 .path = "show runtime",
477 .short_help = "Show packet processing runtime",
478 .function = show_node_runtime,
483 static clib_error_t *
484 clear_node_runtime (vlib_main_t * vm,
485 unformat_input_t * input, vlib_cli_command_t * cmd)
487 vlib_node_main_t *nm;
490 vlib_main_t **stat_vms = 0, *stat_vm;
491 vlib_node_runtime_t *r;
493 for (i = 0; i < vec_len (vlib_mains); i++)
495 stat_vm = vlib_mains[i];
497 vec_add1 (stat_vms, stat_vm);
500 vlib_worker_thread_barrier_sync (vm);
502 for (j = 0; j < vec_len (stat_vms); j++)
504 stat_vm = stat_vms[j];
505 nm = &stat_vm->node_main;
507 for (i = 0; i < vec_len (nm->nodes); i++)
510 vlib_node_sync_stats (stat_vm, n);
511 n->stats_last_clear = n->stats_total;
513 r = vlib_node_get_runtime (stat_vm, n->index);
516 /* Note: input/output rates computed using vlib_global_main */
517 nm->time_last_runtime_stats_clear = vlib_time_now (vm);
520 vlib_worker_thread_barrier_release (vm);
528 VLIB_CLI_COMMAND (clear_node_runtime_command, static) = {
529 .path = "clear runtime",
530 .short_help = "Clear packet processing runtime statistics",
531 .function = clear_node_runtime,
535 static clib_error_t *
536 show_node (vlib_main_t * vm, unformat_input_t * input,
537 vlib_cli_command_t * cmd)
539 unformat_input_t _line_input, *line_input = &_line_input;
540 clib_error_t *error = 0;
541 vlib_node_main_t *nm = &vm->node_main;
544 u32 i, node_index = ~0;
546 u8 valid_node_name = 0;
548 if (!unformat_user (input, unformat_line_input, line_input))
551 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
553 if (unformat (line_input, "index %u", &node_index))
556 if (unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
558 else if (!valid_node_name)
559 error = clib_error_return (0, "unknown node name: '%U'",
560 format_unformat_error, line_input);
562 error = clib_error_return (0, "unknown input '%U'",
563 format_unformat_error, line_input);
569 unformat_free (line_input);
574 if (node_index >= vec_len (vm->node_main.nodes))
575 return clib_error_return (0, "please specify valid node");
577 n = vlib_get_node (vm, node_index);
578 vlib_node_sync_stats (vm, n);
582 case VLIB_NODE_TYPE_INTERNAL:
583 type_str = "internal";
585 case VLIB_NODE_TYPE_INPUT:
588 case VLIB_NODE_TYPE_PRE_INPUT:
589 type_str = "pre-input";
591 case VLIB_NODE_TYPE_PROCESS:
592 type_str = "process";
595 type_str = "unknown";
599 s = format (s, ", sibling-of %s", n->sibling_of);
601 vlib_cli_output (vm, "node %s, type %s, state %U, index %d%v\n",
602 n->name, type_str, format_vlib_node_state, vm, n,
604 vec_reset_length (s);
606 if (n->node_fn_registrations)
608 vlib_node_fn_registration_t *fnr = n->node_fn_registrations;
611 if (vec_len (s) == 0)
612 s = format (s, "\n %-15s %=8s %6s",
613 "Name", "Priority", "Active");
614 s = format (s, "\n %-15s %=8u %=6s", fnr->name, fnr->priority,
615 fnr->function == n->function ? "yes" : "");
616 fnr = fnr->next_registration;
620 s = format (s, "\n default only");
621 vlib_cli_output (vm, " node function variants:%v\n", s);
622 vec_reset_length (s);
624 for (i = 0; i < vec_len (n->next_nodes); i++)
627 if (n->next_nodes[i] == VLIB_INVALID_NODE_INDEX)
630 pn = vec_elt (nm->nodes, n->next_nodes[i]);
632 if (vec_len (s) == 0)
633 s = format (s, "\n %10s %10s %=30s %8s",
634 "next-index", "node-index", "Node", "Vectors");
636 s = format (s, "\n %=10u %=10u %-30v %=8llu", i, n->next_nodes[i],
637 pn->name, vec_elt (n->n_vectors_by_next_node, i));
640 if (vec_len (s) == 0)
641 s = format (s, "\n none");
642 vlib_cli_output (vm, "\n next nodes:%v\n", s);
643 vec_reset_length (s);
645 if (n->type == VLIB_NODE_TYPE_INTERNAL)
649 clib_bitmap_foreach (i, n->prev_node_bitmap, ({
650 vlib_node_t *pn = vlib_get_node (vm, i);
652 s = format (s, "\n ");
653 s2 = format (s2, "%v (%u)", pn->name, i);
654 s = format (s, "%-35v", s2);
655 vec_reset_length (s2);
659 if (vec_len (s) == 0)
660 s = format (s, "\n none");
661 vlib_cli_output (vm, "\n known previous nodes:%v\n", s);
662 vec_reset_length (s);
671 VLIB_CLI_COMMAND (show_node_command, static) = {
673 .short_help = "show node [index] <node-name | node-index>",
674 .function = show_node,
677 static clib_error_t *
678 set_node_fn(vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
680 unformat_input_t _line_input, *line_input = &_line_input;
683 clib_error_t *err = 0;
684 vlib_node_fn_registration_t *fnr;
687 if (!unformat_user (input, unformat_line_input, line_input))
690 if (!unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
692 err = clib_error_return (0, "please specify valid node name");
696 if (!unformat (line_input, "%s", &variant))
698 err = clib_error_return (0, "please specify node functional variant");
702 n = vlib_get_node (vm, node_index);
704 if (n->node_fn_registrations == 0)
706 err = clib_error_return (0, "node doesn't have functional variants");
710 fnr = n->node_fn_registrations;
711 vec_add1 (variant, 0);
715 if (!strncmp (fnr->name, (char *) variant, vec_len (variant) - 1))
719 n->function = fnr->function;
721 for (i = 0; i < vec_len (vlib_mains); i++)
723 vlib_node_runtime_t *nrt;
724 nrt = vlib_node_get_runtime (vlib_mains[i], n->index);
725 nrt->function = fnr->function;
729 fnr = fnr->next_registration;
732 err = clib_error_return (0, "node functional variant '%s' not found", variant);
736 unformat_free (line_input);
741 VLIB_CLI_COMMAND (set_node_fn_command, static) = {
742 .path = "set node function",
743 .short_help = "set node function <node-name> <variant-name>",
744 .function = set_node_fn,
748 /* Dummy function to get us linked in. */
750 vlib_node_cli_reference (void)
755 * fd.io coding-style-patch-verification: ON
758 * eval: (c-set-style "gnu")