http: ignore http_proxy env in tests
[vpp.git] / src / vlib / node_cli.c
1 /*
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:
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  * node_cli.c: node CLI
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <vlib/vlib.h>
44 #include <vlib/threads.h>
45 #include <vlib/stats/stats.h>
46 #include <math.h>
47
48 static int
49 node_cmp (void *a1, void *a2)
50 {
51   vlib_node_t **n1 = a1;
52   vlib_node_t **n2 = a2;
53
54   return vec_cmp (n1[0]->name, n2[0]->name);
55 }
56
57 static clib_error_t *
58 show_node_graph (vlib_main_t * vm,
59                  unformat_input_t * input, vlib_cli_command_t * cmd)
60 {
61   vlib_node_main_t *nm = &vm->node_main;
62   vlib_node_t *n;
63   u32 node_index;
64
65   vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, 0);
66
67   if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
68     {
69       n = vlib_get_node (vm, node_index);
70       vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, n);
71     }
72   else
73     {
74       vlib_node_t **nodes = vec_dup (nm->nodes);
75       uword i;
76
77       vec_sort_with_function (nodes, node_cmp);
78
79       for (i = 0; i < vec_len (nodes); i++)
80         vlib_cli_output (vm, "%U\n\n", format_vlib_node_graph, nm, nodes[i]);
81
82       vec_free (nodes);
83     }
84
85   return 0;
86 }
87
88 VLIB_CLI_COMMAND (show_node_graph_command, static) = {
89   .path = "show vlib graph",
90   .short_help = "Show packet processing node graph",
91   .function = show_node_graph,
92 };
93
94 static clib_error_t *
95 show_node_graphviz (vlib_main_t * vm,
96                     unformat_input_t * input, vlib_cli_command_t * cmd)
97 {
98   clib_error_t *error = 0;
99   vlib_node_main_t *nm = &vm->node_main;
100   vlib_node_t **nodes = nm->nodes;
101   u8 *chroot_filename = 0;
102   int fd;
103   uword *active = 0;
104   u32 i, j;
105   unformat_input_t _line_input, *line_input = &_line_input;
106   u8 filter = 0, calls_filter = 0, vectors_filter = 0, both = 0;
107
108   fd = -1;
109   /* Get a line of input. */
110   if (unformat_user (input, unformat_line_input, line_input))
111     {
112       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
113         {
114           if (unformat (line_input, "filter"))
115             filter = 1;
116           else if (unformat (line_input, "calls") && filter)
117             calls_filter = 1;
118           else if (unformat (line_input, "vectors") && filter)
119             vectors_filter = 1;
120           else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
121                              &chroot_filename))
122             {
123               fd = open ((char *) chroot_filename,
124                          O_CREAT | O_TRUNC | O_WRONLY, 0664);
125             }
126           else
127             return clib_error_return (0, "unknown input `%U'",
128                                       format_unformat_error, input);
129         }
130       unformat_free (line_input);
131     }
132
133   /*both is set to true if calls_filter and vectors_filter are, or neither */
134   both = filter & (!(calls_filter ^ vectors_filter));
135
136 #define format__(vm__, fd__, ...) \
137   if ((fd) < 0) \
138     { \
139       vlib_cli_output((vm__), ## __VA_ARGS__); \
140     } \
141   else \
142     { \
143       fdformat((fd__), ## __VA_ARGS__); \
144     }
145
146   format__ (vm, fd, "%s", "digraph {\n");
147
148   clib_bitmap_alloc (active, vec_len (nodes));
149   clib_bitmap_set_region (active, 0, 1, vec_len (nodes));
150   if (filter)
151     {
152       /*Adding the legend to the dot file*/
153       format__ (vm, fd, "%s",
154                 "  rankdir=\"LR\"\n  nodesep=2\n  subgraph cluster_legend {\n "
155                 "   label=\"Legend\"\n    style=\"solid\"\n    labelloc = b\n "
156                 "   subgraph cluster_colors {\n      label=\"Packets/Call\"\n "
157                 "     style=\"solid\"\n      labelloc = b\n");
158       format__ (vm, fd, "%s",
159                 "      0 [label=\"No packet\", fixedsize=true shape=circle "
160                 "width=2 fontsize=17]\n"
161                 "      1 [label=\"1-32\", fillcolor=1 style=filled "
162                 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
163                 "fontsize=17]\n"
164                 "      2 [label=\"33-64\", fillcolor=2 style=filled "
165                 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
166                 "fontsize=17]\n"
167                 "      3 [label=\"65-96\", fillcolor=3 style=filled "
168                 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
169                 "fontsize=17]\n"
170                 "      4 [label=\"97-128\", fillcolor=4 style=filled "
171                 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
172                 "fontsize=17]\n"
173                 "      5 [label=\"129-160\", fillcolor=5 style=filled "
174                 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
175                 "fontsize=17]\n"
176                 "      6 [label=\"161-192\", fillcolor=6 style=filled "
177                 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
178                 "fontsize=17]\n"
179                 "      7 [label=\"193-224\", fillcolor=7 style=filled "
180                 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
181                 "fontsize=17]\n"
182                 "      8 [label=\"224+\", fillcolor=8 style=filled "
183                 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
184                 "fontsize=17]\n");
185       format__ (vm, fd, "%s",
186                 "      0 -> 1 -> 2 -> 3 -> 4 [style=\"invis\",weight =100]\n  "
187                 "    5 -> 6 -> 7 -> 8 [style=\"invis\",weight =100]\n    }\n  "
188                 "  subgraph cluster_size {\n      label=\"Cycles/Packet\"\n   "
189                 "   style=\"solid\"\n      labelloc = b\n");
190       format__ (
191         vm, fd, "%s",
192         "      a[label=\"0\",fixedsize=true shape=circle width=1] \n"
193         "      b[label=\"10\",fixedsize=true shape=circle width=2 "
194         "fontsize=17]\n"
195         "      c[label=\"100\",fixedsize=true shape=circle width=3 "
196         "fontsize=20]\n"
197         "      d[label=\"1000\",fixedsize=true shape=circle width=4 "
198         "fontsize=23]\n"
199         "      a -> b -> c -> d  [style=\"invis\",weight =100]\n    }\n  }\n");
200
201       vlib_worker_thread_barrier_sync (vm);
202       for (j = 0; j < vec_len (nm->nodes); j++)
203         {
204           vlib_node_t *n;
205           n = nm->nodes[j];
206           vlib_node_sync_stats (vm, n);
207         }
208
209       /* Updating the stats for multithreaded use cases.
210        * We need to dup the nodes to sum the stats from all threads.*/
211       nodes = vec_dup (nm->nodes);
212       for (i = 1; i < vlib_get_n_threads (); i++)
213         {
214           vlib_node_main_t *nm_clone;
215           vlib_main_t *vm_clone;
216           vlib_node_runtime_t *rt;
217           vlib_node_t *n;
218
219           vm_clone = vlib_get_main_by_index (i);
220           nm_clone = &vm_clone->node_main;
221
222           for (j = 0; j < vec_len (nm_clone->nodes); j++)
223             {
224               n = nm_clone->nodes[j];
225
226               rt = vlib_node_get_runtime (vm_clone, n->index);
227               /* Sync the stats directly in the duplicated node.*/
228               vlib_node_runtime_sync_stats_node (nodes[j], rt, 0, 0, 0);
229             }
230         }
231       vlib_worker_thread_barrier_release (vm);
232
233       for (i = 0; i < vec_len (nodes); i++)
234         {
235           u64 p, c, l;
236           c = nodes[i]->stats_total.calls - nodes[i]->stats_last_clear.calls;
237           p =
238             nodes[i]->stats_total.vectors - nodes[i]->stats_last_clear.vectors;
239           l = nodes[i]->stats_total.clocks - nodes[i]->stats_last_clear.clocks;
240
241           if ((both && c > 0 && p > 0) || (calls_filter && c > 0) ||
242               (vectors_filter && p > 0))
243             {
244               format__ (vm, fd, "  \"%v\" [shape=circle", nodes[i]->name);
245               /*Changing the size and the font of nodes that receive packets*/
246               if (p > 0)
247                 {
248                   f64 x = (f64) l / (f64) p;
249                   f64 size_ratio = (1 + log10 (x + 1));
250                   format__ (vm, fd, " width=%.2f fontsize=%.2f fixedsize=true",
251                             size_ratio, 11 + 3 * size_ratio);
252                   /*Coloring nodes that are indeed called*/
253                   if (c > 0)
254                     {
255                       u64 color = ((p - 1) / (32 * c)) + 1;
256                       color = clib_min (color, 8);
257                       format__ (
258                         vm, fd,
259                         " fillcolor=%u style=filled colorscheme=ylorrd8",
260                         color);
261                     }
262                 }
263               format__ (vm, fd, "]\n");
264             }
265           else
266             {
267               clib_bitmap_set (active, i, 0);
268             }
269         }
270     }
271
272   clib_bitmap_foreach (i, active)
273     {
274       for (j = 0; j < vec_len (nodes[i]->next_nodes); j++)
275         {
276           if (nodes[i]->next_nodes[j] == VLIB_INVALID_NODE_INDEX)
277             continue;
278
279           if (!filter || clib_bitmap_get (active, nodes[i]->next_nodes[j]))
280             {
281               format__ (vm, fd, "  \"%v\" -> \"%v\"\n", nodes[i]->name,
282                         nodes[nodes[i]->next_nodes[j]]->name);
283             }
284         }
285     }
286
287   format__ (vm, fd, "}\n");
288
289   if (fd >= 0)
290     {
291       /*Dumping all the nodes saturates dot capacities to render a directed
292        * graph. In this case, prefer using he fdp command to generate an
293        * undirected graph. */
294       const char *soft = filter ? "dot" : "fdp";
295       vlib_cli_output (
296         vm, "vlib graph dumped into `%s'. Run eg. `%s -Tsvg -O %s'.",
297         chroot_filename, soft, chroot_filename);
298     }
299
300   clib_bitmap_free (active);
301   vec_free (chroot_filename);
302   if (filter)
303     vec_free (nodes);
304   if (fd >= 0)
305     close (fd);
306   return error;
307 }
308
309 /*?
310  * Dump dot files data to draw a graph of all the nodes.
311  * If the argument 'filter' is provided, only the active nodes (since the last
312  * "clear run" command) are selected and they are scaled and colored according
313  * to their utilization. You can choose to filter nodes that are called,
314  * nodes that receive vectors or both (default).
315  * The 'file' option allows to save data in a temp file.
316  *
317  * @cliexpar
318  * @clistart
319  * show vlib graphviz
320  * show vlib graphviz filter file tmpfile
321  * show vlib graphviz filter calls file tmpfile
322  * @cliend
323  * @cliexcmd{show vlib graphviz [filter][calls][vectors][file <filename>]}
324 ?*/
325 VLIB_CLI_COMMAND (show_node_graphviz_command, static) = {
326   .path = "show vlib graphviz",
327   .short_help = "Dump packet processing node graph as a graphviz dotfile",
328   .function = show_node_graphviz,
329   .is_mp_safe = 1,
330 };
331
332 static u8 *
333 format_vlib_node_state (u8 * s, va_list * va)
334 {
335   vlib_main_t *vm = va_arg (*va, vlib_main_t *);
336   vlib_node_t *n = va_arg (*va, vlib_node_t *);
337   char *state;
338
339   state = "active";
340   if (n->type == VLIB_NODE_TYPE_PROCESS)
341     {
342       vlib_process_t *p = vlib_get_process_from_node (vm, n);
343
344       switch (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
345                           | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
346         {
347         default:
348           if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
349             state = "done";
350           break;
351
352         case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
353           state = "time wait";
354           break;
355
356         case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
357           state = "event wait";
358           break;
359
360         case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
361           state =
362             "any wait";
363           break;
364         }
365     }
366   else if (n->type != VLIB_NODE_TYPE_INTERNAL)
367     {
368       state = "polling";
369       if (n->state == VLIB_NODE_STATE_DISABLED)
370         state = "disabled";
371       else if (n->state == VLIB_NODE_STATE_INTERRUPT)
372         state = "interrupt wait";
373     }
374
375   return format (s, "%s", state);
376 }
377
378 static u8 *
379 format_vlib_node_stats (u8 * s, va_list * va)
380 {
381   vlib_main_t *vm = va_arg (*va, vlib_main_t *);
382   vlib_node_t *n = va_arg (*va, vlib_node_t *);
383   int max = va_arg (*va, int);
384   f64 v;
385   u8 *ns;
386   u8 *misc_info = 0;
387   u64 c, p, l, d;
388   f64 x;
389   f64 maxc, maxcn;
390   u32 maxn;
391   u32 indent;
392
393   if (!n)
394     {
395       if (max)
396         s = format (s,
397                     "%=30s%=17s%=16s%=16s%=16s%=16s",
398                     "Name", "Max Node Clocks", "Vectors at Max",
399                     "Max Clocks", "Avg Clocks", "Avg Vectors/Call");
400       else
401         s = format (s,
402                     "%=30s%=12s%=16s%=16s%=16s%=16s%=16s",
403                     "Name", "State", "Calls", "Vectors", "Suspends",
404                     "Clocks", "Vectors/Call");
405       return s;
406     }
407
408   indent = format_get_indent (s);
409
410   l = n->stats_total.clocks - n->stats_last_clear.clocks;
411   c = n->stats_total.calls - n->stats_last_clear.calls;
412   p = n->stats_total.vectors - n->stats_last_clear.vectors;
413   d = n->stats_total.suspends - n->stats_last_clear.suspends;
414   maxc = (f64) n->stats_total.max_clock;
415   maxn = n->stats_total.max_clock_n;
416   if (n->stats_total.max_clock_n)
417     maxcn = (f64) n->stats_total.max_clock / (f64) maxn;
418   else
419     maxcn = 0.0;
420
421   /* Clocks per packet, per call or per suspend. */
422   x = 0;
423   if (p > 0)
424     x = (f64) l / (f64) p;
425   else if (c > 0)
426     x = (f64) l / (f64) c;
427   else if (d > 0)
428     x = (f64) l / (f64) d;
429
430   if (c > 0)
431     v = (double) p / (double) c;
432   else
433     v = 0;
434
435   if (n->type == VLIB_NODE_TYPE_PROCESS)
436     {
437       vlib_process_t *p = vlib_get_process_from_node (vm, n);
438
439       /* Show processes with events pending.  This helps spot bugs where events are not
440          being handled. */
441       if (!clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
442         misc_info = format (misc_info, "events pending, ");
443     }
444   ns = n->name;
445
446   if (max)
447     s = format (s, "%-30v%=17.2e%=16d%=16.2e%=16.2e%=16.2e",
448                 ns, maxc, maxn, maxcn, x, v);
449   else
450     s = format (s, "%-30v%=12U%16Ld%16Ld%16Ld%16.2e%16.2f", ns,
451                 format_vlib_node_state, vm, n, c, p, d, x, v);
452
453   if (ns != n->name)
454     vec_free (ns);
455
456   if (misc_info)
457     {
458       s = format (s, "\n%U%v", format_white_space, indent + 4, misc_info);
459       vec_free (misc_info);
460     }
461
462   return s;
463 }
464
465 static clib_error_t *
466 show_node_runtime (vlib_main_t * vm,
467                    unformat_input_t * input, vlib_cli_command_t * cmd)
468 {
469   vlib_node_main_t *nm = &vm->node_main;
470   vlib_node_t *n;
471   f64 time_now;
472   u32 node_index;
473   vlib_node_t ***node_dups = 0;
474   f64 *internal_node_vector_rates = 0;
475
476   time_now = vlib_time_now (vm);
477
478   if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
479     {
480       n = vlib_get_node (vm, node_index);
481       vlib_node_sync_stats (vm, n);
482       vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, 0, 0);
483       vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, n, 0);
484     }
485   else
486     {
487       vlib_node_t **nodes;
488       uword i, j;
489       f64 dt;
490       u64 n_input, n_output, n_drop, n_punt;
491       u64 n_clocks, l, v, c, d;
492       int brief = 1;
493       int summary = 0;
494       int max = 0;
495       vlib_main_t **stat_vms = 0, *stat_vm;
496
497       /* Suppress nodes with zero calls since last clear */
498       if (unformat (input, "brief") || unformat (input, "b"))
499         brief = 1;
500       if (unformat (input, "verbose") || unformat (input, "v"))
501         brief = 0;
502       if (unformat (input, "max") || unformat (input, "m"))
503         max = 1;
504       if (unformat (input, "summary") || unformat (input, "sum")
505           || unformat (input, "su"))
506         summary = 1;
507
508       for (i = 0; i < vlib_get_n_threads (); i++)
509         {
510           stat_vm = vlib_get_main_by_index (i);
511           if (stat_vm)
512             vec_add1 (stat_vms, stat_vm);
513         }
514
515       /*
516        * Barrier sync across stats scraping.
517        * Otherwise, the counts will be grossly inaccurate.
518        */
519       vlib_worker_thread_barrier_sync (vm);
520
521       for (j = 0; j < vec_len (stat_vms); j++)
522         {
523           stat_vm = stat_vms[j];
524           nm = &stat_vm->node_main;
525
526           for (i = 0; i < vec_len (nm->nodes); i++)
527             {
528               n = nm->nodes[i];
529               vlib_node_sync_stats (stat_vm, n);
530             }
531
532           nodes = vec_dup (nm->nodes);
533
534           vec_add1 (node_dups, nodes);
535           vec_add1 (internal_node_vector_rates,
536                     vlib_internal_node_vector_rate (stat_vm));
537         }
538       vlib_worker_thread_barrier_release (vm);
539
540
541       for (j = 0; j < vec_len (stat_vms); j++)
542         {
543           stat_vm = stat_vms[j];
544           nodes = node_dups[j];
545
546           vec_sort_with_function (nodes, node_cmp);
547
548           n_input = n_output = n_drop = n_punt = n_clocks = 0;
549           for (i = 0; i < vec_len (nodes); i++)
550             {
551               n = nodes[i];
552
553               l = n->stats_total.clocks - n->stats_last_clear.clocks;
554               n_clocks += l;
555
556               v = n->stats_total.vectors - n->stats_last_clear.vectors;
557
558               switch (n->type)
559                 {
560                 default:
561                   continue;
562
563                 case VLIB_NODE_TYPE_INTERNAL:
564                   n_output += (n->flags & VLIB_NODE_FLAG_IS_OUTPUT) ? v : 0;
565                   n_drop += (n->flags & VLIB_NODE_FLAG_IS_DROP) ? v : 0;
566                   n_punt += (n->flags & VLIB_NODE_FLAG_IS_PUNT) ? v : 0;
567                   if (n->flags & VLIB_NODE_FLAG_IS_HANDOFF)
568                     n_input += v;
569                   break;
570
571                 case VLIB_NODE_TYPE_INPUT:
572                   n_input += v;
573                   break;
574                 }
575             }
576
577           if (vlib_get_n_threads () > 1)
578             {
579               vlib_worker_thread_t *w = vlib_worker_threads + j;
580               if (j > 0)
581                 vlib_cli_output (vm, "---------------");
582
583               if (w->cpu_id > -1)
584                 vlib_cli_output (vm, "Thread %d %s (lcore %u)", j, w->name,
585                                  w->cpu_id);
586               else
587                 vlib_cli_output (vm, "Thread %d %s", j, w->name);
588             }
589
590           dt = time_now - nm->time_last_runtime_stats_clear;
591           vlib_cli_output (
592             vm,
593             "Time %.1f, %f sec internal node vector rate %.2f loops/sec %.2f\n"
594             "  vector rates in %.4e, out %.4e, drop %.4e, punt %.4e",
595             dt, vlib_stats_get_segment_update_rate (),
596             internal_node_vector_rates[j], stat_vm->loops_per_second,
597             (f64) n_input / dt, (f64) n_output / dt, (f64) n_drop / dt,
598             (f64) n_punt / dt);
599
600           if (summary == 0)
601             {
602               vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm,
603                                0, max);
604               for (i = 0; i < vec_len (nodes); i++)
605                 {
606                   c =
607                     nodes[i]->stats_total.calls -
608                     nodes[i]->stats_last_clear.calls;
609                   d =
610                     nodes[i]->stats_total.suspends -
611                     nodes[i]->stats_last_clear.suspends;
612                   if (c || d || !brief)
613                     {
614                       vlib_cli_output (vm, "%U", format_vlib_node_stats,
615                                        stat_vm, nodes[i], max);
616                     }
617                 }
618             }
619           vec_free (nodes);
620         }
621       vec_free (stat_vms);
622       vec_free (node_dups);
623       vec_free (internal_node_vector_rates);
624     }
625
626   return 0;
627 }
628
629 VLIB_CLI_COMMAND (show_node_runtime_command, static) = {
630   .path = "show runtime",
631   .short_help = "Show packet processing runtime",
632   .function = show_node_runtime,
633   .is_mp_safe = 1,
634 };
635
636 static clib_error_t *
637 clear_node_runtime (vlib_main_t * vm,
638                     unformat_input_t * input, vlib_cli_command_t * cmd)
639 {
640   vlib_node_main_t *nm;
641   vlib_node_t *n;
642   int i, j;
643   vlib_main_t **stat_vms = 0, *stat_vm;
644   vlib_node_runtime_t *r;
645
646   for (i = 0; i < vlib_get_n_threads (); i++)
647     {
648       stat_vm = vlib_get_main_by_index (i);
649       if (stat_vm)
650         vec_add1 (stat_vms, stat_vm);
651     }
652
653   vlib_worker_thread_barrier_sync (vm);
654
655   for (j = 0; j < vec_len (stat_vms); j++)
656     {
657       stat_vm = stat_vms[j];
658       nm = &stat_vm->node_main;
659
660       for (i = 0; i < vec_len (nm->nodes); i++)
661         {
662           n = nm->nodes[i];
663           vlib_node_sync_stats (stat_vm, n);
664           n->stats_last_clear = n->stats_total;
665
666           r = vlib_node_get_runtime (stat_vm, n->index);
667           r->max_clock = 0;
668         }
669       /* Note: input/output rates computed using vlib_global_main */
670       nm->time_last_runtime_stats_clear = vlib_time_now (vm);
671     }
672
673   vlib_stats_set_timestamp (STAT_COUNTER_LAST_STATS_CLEAR,
674                             vm->node_main.time_last_runtime_stats_clear);
675   vlib_worker_thread_barrier_release (vm);
676
677   vec_free (stat_vms);
678
679   return 0;
680 }
681
682 VLIB_CLI_COMMAND (clear_node_runtime_command, static) = {
683   .path = "clear runtime",
684   .short_help = "Clear packet processing runtime statistics",
685   .function = clear_node_runtime,
686 };
687
688 static clib_error_t *
689 show_node (vlib_main_t * vm, unformat_input_t * input,
690            vlib_cli_command_t * cmd)
691 {
692   unformat_input_t _line_input, *line_input = &_line_input;
693   clib_error_t *error = 0;
694   vlib_node_main_t *nm = &vm->node_main;
695   vlib_node_t *n;
696   u8 *s = 0, *s2 = 0;
697   u32 i, node_index = ~0, verbose = 0;
698   char *type_str;
699   u8 valid_node_name = 0;
700   u64 cl, ca, v;
701
702   if (!unformat_user (input, unformat_line_input, line_input))
703     return 0;
704
705   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
706     {
707       if (unformat (line_input, "index %u", &node_index))
708         ;
709       else if (unformat (line_input, "verbose"))
710         verbose = 1;
711       else
712         if (unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
713         valid_node_name = 1;
714       else if (!valid_node_name)
715         error = clib_error_return (0, "unknown node name: '%U'",
716                                    format_unformat_error, line_input);
717       else
718         error = clib_error_return (0, "unknown input '%U'",
719                                    format_unformat_error, line_input);
720
721       if (error)
722         break;
723     }
724
725   unformat_free (line_input);
726
727   if (error)
728     return error;
729
730   if (node_index >= vec_len (vm->node_main.nodes))
731     return clib_error_return (0, "please specify valid node");
732
733   n = vlib_get_node (vm, node_index);
734   vlib_node_sync_stats (vm, n);
735
736   switch (n->type)
737     {
738     case VLIB_NODE_TYPE_INTERNAL:
739       type_str = "internal";
740       break;
741     case VLIB_NODE_TYPE_INPUT:
742       type_str = "input";
743       break;
744     case VLIB_NODE_TYPE_PRE_INPUT:
745       type_str = "pre-input";
746       break;
747     case VLIB_NODE_TYPE_PROCESS:
748       type_str = "process";
749       break;
750     default:
751       type_str = "unknown";
752     }
753
754   if (n->sibling_of)
755     s = format (s, ", sibling-of %s", n->sibling_of);
756
757   vlib_cli_output (vm, "node %v, type %s, state %U, index %d%v\n",
758                    n->name, type_str, format_vlib_node_state, vm, n,
759                    n->index, s);
760   vec_reset_length (s);
761
762   if (n->node_fn_registrations)
763     {
764       vlib_node_fn_registration_t *fnr = n->node_fn_registrations;
765       vlib_node_fn_variant_t *v;
766       while (fnr)
767         {
768           v = vec_elt_at_index (vm->node_main.variants, fnr->march_variant);
769           if (vec_len (s) == 0)
770             s = format (s, "\n    %-15s  %=8s  %6s  %s", "Name", "Priority",
771                         "Active", "Description");
772           s = format (s, "\n    %-15s  %8d  %=6s  %s", v->suffix, v->priority,
773                       fnr->function == n->function ? "yes" : "", v->desc);
774           fnr = fnr->next_registration;
775         }
776     }
777   else
778     s = format (s, "\n    default only");
779   vlib_cli_output (vm, "  node function variants:%v\n", s);
780   vec_reset_length (s);
781
782   for (i = 0; i < vec_len (n->next_nodes); i++)
783     {
784       vlib_node_t *pn;
785       if (n->next_nodes[i] == VLIB_INVALID_NODE_INDEX)
786         continue;
787
788       pn = vec_elt (nm->nodes, n->next_nodes[i]);
789
790       if (vec_len (s) == 0)
791         s = format (s, "\n    %10s  %10s  %=30s %8s",
792                     "next-index", "node-index", "Node", "Vectors");
793
794       s = format (s, "\n    %=10u  %=10u  %=30v %=8llu", i, n->next_nodes[i],
795                   pn->name, vec_elt (n->n_vectors_by_next_node, i));
796     }
797
798   if (vec_len (s) == 0)
799     s = format (s, "\n    none");
800   vlib_cli_output (vm, "\n  next nodes:%v\n", s);
801   vec_reset_length (s);
802
803   if (n->type == VLIB_NODE_TYPE_INTERNAL)
804     {
805       int j = 0;
806       clib_bitmap_foreach (i, n->prev_node_bitmap)  {
807             vlib_node_t *pn = vlib_get_node (vm, i);
808             if (j++ % 3 == 0)
809               s = format (s, "\n    ");
810             s2 = format (s2, "%v (%u)", pn->name, i);
811             s = format (s, "%-35v", s2);
812             vec_reset_length (s2);
813           }
814
815       if (vec_len (s) == 0)
816         s = format (s, "\n    none");
817       vlib_cli_output (vm, "\n  known previous nodes:%v\n", s);
818       vec_reset_length (s);
819       vec_free (s2);
820     }
821
822   if (!verbose)
823     goto done;
824
825   s = format (s, "\n%8s %=12s %=12s %=12s %=12s %=12s\n", "Thread", "Calls",
826               "Clocks", "Vectors", "Max Clock", "Max Vectors");
827   for (i = 0; i < vlib_get_n_threads (); i++)
828     {
829       n = vlib_get_node (vlib_get_main_by_index (i), node_index);
830       vlib_node_sync_stats (vlib_get_main_by_index (i), n);
831
832       cl = n->stats_total.clocks - n->stats_last_clear.clocks;
833       ca = n->stats_total.calls - n->stats_last_clear.calls;
834       v = n->stats_total.vectors - n->stats_last_clear.vectors;
835
836       s = format (s, "%=8u %=12lu %=12lu %=12lu %=12u %=12u\n", i, ca, cl, v,
837                   n->stats_total.max_clock, n->stats_total.max_clock_n);
838     }
839
840   vlib_cli_output (vm, "%v", s);
841
842 done:
843
844   vec_free (s);
845   return 0;
846 }
847
848 VLIB_CLI_COMMAND (show_node_command, static) = {
849   .path = "show node",
850   .short_help = "show node [index] <node-name | node-index>",
851   .function = show_node,
852 };
853
854 static clib_error_t *
855 set_node_fn(vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
856 {
857   unformat_input_t _line_input, *line_input = &_line_input;
858   u32 node_index, march_variant;
859   vlib_node_t *n;
860   clib_error_t *err = 0;
861
862   if (!unformat_user (input, unformat_line_input, line_input))
863     return 0;
864
865   if (!unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
866     {
867       err = clib_error_return (0, "please specify valid node name");
868       goto done;
869     }
870
871   if (!unformat (line_input, "%U", unformat_vlib_node_variant, &march_variant))
872     {
873       err = clib_error_return (0, "please specify node function variant");
874       goto done;
875     }
876
877   n = vlib_get_node (vm, node_index);
878
879   if (n->node_fn_registrations == 0)
880     {
881       err = clib_error_return (0, "node doesn't have function variants");
882       goto done;
883     }
884
885   if (vlib_node_set_march_variant (vm, node_index, march_variant))
886     {
887       vlib_node_fn_variant_t *v;
888       v = vec_elt_at_index (vm->node_main.variants, march_variant);
889       err = clib_error_return (0, "node function variant '%s' not found",
890                                v->suffix);
891       goto done;
892     }
893
894
895 done:
896   unformat_free (line_input);
897   return err;
898 }
899
900 VLIB_CLI_COMMAND (set_node_fn_command, static) = {
901   .path = "set node function",
902   .short_help = "set node function <node-name> <variant-name>",
903   .function = set_node_fn,
904 };
905
906 /* Dummy function to get us linked in. */
907 void
908 vlib_node_cli_reference (void)
909 {
910 }
911
912 /*
913  * fd.io coding-style-patch-verification: ON
914  *
915  * Local Variables:
916  * eval: (c-set-style "gnu")
917  * End:
918  */