blob: 1ba0575f18210acb1e125f84fb61a946874819a6 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Benoît Ganne362456a2019-01-21 17:41:25 +010040#include <sys/types.h>
41#include <sys/stat.h>
42#include <fcntl.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070043#include <vlib/vlib.h>
44#include <vlib/threads.h>
45
Matus Fabiand2dc3df2015-12-14 10:31:33 -050046static int
Dave Barach9b8ffd92016-07-08 08:13:45 -040047node_cmp (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -050048{
Dave Barach9b8ffd92016-07-08 08:13:45 -040049 vlib_node_t **n1 = a1;
50 vlib_node_t **n2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -050051
52 return vec_cmp (n1[0]->name, n2[0]->name);
53}
54
Ed Warnickecb9cada2015-12-08 15:45:58 -070055static clib_error_t *
56show_node_graph (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -040057 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070058{
Dave Barach9b8ffd92016-07-08 08:13:45 -040059 vlib_node_main_t *nm = &vm->node_main;
60 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 u32 node_index;
62
63 vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, 0);
64
65 if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
66 {
67 n = vlib_get_node (vm, node_index);
68 vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, n);
69 }
70 else
71 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040072 vlib_node_t **nodes = vec_dup (nm->nodes);
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 uword i;
74
Matus Fabiand2dc3df2015-12-14 10:31:33 -050075 vec_sort_with_function (nodes, node_cmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
77 for (i = 0; i < vec_len (nodes); i++)
78 vlib_cli_output (vm, "%U\n\n", format_vlib_node_graph, nm, nodes[i]);
79
80 vec_free (nodes);
81 }
82
83 return 0;
84}
85
Dave Barach9b8ffd92016-07-08 08:13:45 -040086/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070087VLIB_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,
91};
Dave Barach9b8ffd92016-07-08 08:13:45 -040092/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Benoît Ganne362456a2019-01-21 17:41:25 +010094static clib_error_t *
95show_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 u8 *chroot_filename = 0;
101 int fd;
102 vlib_node_t **nodes = 0;
103 uword i, j;
104
105 if (!unformat_user (input, unformat_vlib_tmpfile, &chroot_filename))
106 {
107 fd = -1;
108 }
109 else
110 {
111 fd =
112 open ((char *) chroot_filename, O_CREAT | O_TRUNC | O_WRONLY, 0664);
113 }
114
115#define format__(vm__, fd__, ...) \
116 if ((fd) < 0) \
117 { \
118 vlib_cli_output((vm__), ## __VA_ARGS__); \
119 } \
120 else \
121 { \
122 fdformat((fd__), ## __VA_ARGS__); \
123 }
124
125 format__ (vm, fd, "%s", "digraph {\n");
126
127 nodes = vec_dup (nm->nodes);
128 vec_sort_with_function (nodes, node_cmp);
129
130 for (i = 0; i < vec_len (nodes); i++)
131 {
132 for (j = 0; j < vec_len (nodes[i]->next_nodes); j++)
133 {
134 vlib_node_t *x;
135
136 if (nodes[i]->next_nodes[j] == VLIB_INVALID_NODE_INDEX)
137 continue;
138
139 x = vec_elt (nm->nodes, nodes[i]->next_nodes[j]);
140 format__ (vm, fd, " \"%v\" -> \"%v\"\n", nodes[i]->name, x->name);
141 }
142 }
143
144 format__ (vm, fd, "%s", "}");
145
146 if (fd >= 0)
147 {
148 vlib_cli_output (vm,
149 "vlib graph dumped into `%s'. Run eg. `fdp -Tsvg -O %s'.",
150 chroot_filename, chroot_filename);
151 }
152
153 vec_free (nodes);
154 vec_free (chroot_filename);
155 vec_free (nodes);
156 if (fd >= 0)
157 close (fd);
158 return error;
159}
160
161/* *INDENT-OFF* */
162VLIB_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,
166};
167/* *INDENT-ON* */
168
Dave Barach9b8ffd92016-07-08 08:13:45 -0400169static u8 *
Damjan Marion69abe442018-08-27 22:43:23 +0200170format_vlib_node_state (u8 * s, va_list * va)
171{
172 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
173 vlib_node_t *n = va_arg (*va, vlib_node_t *);
174 char *state;
175
176 state = "active";
177 if (n->type == VLIB_NODE_TYPE_PROCESS)
178 {
179 vlib_process_t *p = vlib_get_process_from_node (vm, n);
180
181 switch (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
182 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
183 {
184 default:
185 if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
186 state = "done";
187 break;
188
189 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
190 state = "time wait";
191 break;
192
193 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
194 state = "event wait";
195 break;
196
197 case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
198 state =
199 "any wait";
200 break;
201 }
202 }
203 else if (n->type != VLIB_NODE_TYPE_INTERNAL)
204 {
205 state = "polling";
206 if (n->state == VLIB_NODE_STATE_DISABLED)
207 state = "disabled";
208 else if (n->state == VLIB_NODE_STATE_INTERRUPT)
209 state = "interrupt wait";
210 }
211
212 return format (s, "%s", state);
213}
214
215static u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400216format_vlib_node_stats (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400218 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
219 vlib_node_t *n = va_arg (*va, vlib_node_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220 int max = va_arg (*va, int);
221 f64 v;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400222 u8 *ns;
223 u8 *misc_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224 u64 c, p, l, d;
225 f64 x;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400226 f64 maxc, maxcn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227 u32 maxn;
Christophe Fontained3c008d2017-10-02 18:10:54 +0200228 u32 indent;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
Dave Barach9b8ffd92016-07-08 08:13:45 -0400230 if (!n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400232 if (max)
Dave Barach4d1a8662018-09-10 12:31:15 -0400233 s = format (s,
234 "%=30s%=17s%=16s%=16s%=16s%=16s",
235 "Name", "Max Node Clocks", "Vectors at Max",
236 "Max Clocks", "Avg Clocks", "Avg Vectors/Call");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 else
Dave Barach4d1a8662018-09-10 12:31:15 -0400238 s = format (s,
239 "%=30s%=12s%=16s%=16s%=16s%=16s%=16s",
240 "Name", "State", "Calls", "Vectors", "Suspends",
241 "Clocks", "Vectors/Call");
Dave Barach4d1a8662018-09-10 12:31:15 -0400242 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 }
244
245 indent = format_get_indent (s);
246
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;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400251 maxc = (f64) n->stats_total.max_clock;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 maxn = n->stats_total.max_clock_n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400253 if (n->stats_total.max_clock_n)
254 maxcn = (f64) n->stats_total.max_clock / (f64) maxn;
255 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256 maxcn = 0.0;
257
258 /* Clocks per packet, per call or per suspend. */
259 x = 0;
260 if (p > 0)
261 x = (f64) l / (f64) p;
262 else if (c > 0)
263 x = (f64) l / (f64) c;
264 else if (d > 0)
265 x = (f64) l / (f64) d;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400266
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 if (c > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400268 v = (double) p / (double) c;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269 else
270 v = 0;
271
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 if (n->type == VLIB_NODE_TYPE_PROCESS)
273 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400274 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275
276 /* Show processes with events pending. This helps spot bugs where events are not
Dave Barach9b8ffd92016-07-08 08:13:45 -0400277 being handled. */
278 if (!clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 misc_info = format (misc_info, "events pending, ");
Dave Barach9b8ffd92016-07-08 08:13:45 -0400280 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281 ns = n->name;
282
283 if (max)
284 s = format (s, "%-30v%=17.2e%=16d%=16.2e%=16.2e%=16.2e",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400285 ns, maxc, maxn, maxcn, x, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286 else
Damjan Marion69abe442018-08-27 22:43:23 +0200287 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289
290 if (ns != n->name)
291 vec_free (ns);
292
293 if (misc_info)
294 {
295 s = format (s, "\n%U%v", format_white_space, indent + 4, misc_info);
296 vec_free (misc_info);
297 }
298
299 return s;
300}
301
Dave Baracha8df85c2019-10-01 13:34:23 -0400302f64 vlib_get_stat_segment_update_rate (void) __attribute__ ((weak));
303f64
304vlib_get_stat_segment_update_rate (void)
305{
306 return 1e70;
307}
308
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309static clib_error_t *
310show_node_runtime (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400311 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400313 vlib_node_main_t *nm = &vm->node_main;
314 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 f64 time_now;
316 u32 node_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400317 vlib_node_t ***node_dups = 0;
Dave Baracha8df85c2019-10-01 13:34:23 -0400318 f64 *internal_node_vector_rates = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
320 time_now = vlib_time_now (vm);
321
322 if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
323 {
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);
328 }
329 else
330 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400331 vlib_node_t **nodes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 uword i, j;
333 f64 dt;
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;
337 int brief = 1;
Dave Barachac78f8a2019-10-04 09:59:00 -0400338 int summary = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339 int max = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400340 vlib_main_t **stat_vms = 0, *stat_vm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341
342 /* Suppress nodes with zero calls since last clear */
343 if (unformat (input, "brief") || unformat (input, "b"))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400344 brief = 1;
345 if (unformat (input, "verbose") || unformat (input, "v"))
346 brief = 0;
347 if (unformat (input, "max") || unformat (input, "m"))
348 max = 1;
Dave Barachac78f8a2019-10-04 09:59:00 -0400349 if (unformat (input, "summary") || unformat (input, "sum")
350 || unformat (input, "su"))
351 summary = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Dave Barach80f54e22017-03-08 19:08:56 -0500353 for (i = 0; i < vec_len (vlib_mains); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354 {
Dave Barach80f54e22017-03-08 19:08:56 -0500355 stat_vm = vlib_mains[i];
356 if (stat_vm)
357 vec_add1 (stat_vms, stat_vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400358 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 * Barrier sync across stats scraping.
362 * Otherwise, the counts will be grossly inaccurate.
363 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400364 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
366 for (j = 0; j < vec_len (stat_vms); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400367 {
368 stat_vm = stat_vms[j];
369 nm = &stat_vm->node_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
Dave Barach9b8ffd92016-07-08 08:13:45 -0400371 for (i = 0; i < vec_len (nm->nodes); i++)
372 {
373 n = nm->nodes[i];
374 vlib_node_sync_stats (stat_vm, n);
375 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376
Dave Barach9b8ffd92016-07-08 08:13:45 -0400377 nodes = vec_dup (nm->nodes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
Dave Barach9b8ffd92016-07-08 08:13:45 -0400379 vec_add1 (node_dups, nodes);
Dave Baracha8df85c2019-10-01 13:34:23 -0400380 vec_add1 (internal_node_vector_rates,
381 vlib_internal_node_vector_rate (stat_vm));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400382 }
383 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384
385
386 for (j = 0; j < vec_len (stat_vms); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400387 {
388 stat_vm = stat_vms[j];
389 nodes = node_dups[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Dave Barach9b8ffd92016-07-08 08:13:45 -0400391 vec_sort_with_function (nodes, node_cmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
Dave Barach9b8ffd92016-07-08 08:13:45 -0400393 n_input = n_output = n_drop = n_punt = n_clocks = 0;
394 n_internal_vectors = n_internal_calls = 0;
395 for (i = 0; i < vec_len (nodes); i++)
396 {
397 n = nodes[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
Dave Barach9b8ffd92016-07-08 08:13:45 -0400399 l = n->stats_total.clocks - n->stats_last_clear.clocks;
400 n_clocks += l;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401
Dave Barach9b8ffd92016-07-08 08:13:45 -0400402 v = n->stats_total.vectors - n->stats_last_clear.vectors;
403 c = n->stats_total.calls - n->stats_last_clear.calls;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404
Dave Barach9b8ffd92016-07-08 08:13:45 -0400405 switch (n->type)
406 {
407 default:
408 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
Dave Barach9b8ffd92016-07-08 08:13:45 -0400410 case VLIB_NODE_TYPE_INTERNAL:
411 n_output += (n->flags & VLIB_NODE_FLAG_IS_OUTPUT) ? v : 0;
412 n_drop += (n->flags & VLIB_NODE_FLAG_IS_DROP) ? v : 0;
413 n_punt += (n->flags & VLIB_NODE_FLAG_IS_PUNT) ? v : 0;
414 if (!(n->flags & VLIB_NODE_FLAG_IS_OUTPUT))
415 {
416 n_internal_vectors += v;
417 n_internal_calls += c;
418 }
419 if (n->flags & VLIB_NODE_FLAG_IS_HANDOFF)
420 n_input += v;
421 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Dave Barach9b8ffd92016-07-08 08:13:45 -0400423 case VLIB_NODE_TYPE_INPUT:
424 n_input += v;
425 break;
426 }
427 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
Dave Barach80f54e22017-03-08 19:08:56 -0500429 if (vec_len (vlib_mains) > 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400430 {
431 vlib_worker_thread_t *w = vlib_worker_threads + j;
432 if (j > 0)
433 vlib_cli_output (vm, "---------------");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200435 if (w->cpu_id > -1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400436 vlib_cli_output (vm, "Thread %d %s (lcore %u)", j, w->name,
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200437 w->cpu_id);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400438 else
439 vlib_cli_output (vm, "Thread %d %s", j, w->name);
440 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Dave Barach9b8ffd92016-07-08 08:13:45 -0400442 dt = time_now - nm->time_last_runtime_stats_clear;
443 vlib_cli_output
444 (vm,
Dave Barach000a0292020-02-17 17:07:12 -0500445 "Time %.1f, %f sec internal node vector rate %.2f loops/sec %.2f\n"
Dave Baracha8df85c2019-10-01 13:34:23 -0400446 " vector rates in %.4e, out %.4e, drop %.4e, punt %.4e",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400447 dt,
Dave Baracha8df85c2019-10-01 13:34:23 -0400448 vlib_get_stat_segment_update_rate (),
449 internal_node_vector_rates[j],
Dave Barach000a0292020-02-17 17:07:12 -0500450 stat_vm->loops_per_second,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400451 (f64) n_input / dt,
452 (f64) n_output / dt, (f64) n_drop / dt, (f64) n_punt / dt);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453
Dave Barachac78f8a2019-10-04 09:59:00 -0400454 if (summary == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400455 {
Dave Barachac78f8a2019-10-04 09:59:00 -0400456 vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm,
457 0, max);
458 for (i = 0; i < vec_len (nodes); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400459 {
Dave Barachac78f8a2019-10-04 09:59:00 -0400460 c =
461 nodes[i]->stats_total.calls -
462 nodes[i]->stats_last_clear.calls;
463 d =
464 nodes[i]->stats_total.suspends -
465 nodes[i]->stats_last_clear.suspends;
466 if (c || d || !brief)
467 {
468 vlib_cli_output (vm, "%U", format_vlib_node_stats,
469 stat_vm, nodes[i], max);
470 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400471 }
472 }
473 vec_free (nodes);
474 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475 vec_free (stat_vms);
476 vec_free (node_dups);
Dave Baracha8df85c2019-10-01 13:34:23 -0400477 vec_free (internal_node_vector_rates);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 }
479
480 return 0;
481}
482
Dave Barach9b8ffd92016-07-08 08:13:45 -0400483/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484VLIB_CLI_COMMAND (show_node_runtime_command, static) = {
485 .path = "show runtime",
486 .short_help = "Show packet processing runtime",
487 .function = show_node_runtime,
488 .is_mp_safe = 1,
489};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400490/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491
492static clib_error_t *
493clear_node_runtime (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400494 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400496 vlib_node_main_t *nm;
497 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 int i, j;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400499 vlib_main_t **stat_vms = 0, *stat_vm;
500 vlib_node_runtime_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501
Dave Barach80f54e22017-03-08 19:08:56 -0500502 for (i = 0; i < vec_len (vlib_mains); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 {
Dave Barach80f54e22017-03-08 19:08:56 -0500504 stat_vm = vlib_mains[i];
505 if (stat_vm)
506 vec_add1 (stat_vms, stat_vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400508
509 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510
511 for (j = 0; j < vec_len (stat_vms); j++)
512 {
513 stat_vm = stat_vms[j];
514 nm = &stat_vm->node_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
Dave Barach9b8ffd92016-07-08 08:13:45 -0400516 for (i = 0; i < vec_len (nm->nodes); i++)
517 {
518 n = nm->nodes[i];
519 vlib_node_sync_stats (stat_vm, n);
520 n->stats_last_clear = n->stats_total;
521
522 r = vlib_node_get_runtime (stat_vm, n->index);
523 r->max_clock = 0;
524 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 /* Note: input/output rates computed using vlib_global_main */
526 nm->time_last_runtime_stats_clear = vlib_time_now (vm);
527 }
528
Dave Barach9b8ffd92016-07-08 08:13:45 -0400529 vlib_worker_thread_barrier_release (vm);
530
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 vec_free (stat_vms);
532
533 return 0;
534}
535
Dave Barach9b8ffd92016-07-08 08:13:45 -0400536/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537VLIB_CLI_COMMAND (clear_node_runtime_command, static) = {
538 .path = "clear runtime",
539 .short_help = "Clear packet processing runtime statistics",
540 .function = clear_node_runtime,
541};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400542/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Damjan Marion69abe442018-08-27 22:43:23 +0200544static clib_error_t *
545show_node (vlib_main_t * vm, unformat_input_t * input,
546 vlib_cli_command_t * cmd)
547{
548 unformat_input_t _line_input, *line_input = &_line_input;
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700549 clib_error_t *error = 0;
Damjan Marion69abe442018-08-27 22:43:23 +0200550 vlib_node_main_t *nm = &vm->node_main;
551 vlib_node_t *n;
552 u8 *s = 0, *s2 = 0;
553 u32 i, node_index = ~0;
554 char *type_str;
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700555 u8 valid_node_name = 0;
Damjan Marion69abe442018-08-27 22:43:23 +0200556
557 if (!unformat_user (input, unformat_line_input, line_input))
558 return 0;
559
560 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
561 {
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700562 if (unformat (line_input, "index %u", &node_index))
Damjan Marion69abe442018-08-27 22:43:23 +0200563 ;
564 else
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700565 if (unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
566 valid_node_name = 1;
567 else if (!valid_node_name)
568 error = clib_error_return (0, "unknown node name: '%U'",
569 format_unformat_error, line_input);
570 else
571 error = clib_error_return (0, "unknown input '%U'",
572 format_unformat_error, line_input);
Filip Tehlarbe0ffbc2019-07-09 13:52:26 +0000573
574 if (error)
575 break;
Damjan Marion69abe442018-08-27 22:43:23 +0200576 }
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700577
Damjan Marion69abe442018-08-27 22:43:23 +0200578 unformat_free (line_input);
579
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700580 if (error)
581 return error;
582
Damjan Marion69abe442018-08-27 22:43:23 +0200583 if (node_index >= vec_len (vm->node_main.nodes))
584 return clib_error_return (0, "please specify valid node");
585
586 n = vlib_get_node (vm, node_index);
587 vlib_node_sync_stats (vm, n);
588
589 switch (n->type)
590 {
591 case VLIB_NODE_TYPE_INTERNAL:
592 type_str = "internal";
593 break;
594 case VLIB_NODE_TYPE_INPUT:
595 type_str = "input";
596 break;
597 case VLIB_NODE_TYPE_PRE_INPUT:
598 type_str = "pre-input";
599 break;
600 case VLIB_NODE_TYPE_PROCESS:
601 type_str = "process";
602 break;
603 default:
604 type_str = "unknown";
605 }
606
607 if (n->sibling_of)
608 s = format (s, ", sibling-of %s", n->sibling_of);
609
Benoît Ganne62d9fda2019-12-16 15:49:47 +0100610 vlib_cli_output (vm, "node %v, type %s, state %U, index %d%v\n",
Damjan Marion69abe442018-08-27 22:43:23 +0200611 n->name, type_str, format_vlib_node_state, vm, n,
612 n->index, s);
613 vec_reset_length (s);
614
615 if (n->node_fn_registrations)
616 {
617 vlib_node_fn_registration_t *fnr = n->node_fn_registrations;
618 while (fnr)
619 {
620 if (vec_len (s) == 0)
621 s = format (s, "\n %-15s %=8s %6s",
622 "Name", "Priority", "Active");
623 s = format (s, "\n %-15s %=8u %=6s", fnr->name, fnr->priority,
624 fnr->function == n->function ? "yes" : "");
625 fnr = fnr->next_registration;
626 }
627 }
628 else
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700629 s = format (s, "\n default only");
Damjan Marion69abe442018-08-27 22:43:23 +0200630 vlib_cli_output (vm, " node function variants:%v\n", s);
631 vec_reset_length (s);
632
633 for (i = 0; i < vec_len (n->next_nodes); i++)
634 {
635 vlib_node_t *pn;
636 if (n->next_nodes[i] == VLIB_INVALID_NODE_INDEX)
637 continue;
638
639 pn = vec_elt (nm->nodes, n->next_nodes[i]);
640
641 if (vec_len (s) == 0)
642 s = format (s, "\n %10s %10s %=30s %8s",
643 "next-index", "node-index", "Node", "Vectors");
644
645 s = format (s, "\n %=10u %=10u %-30v %=8llu", i, n->next_nodes[i],
646 pn->name, vec_elt (n->n_vectors_by_next_node, i));
647 }
648
649 if (vec_len (s) == 0)
650 s = format (s, "\n none");
651 vlib_cli_output (vm, "\n next nodes:%v\n", s);
652 vec_reset_length (s);
653
654 if (n->type == VLIB_NODE_TYPE_INTERNAL)
655 {
656 int j = 0;
657 /* *INDENT-OFF* */
658 clib_bitmap_foreach (i, n->prev_node_bitmap, ({
659 vlib_node_t *pn = vlib_get_node (vm, i);
660 if (j++ % 3 == 0)
661 s = format (s, "\n ");
662 s2 = format (s2, "%v (%u)", pn->name, i);
663 s = format (s, "%-35v", s2);
664 vec_reset_length (s2);
665 }));
666 /* *INDENT-ON* */
667
668 if (vec_len (s) == 0)
669 s = format (s, "\n none");
670 vlib_cli_output (vm, "\n known previous nodes:%v\n", s);
671 vec_reset_length (s);
672 }
673
674 vec_free (s);
675 vec_free (s2);
676 return 0;
677}
678
679/* *INDENT-OFF* */
680VLIB_CLI_COMMAND (show_node_command, static) = {
681 .path = "show node",
682 .short_help = "show node [index] <node-name | node-index>",
683 .function = show_node,
684};
685
686static clib_error_t *
687set_node_fn(vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
688{
689 unformat_input_t _line_input, *line_input = &_line_input;
690 u32 node_index;
691 vlib_node_t *n;
692 clib_error_t *err = 0;
693 vlib_node_fn_registration_t *fnr;
694 u8 *variant = 0;
695
696 if (!unformat_user (input, unformat_line_input, line_input))
697 return 0;
698
699 if (!unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
700 {
701 err = clib_error_return (0, "please specify valid node name");
702 goto done;
703 }
704
705 if (!unformat (line_input, "%s", &variant))
706 {
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700707 err = clib_error_return (0, "please specify node functional variant");
Damjan Marion69abe442018-08-27 22:43:23 +0200708 goto done;
709 }
710
711 n = vlib_get_node (vm, node_index);
712
713 if (n->node_fn_registrations == 0)
714 {
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700715 err = clib_error_return (0, "node doesn't have functional variants");
Damjan Marion69abe442018-08-27 22:43:23 +0200716 goto done;
717 }
718
719 fnr = n->node_fn_registrations;
720 vec_add1 (variant, 0);
721
722 while (fnr)
723 {
724 if (!strncmp (fnr->name, (char *) variant, vec_len (variant) - 1))
725 {
726 int i;
727
728 n->function = fnr->function;
729
730 for (i = 0; i < vec_len (vlib_mains); i++)
731 {
732 vlib_node_runtime_t *nrt;
733 nrt = vlib_node_get_runtime (vlib_mains[i], n->index);
734 nrt->function = fnr->function;
735 }
736 goto done;
737 }
738 fnr = fnr->next_registration;
739 }
740
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700741 err = clib_error_return (0, "node functional variant '%s' not found", variant);
Damjan Marion69abe442018-08-27 22:43:23 +0200742
743done:
744 vec_free (variant);
745 unformat_free (line_input);
746 return err;
747}
748
749/* *INDENT-OFF* */
750VLIB_CLI_COMMAND (set_node_fn_command, static) = {
751 .path = "set node function",
752 .short_help = "set node function <node-name> <variant-name>",
753 .function = set_node_fn,
754};
755/* *INDENT-ON* */
756
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757/* Dummy function to get us linked in. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400758void
759vlib_node_cli_reference (void)
760{
761}
762
763/*
764 * fd.io coding-style-patch-verification: ON
765 *
766 * Local Variables:
767 * eval: (c-set-style "gnu")
768 * End:
769 */