blob: 77fef775a2fb90c3b31cf5ce4ab725f77454cf7f [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>
Arthur de Kerhor156158f2021-02-18 03:09:42 -080045#include <math.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
Matus Fabiand2dc3df2015-12-14 10:31:33 -050047static int
Dave Barach9b8ffd92016-07-08 08:13:45 -040048node_cmp (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -050049{
Dave Barach9b8ffd92016-07-08 08:13:45 -040050 vlib_node_t **n1 = a1;
51 vlib_node_t **n2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -050052
53 return vec_cmp (n1[0]->name, n2[0]->name);
54}
55
Ed Warnickecb9cada2015-12-08 15:45:58 -070056static clib_error_t *
57show_node_graph (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -040058 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
Dave Barach9b8ffd92016-07-08 08:13:45 -040060 vlib_node_main_t *nm = &vm->node_main;
61 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 u32 node_index;
63
64 vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, 0);
65
66 if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
67 {
68 n = vlib_get_node (vm, node_index);
69 vlib_cli_output (vm, "%U\n", format_vlib_node_graph, nm, n);
70 }
71 else
72 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040073 vlib_node_t **nodes = vec_dup (nm->nodes);
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 uword i;
75
Matus Fabiand2dc3df2015-12-14 10:31:33 -050076 vec_sort_with_function (nodes, node_cmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
78 for (i = 0; i < vec_len (nodes); i++)
79 vlib_cli_output (vm, "%U\n\n", format_vlib_node_graph, nm, nodes[i]);
80
81 vec_free (nodes);
82 }
83
84 return 0;
85}
86
Dave Barach9b8ffd92016-07-08 08:13:45 -040087/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070088VLIB_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};
Dave Barach9b8ffd92016-07-08 08:13:45 -040093/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Benoît Ganne362456a2019-01-21 17:41:25 +010095static clib_error_t *
96show_node_graphviz (vlib_main_t * vm,
97 unformat_input_t * input, vlib_cli_command_t * cmd)
98{
99 clib_error_t *error = 0;
100 vlib_node_main_t *nm = &vm->node_main;
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800101 vlib_node_t **nodes = nm->nodes;
Benoît Ganne362456a2019-01-21 17:41:25 +0100102 u8 *chroot_filename = 0;
103 int fd;
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800104 uword *active = 0;
105 u32 i, j;
106 unformat_input_t _line_input, *line_input = &_line_input;
107 u8 filter = 0, calls_filter = 0, vectors_filter = 0, both = 0;
Benoît Ganne362456a2019-01-21 17:41:25 +0100108
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800109 fd = -1;
110 /* Get a line of input. */
111 if (unformat_user (input, unformat_line_input, line_input))
Benoît Ganne362456a2019-01-21 17:41:25 +0100112 {
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800113 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
114 {
115 if (unformat (line_input, "filter"))
116 filter = 1;
117 else if (unformat (line_input, "calls") && filter)
118 calls_filter = 1;
119 else if (unformat (line_input, "vectors") && filter)
120 vectors_filter = 1;
121 else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
122 &chroot_filename))
123 {
124 fd = open ((char *) chroot_filename,
125 O_CREAT | O_TRUNC | O_WRONLY, 0664);
126 }
127 else
128 return clib_error_return (0, "unknown input `%U'",
129 format_unformat_error, input);
130 }
131 unformat_free (line_input);
Benoît Ganne362456a2019-01-21 17:41:25 +0100132 }
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800133
134 /*both is set to true if calls_filter and vectors_filter are, or neither */
135 both = filter & (!(calls_filter ^ vectors_filter));
Benoît Ganne362456a2019-01-21 17:41:25 +0100136
137#define format__(vm__, fd__, ...) \
138 if ((fd) < 0) \
139 { \
140 vlib_cli_output((vm__), ## __VA_ARGS__); \
141 } \
142 else \
143 { \
144 fdformat((fd__), ## __VA_ARGS__); \
145 }
146
147 format__ (vm, fd, "%s", "digraph {\n");
148
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800149 clib_bitmap_alloc (active, vec_len (nodes));
150 clib_bitmap_set_region (active, 0, 1, vec_len (nodes));
151 if (filter)
Benoît Ganne362456a2019-01-21 17:41:25 +0100152 {
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800153 /*Adding the legend to the dot file*/
154 format__ (vm, fd, "%s",
155 " rankdir=\"LR\"\n nodesep=2\n subgraph cluster_legend {\n "
156 " label=\"Legend\"\n style=\"solid\"\n labelloc = b\n "
157 " subgraph cluster_colors {\n label=\"Packets/Call\"\n "
158 " style=\"solid\"\n labelloc = b\n");
159 format__ (vm, fd, "%s",
160 " 0 [label=\"No packet\", fixedsize=true shape=circle "
161 "width=2 fontsize=17]\n"
162 " 1 [label=\"1-32\", fillcolor=1 style=filled "
163 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
164 "fontsize=17]\n"
165 " 2 [label=\"33-64\", fillcolor=2 style=filled "
166 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
167 "fontsize=17]\n"
168 " 3 [label=\"65-96\", fillcolor=3 style=filled "
169 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
170 "fontsize=17]\n"
171 " 4 [label=\"97-128\", fillcolor=4 style=filled "
172 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
173 "fontsize=17]\n"
174 " 5 [label=\"129-160\", fillcolor=5 style=filled "
175 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
176 "fontsize=17]\n"
177 " 6 [label=\"161-192\", fillcolor=6 style=filled "
178 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
179 "fontsize=17]\n"
180 " 7 [label=\"193-224\", fillcolor=7 style=filled "
181 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
182 "fontsize=17]\n"
183 " 8 [label=\"224+\", fillcolor=8 style=filled "
184 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
185 "fontsize=17]\n");
186 format__ (vm, fd, "%s",
187 " 0 -> 1 -> 2 -> 3 -> 4 [style=\"invis\",weight =100]\n "
188 " 5 -> 6 -> 7 -> 8 [style=\"invis\",weight =100]\n }\n "
189 " subgraph cluster_size {\n label=\"Cycles/Packet\"\n "
190 " style=\"solid\"\n labelloc = b\n");
191 format__ (
192 vm, fd, "%s",
193 " a[label=\"0\",fixedsize=true shape=circle width=1] \n"
194 " b[label=\"10\",fixedsize=true shape=circle width=2 "
195 "fontsize=17]\n"
196 " c[label=\"100\",fixedsize=true shape=circle width=3 "
197 "fontsize=20]\n"
198 " d[label=\"1000\",fixedsize=true shape=circle width=4 "
199 "fontsize=23]\n"
200 " a -> b -> c -> d [style=\"invis\",weight =100]\n }\n }\n");
201
202 vlib_worker_thread_barrier_sync (vm);
203 for (j = 0; j < vec_len (nm->nodes); j++)
Benoît Ganne362456a2019-01-21 17:41:25 +0100204 {
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800205 vlib_node_t *n;
206 n = nm->nodes[j];
207 vlib_node_sync_stats (vm, n);
208 }
Benoît Ganne362456a2019-01-21 17:41:25 +0100209
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800210 /* Updating the stats for multithreaded use cases.
211 * We need to dup the nodes to sum the stats from all threads.*/
212 nodes = vec_dup (nm->nodes);
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100213 for (i = 1; i < vlib_get_n_threads (); i++)
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800214 {
215 vlib_node_main_t *nm_clone;
216 vlib_main_t *vm_clone;
217 vlib_node_runtime_t *rt;
218 vlib_node_t *n;
Benoît Ganne362456a2019-01-21 17:41:25 +0100219
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100220 vm_clone = vlib_get_main_by_index (i);
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800221 nm_clone = &vm_clone->node_main;
222
223 for (j = 0; j < vec_len (nm_clone->nodes); j++)
224 {
225 n = nm_clone->nodes[j];
226
227 rt = vlib_node_get_runtime (vm_clone, n->index);
228 /* Sync the stats directly in the duplicated node.*/
229 vlib_node_runtime_sync_stats_node (nodes[j], rt, 0, 0, 0);
230 }
231 }
232 vlib_worker_thread_barrier_release (vm);
233
234 for (i = 0; i < vec_len (nodes); i++)
235 {
236 u64 p, c, l;
237 c = nodes[i]->stats_total.calls - nodes[i]->stats_last_clear.calls;
238 p =
239 nodes[i]->stats_total.vectors - nodes[i]->stats_last_clear.vectors;
240 l = nodes[i]->stats_total.clocks - nodes[i]->stats_last_clear.clocks;
241
242 if ((both && c > 0 && p > 0) || (calls_filter && c > 0) ||
243 (vectors_filter && p > 0))
244 {
245 format__ (vm, fd, " \"%v\" [shape=circle", nodes[i]->name);
246 /*Changing the size and the font of nodes that receive packets*/
247 if (p > 0)
248 {
249 f64 x = (f64) l / (f64) p;
250 f64 size_ratio = (1 + log10 (x + 1));
251 format__ (vm, fd, " width=%.2f fontsize=%.2f fixedsize=true",
252 size_ratio, 11 + 3 * size_ratio);
253 /*Coloring nodes that are indeed called*/
254 if (c > 0)
255 {
256 u64 color = ((p - 1) / (32 * c)) + 1;
257 color = clib_min (color, 8);
258 format__ (
259 vm, fd,
260 " fillcolor=%u style=filled colorscheme=ylorrd8",
261 color);
262 }
263 }
264 format__ (vm, fd, "]\n");
265 }
266 else
267 {
268 clib_bitmap_set (active, i, 0);
269 }
Benoît Ganne362456a2019-01-21 17:41:25 +0100270 }
271 }
272
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800273 clib_bitmap_foreach (i, active)
274 {
275 for (j = 0; j < vec_len (nodes[i]->next_nodes); j++)
276 {
277 if (nodes[i]->next_nodes[j] == VLIB_INVALID_NODE_INDEX)
278 continue;
279
280 if (!filter || clib_bitmap_get (active, nodes[i]->next_nodes[j]))
281 {
282 format__ (vm, fd, " \"%v\" -> \"%v\"\n", nodes[i]->name,
283 nodes[nodes[i]->next_nodes[j]]->name);
284 }
285 }
286 }
287
288 format__ (vm, fd, "}\n");
Benoît Ganne362456a2019-01-21 17:41:25 +0100289
290 if (fd >= 0)
291 {
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800292 /*Dumping all the nodes saturates dot capacities to render a directed
293 * graph. In this case, prefer using he fdp command to generate an
294 * undirected graph. */
295 const char *soft = filter ? "dot" : "fdp";
296 vlib_cli_output (
297 vm, "vlib graph dumped into `%s'. Run eg. `%s -Tsvg -O %s'.",
298 chroot_filename, soft, chroot_filename);
Benoît Ganne362456a2019-01-21 17:41:25 +0100299 }
300
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800301 clib_bitmap_free (active);
Benoît Ganne362456a2019-01-21 17:41:25 +0100302 vec_free (chroot_filename);
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800303 if (filter)
304 vec_free (nodes);
Benoît Ganne362456a2019-01-21 17:41:25 +0100305 if (fd >= 0)
306 close (fd);
307 return error;
308}
309
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800310/*?
311 * Dump dot files data to draw a graph of all the nodes.
312 * If the argument 'filter' is provided, only the active nodes (since the last
Nathan Skrzypczakecd6b1a2021-09-29 15:35:31 +0200313 * "clear run" command) are selected and they are scaled and colored according
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800314 * to their utilization. You can choose to filter nodes that are called,
315 * nodes that receive vectors or both (default).
316 * The 'file' option allows to save data in a temp file.
317 *
318 * @cliexpar
319 * @clistart
320 * show vlib graphviz
321 * show vlib graphviz filter file tmpfile
322 * show vlib graphviz filter calls file tmpfile
323 * @cliend
324 * @cliexcmd{show vlib graphviz [filter][calls][vectors][file <filename>]}
325?*/
Benoît Ganne362456a2019-01-21 17:41:25 +0100326/* *INDENT-OFF* */
327VLIB_CLI_COMMAND (show_node_graphviz_command, static) = {
328 .path = "show vlib graphviz",
329 .short_help = "Dump packet processing node graph as a graphviz dotfile",
330 .function = show_node_graphviz,
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800331 .is_mp_safe = 1,
Benoît Ganne362456a2019-01-21 17:41:25 +0100332};
333/* *INDENT-ON* */
334
Dave Barach9b8ffd92016-07-08 08:13:45 -0400335static u8 *
Damjan Marion69abe442018-08-27 22:43:23 +0200336format_vlib_node_state (u8 * s, va_list * va)
337{
338 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
339 vlib_node_t *n = va_arg (*va, vlib_node_t *);
340 char *state;
341
342 state = "active";
343 if (n->type == VLIB_NODE_TYPE_PROCESS)
344 {
345 vlib_process_t *p = vlib_get_process_from_node (vm, n);
346
347 switch (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
348 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
349 {
350 default:
351 if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
352 state = "done";
353 break;
354
355 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
356 state = "time wait";
357 break;
358
359 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
360 state = "event wait";
361 break;
362
363 case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
364 state =
365 "any wait";
366 break;
367 }
368 }
369 else if (n->type != VLIB_NODE_TYPE_INTERNAL)
370 {
371 state = "polling";
372 if (n->state == VLIB_NODE_STATE_DISABLED)
373 state = "disabled";
374 else if (n->state == VLIB_NODE_STATE_INTERRUPT)
375 state = "interrupt wait";
376 }
377
378 return format (s, "%s", state);
379}
380
381static u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400382format_vlib_node_stats (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400384 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
385 vlib_node_t *n = va_arg (*va, vlib_node_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386 int max = va_arg (*va, int);
387 f64 v;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400388 u8 *ns;
389 u8 *misc_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390 u64 c, p, l, d;
391 f64 x;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400392 f64 maxc, maxcn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 u32 maxn;
Christophe Fontained3c008d2017-10-02 18:10:54 +0200394 u32 indent;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395
Dave Barach9b8ffd92016-07-08 08:13:45 -0400396 if (!n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400398 if (max)
Dave Barach4d1a8662018-09-10 12:31:15 -0400399 s = format (s,
400 "%=30s%=17s%=16s%=16s%=16s%=16s",
401 "Name", "Max Node Clocks", "Vectors at Max",
402 "Max Clocks", "Avg Clocks", "Avg Vectors/Call");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 else
Dave Barach4d1a8662018-09-10 12:31:15 -0400404 s = format (s,
405 "%=30s%=12s%=16s%=16s%=16s%=16s%=16s",
406 "Name", "State", "Calls", "Vectors", "Suspends",
407 "Clocks", "Vectors/Call");
Dave Barach4d1a8662018-09-10 12:31:15 -0400408 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 }
410
411 indent = format_get_indent (s);
412
413 l = n->stats_total.clocks - n->stats_last_clear.clocks;
414 c = n->stats_total.calls - n->stats_last_clear.calls;
415 p = n->stats_total.vectors - n->stats_last_clear.vectors;
416 d = n->stats_total.suspends - n->stats_last_clear.suspends;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400417 maxc = (f64) n->stats_total.max_clock;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 maxn = n->stats_total.max_clock_n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400419 if (n->stats_total.max_clock_n)
420 maxcn = (f64) n->stats_total.max_clock / (f64) maxn;
421 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 maxcn = 0.0;
423
424 /* Clocks per packet, per call or per suspend. */
425 x = 0;
426 if (p > 0)
427 x = (f64) l / (f64) p;
428 else if (c > 0)
429 x = (f64) l / (f64) c;
430 else if (d > 0)
431 x = (f64) l / (f64) d;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400432
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 if (c > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400434 v = (double) p / (double) c;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435 else
436 v = 0;
437
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 if (n->type == VLIB_NODE_TYPE_PROCESS)
439 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400440 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
442 /* Show processes with events pending. This helps spot bugs where events are not
Dave Barach9b8ffd92016-07-08 08:13:45 -0400443 being handled. */
444 if (!clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 misc_info = format (misc_info, "events pending, ");
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 ns = n->name;
448
449 if (max)
450 s = format (s, "%-30v%=17.2e%=16d%=16.2e%=16.2e%=16.2e",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400451 ns, maxc, maxn, maxcn, x, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 else
Damjan Marion69abe442018-08-27 22:43:23 +0200453 s = format (s, "%-30v%=12U%16Ld%16Ld%16Ld%16.2e%16.2f", ns,
454 format_vlib_node_state, vm, n, c, p, d, x, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455
456 if (ns != n->name)
457 vec_free (ns);
458
459 if (misc_info)
460 {
461 s = format (s, "\n%U%v", format_white_space, indent + 4, misc_info);
462 vec_free (misc_info);
463 }
464
465 return s;
466}
467
Dave Baracha8df85c2019-10-01 13:34:23 -0400468f64 vlib_get_stat_segment_update_rate (void) __attribute__ ((weak));
469f64
470vlib_get_stat_segment_update_rate (void)
471{
472 return 1e70;
473}
474
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475static clib_error_t *
476show_node_runtime (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400477 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400479 vlib_node_main_t *nm = &vm->node_main;
480 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481 f64 time_now;
482 u32 node_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400483 vlib_node_t ***node_dups = 0;
Dave Baracha8df85c2019-10-01 13:34:23 -0400484 f64 *internal_node_vector_rates = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485
486 time_now = vlib_time_now (vm);
487
488 if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
489 {
490 n = vlib_get_node (vm, node_index);
491 vlib_node_sync_stats (vm, n);
492 vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, 0, 0);
493 vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, n, 0);
494 }
495 else
496 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400497 vlib_node_t **nodes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 uword i, j;
499 f64 dt;
500 u64 n_input, n_output, n_drop, n_punt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501 u64 n_clocks, l, v, c, d;
502 int brief = 1;
Dave Barachac78f8a2019-10-04 09:59:00 -0400503 int summary = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 int max = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400505 vlib_main_t **stat_vms = 0, *stat_vm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506
507 /* Suppress nodes with zero calls since last clear */
508 if (unformat (input, "brief") || unformat (input, "b"))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400509 brief = 1;
510 if (unformat (input, "verbose") || unformat (input, "v"))
511 brief = 0;
512 if (unformat (input, "max") || unformat (input, "m"))
513 max = 1;
Dave Barachac78f8a2019-10-04 09:59:00 -0400514 if (unformat (input, "summary") || unformat (input, "sum")
515 || unformat (input, "su"))
516 summary = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100518 for (i = 0; i < vlib_get_n_threads (); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400519 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100520 stat_vm = vlib_get_main_by_index (i);
Dave Barach80f54e22017-03-08 19:08:56 -0500521 if (stat_vm)
522 vec_add1 (stat_vms, stat_vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400523 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
Dave Barach9b8ffd92016-07-08 08:13:45 -0400525 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526 * Barrier sync across stats scraping.
527 * Otherwise, the counts will be grossly inaccurate.
528 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400529 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530
531 for (j = 0; j < vec_len (stat_vms); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400532 {
533 stat_vm = stat_vms[j];
534 nm = &stat_vm->node_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535
Dave Barach9b8ffd92016-07-08 08:13:45 -0400536 for (i = 0; i < vec_len (nm->nodes); i++)
537 {
538 n = nm->nodes[i];
539 vlib_node_sync_stats (stat_vm, n);
540 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
Dave Barach9b8ffd92016-07-08 08:13:45 -0400542 nodes = vec_dup (nm->nodes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Dave Barach9b8ffd92016-07-08 08:13:45 -0400544 vec_add1 (node_dups, nodes);
Dave Baracha8df85c2019-10-01 13:34:23 -0400545 vec_add1 (internal_node_vector_rates,
546 vlib_internal_node_vector_rate (stat_vm));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400547 }
548 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549
550
551 for (j = 0; j < vec_len (stat_vms); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400552 {
553 stat_vm = stat_vms[j];
554 nodes = node_dups[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555
Dave Barach9b8ffd92016-07-08 08:13:45 -0400556 vec_sort_with_function (nodes, node_cmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557
Dave Barach9b8ffd92016-07-08 08:13:45 -0400558 n_input = n_output = n_drop = n_punt = n_clocks = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400559 for (i = 0; i < vec_len (nodes); i++)
560 {
561 n = nodes[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
Dave Barach9b8ffd92016-07-08 08:13:45 -0400563 l = n->stats_total.clocks - n->stats_last_clear.clocks;
564 n_clocks += l;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
Dave Barach9b8ffd92016-07-08 08:13:45 -0400566 v = n->stats_total.vectors - n->stats_last_clear.vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567
Dave Barach9b8ffd92016-07-08 08:13:45 -0400568 switch (n->type)
569 {
570 default:
571 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572
Dave Barach9b8ffd92016-07-08 08:13:45 -0400573 case VLIB_NODE_TYPE_INTERNAL:
574 n_output += (n->flags & VLIB_NODE_FLAG_IS_OUTPUT) ? v : 0;
575 n_drop += (n->flags & VLIB_NODE_FLAG_IS_DROP) ? v : 0;
576 n_punt += (n->flags & VLIB_NODE_FLAG_IS_PUNT) ? v : 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400577 if (n->flags & VLIB_NODE_FLAG_IS_HANDOFF)
578 n_input += v;
579 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Dave Barach9b8ffd92016-07-08 08:13:45 -0400581 case VLIB_NODE_TYPE_INPUT:
582 n_input += v;
583 break;
584 }
585 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100587 if (vlib_get_n_threads () > 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400588 {
589 vlib_worker_thread_t *w = vlib_worker_threads + j;
590 if (j > 0)
591 vlib_cli_output (vm, "---------------");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200593 if (w->cpu_id > -1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400594 vlib_cli_output (vm, "Thread %d %s (lcore %u)", j, w->name,
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200595 w->cpu_id);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400596 else
597 vlib_cli_output (vm, "Thread %d %s", j, w->name);
598 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599
Dave Barach9b8ffd92016-07-08 08:13:45 -0400600 dt = time_now - nm->time_last_runtime_stats_clear;
601 vlib_cli_output
602 (vm,
Dave Barach000a0292020-02-17 17:07:12 -0500603 "Time %.1f, %f sec internal node vector rate %.2f loops/sec %.2f\n"
Dave Baracha8df85c2019-10-01 13:34:23 -0400604 " vector rates in %.4e, out %.4e, drop %.4e, punt %.4e",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400605 dt,
Dave Baracha8df85c2019-10-01 13:34:23 -0400606 vlib_get_stat_segment_update_rate (),
607 internal_node_vector_rates[j],
Dave Barach000a0292020-02-17 17:07:12 -0500608 stat_vm->loops_per_second,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400609 (f64) n_input / dt,
610 (f64) n_output / dt, (f64) n_drop / dt, (f64) n_punt / dt);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611
Dave Barachac78f8a2019-10-04 09:59:00 -0400612 if (summary == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400613 {
Dave Barachac78f8a2019-10-04 09:59:00 -0400614 vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm,
615 0, max);
616 for (i = 0; i < vec_len (nodes); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400617 {
Dave Barachac78f8a2019-10-04 09:59:00 -0400618 c =
619 nodes[i]->stats_total.calls -
620 nodes[i]->stats_last_clear.calls;
621 d =
622 nodes[i]->stats_total.suspends -
623 nodes[i]->stats_last_clear.suspends;
624 if (c || d || !brief)
625 {
626 vlib_cli_output (vm, "%U", format_vlib_node_stats,
627 stat_vm, nodes[i], max);
628 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400629 }
630 }
631 vec_free (nodes);
632 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 vec_free (stat_vms);
634 vec_free (node_dups);
Dave Baracha8df85c2019-10-01 13:34:23 -0400635 vec_free (internal_node_vector_rates);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636 }
637
638 return 0;
639}
640
Dave Barach9b8ffd92016-07-08 08:13:45 -0400641/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642VLIB_CLI_COMMAND (show_node_runtime_command, static) = {
643 .path = "show runtime",
644 .short_help = "Show packet processing runtime",
645 .function = show_node_runtime,
646 .is_mp_safe = 1,
647};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400648/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649
650static clib_error_t *
651clear_node_runtime (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400652 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400654 vlib_node_main_t *nm;
655 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656 int i, j;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657 vlib_main_t **stat_vms = 0, *stat_vm;
658 vlib_node_runtime_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100660 for (i = 0; i < vlib_get_n_threads (); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100662 stat_vm = vlib_get_main_by_index (i);
Dave Barach80f54e22017-03-08 19:08:56 -0500663 if (stat_vm)
664 vec_add1 (stat_vms, stat_vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400666
667 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668
669 for (j = 0; j < vec_len (stat_vms); j++)
670 {
671 stat_vm = stat_vms[j];
672 nm = &stat_vm->node_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
Dave Barach9b8ffd92016-07-08 08:13:45 -0400674 for (i = 0; i < vec_len (nm->nodes); i++)
675 {
676 n = nm->nodes[i];
677 vlib_node_sync_stats (stat_vm, n);
678 n->stats_last_clear = n->stats_total;
679
680 r = vlib_node_get_runtime (stat_vm, n->index);
681 r->max_clock = 0;
682 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 /* Note: input/output rates computed using vlib_global_main */
684 nm->time_last_runtime_stats_clear = vlib_time_now (vm);
685 }
686
Dave Barach9b8ffd92016-07-08 08:13:45 -0400687 vlib_worker_thread_barrier_release (vm);
688
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689 vec_free (stat_vms);
690
691 return 0;
692}
693
Dave Barach9b8ffd92016-07-08 08:13:45 -0400694/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695VLIB_CLI_COMMAND (clear_node_runtime_command, static) = {
696 .path = "clear runtime",
697 .short_help = "Clear packet processing runtime statistics",
698 .function = clear_node_runtime,
699};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400700/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701
Damjan Marion69abe442018-08-27 22:43:23 +0200702static clib_error_t *
703show_node (vlib_main_t * vm, unformat_input_t * input,
704 vlib_cli_command_t * cmd)
705{
706 unformat_input_t _line_input, *line_input = &_line_input;
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700707 clib_error_t *error = 0;
Damjan Marion69abe442018-08-27 22:43:23 +0200708 vlib_node_main_t *nm = &vm->node_main;
709 vlib_node_t *n;
710 u8 *s = 0, *s2 = 0;
Florin Coras178ecc02020-03-12 23:26:11 +0000711 u32 i, node_index = ~0, verbose = 0;
Damjan Marion69abe442018-08-27 22:43:23 +0200712 char *type_str;
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700713 u8 valid_node_name = 0;
Florin Coras178ecc02020-03-12 23:26:11 +0000714 u64 cl, ca, v;
Damjan Marion69abe442018-08-27 22:43:23 +0200715
716 if (!unformat_user (input, unformat_line_input, line_input))
717 return 0;
718
719 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
720 {
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700721 if (unformat (line_input, "index %u", &node_index))
Damjan Marion69abe442018-08-27 22:43:23 +0200722 ;
Florin Coras178ecc02020-03-12 23:26:11 +0000723 else if (unformat (line_input, "verbose"))
724 verbose = 1;
Damjan Marion69abe442018-08-27 22:43:23 +0200725 else
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700726 if (unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
727 valid_node_name = 1;
728 else if (!valid_node_name)
729 error = clib_error_return (0, "unknown node name: '%U'",
730 format_unformat_error, line_input);
731 else
732 error = clib_error_return (0, "unknown input '%U'",
733 format_unformat_error, line_input);
Filip Tehlarbe0ffbc2019-07-09 13:52:26 +0000734
735 if (error)
736 break;
Damjan Marion69abe442018-08-27 22:43:23 +0200737 }
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700738
Damjan Marion69abe442018-08-27 22:43:23 +0200739 unformat_free (line_input);
740
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700741 if (error)
742 return error;
743
Damjan Marion69abe442018-08-27 22:43:23 +0200744 if (node_index >= vec_len (vm->node_main.nodes))
745 return clib_error_return (0, "please specify valid node");
746
747 n = vlib_get_node (vm, node_index);
748 vlib_node_sync_stats (vm, n);
749
750 switch (n->type)
751 {
752 case VLIB_NODE_TYPE_INTERNAL:
753 type_str = "internal";
754 break;
755 case VLIB_NODE_TYPE_INPUT:
756 type_str = "input";
757 break;
758 case VLIB_NODE_TYPE_PRE_INPUT:
759 type_str = "pre-input";
760 break;
761 case VLIB_NODE_TYPE_PROCESS:
762 type_str = "process";
763 break;
764 default:
765 type_str = "unknown";
766 }
767
768 if (n->sibling_of)
769 s = format (s, ", sibling-of %s", n->sibling_of);
770
Benoît Ganne62d9fda2019-12-16 15:49:47 +0100771 vlib_cli_output (vm, "node %v, type %s, state %U, index %d%v\n",
Damjan Marion69abe442018-08-27 22:43:23 +0200772 n->name, type_str, format_vlib_node_state, vm, n,
773 n->index, s);
774 vec_reset_length (s);
775
776 if (n->node_fn_registrations)
777 {
778 vlib_node_fn_registration_t *fnr = n->node_fn_registrations;
Damjan Mariona31698b2021-03-10 14:35:28 +0100779 vlib_node_fn_variant_t *v;
Damjan Marion69abe442018-08-27 22:43:23 +0200780 while (fnr)
781 {
Damjan Mariona31698b2021-03-10 14:35:28 +0100782 v = vec_elt_at_index (vm->node_main.variants, fnr->march_variant);
Damjan Marion69abe442018-08-27 22:43:23 +0200783 if (vec_len (s) == 0)
Damjan Mariona31698b2021-03-10 14:35:28 +0100784 s = format (s, "\n %-15s %=8s %6s %s", "Name", "Priority",
785 "Active", "Description");
786 s = format (s, "\n %-15s %8d %=6s %s", v->suffix, v->priority,
787 fnr->function == n->function ? "yes" : "", v->desc);
Damjan Marion69abe442018-08-27 22:43:23 +0200788 fnr = fnr->next_registration;
789 }
790 }
791 else
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700792 s = format (s, "\n default only");
Damjan Marion69abe442018-08-27 22:43:23 +0200793 vlib_cli_output (vm, " node function variants:%v\n", s);
794 vec_reset_length (s);
795
796 for (i = 0; i < vec_len (n->next_nodes); i++)
797 {
798 vlib_node_t *pn;
799 if (n->next_nodes[i] == VLIB_INVALID_NODE_INDEX)
800 continue;
801
802 pn = vec_elt (nm->nodes, n->next_nodes[i]);
803
804 if (vec_len (s) == 0)
805 s = format (s, "\n %10s %10s %=30s %8s",
806 "next-index", "node-index", "Node", "Vectors");
807
Florin Coras178ecc02020-03-12 23:26:11 +0000808 s = format (s, "\n %=10u %=10u %=30v %=8llu", i, n->next_nodes[i],
Damjan Marion69abe442018-08-27 22:43:23 +0200809 pn->name, vec_elt (n->n_vectors_by_next_node, i));
810 }
811
812 if (vec_len (s) == 0)
813 s = format (s, "\n none");
814 vlib_cli_output (vm, "\n next nodes:%v\n", s);
815 vec_reset_length (s);
816
817 if (n->type == VLIB_NODE_TYPE_INTERNAL)
818 {
819 int j = 0;
820 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100821 clib_bitmap_foreach (i, n->prev_node_bitmap) {
Damjan Marion69abe442018-08-27 22:43:23 +0200822 vlib_node_t *pn = vlib_get_node (vm, i);
823 if (j++ % 3 == 0)
824 s = format (s, "\n ");
825 s2 = format (s2, "%v (%u)", pn->name, i);
826 s = format (s, "%-35v", s2);
827 vec_reset_length (s2);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100828 }
Damjan Marion69abe442018-08-27 22:43:23 +0200829 /* *INDENT-ON* */
830
831 if (vec_len (s) == 0)
832 s = format (s, "\n none");
833 vlib_cli_output (vm, "\n known previous nodes:%v\n", s);
834 vec_reset_length (s);
Florin Coras178ecc02020-03-12 23:26:11 +0000835 vec_free (s2);
Damjan Marion69abe442018-08-27 22:43:23 +0200836 }
837
Florin Coras178ecc02020-03-12 23:26:11 +0000838 if (!verbose)
839 goto done;
840
841 s = format (s, "\n%8s %=12s %=12s %=12s %=12s %=12s\n", "Thread", "Calls",
842 "Clocks", "Vectors", "Max Clock", "Max Vectors");
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100843 for (i = 0; i < vlib_get_n_threads (); i++)
Florin Coras178ecc02020-03-12 23:26:11 +0000844 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100845 n = vlib_get_node (vlib_get_main_by_index (i), node_index);
846 vlib_node_sync_stats (vlib_get_main_by_index (i), n);
Florin Coras178ecc02020-03-12 23:26:11 +0000847
848 cl = n->stats_total.clocks - n->stats_last_clear.clocks;
849 ca = n->stats_total.calls - n->stats_last_clear.calls;
850 v = n->stats_total.vectors - n->stats_last_clear.vectors;
851
852 s = format (s, "%=8u %=12lu %=12lu %=12lu %=12u %=12u\n", i, ca, cl, v,
853 n->stats_total.max_clock, n->stats_total.max_clock_n);
854 }
855
856 vlib_cli_output (vm, "%v", s);
857
858done:
859
Damjan Marion69abe442018-08-27 22:43:23 +0200860 vec_free (s);
Damjan Marion69abe442018-08-27 22:43:23 +0200861 return 0;
862}
863
864/* *INDENT-OFF* */
865VLIB_CLI_COMMAND (show_node_command, static) = {
866 .path = "show node",
867 .short_help = "show node [index] <node-name | node-index>",
868 .function = show_node,
869};
870
871static clib_error_t *
872set_node_fn(vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
873{
874 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Mariona31698b2021-03-10 14:35:28 +0100875 u32 node_index, march_variant;
Damjan Marion69abe442018-08-27 22:43:23 +0200876 vlib_node_t *n;
877 clib_error_t *err = 0;
Damjan Marion69abe442018-08-27 22:43:23 +0200878
879 if (!unformat_user (input, unformat_line_input, line_input))
880 return 0;
881
882 if (!unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
883 {
884 err = clib_error_return (0, "please specify valid node name");
885 goto done;
886 }
887
Damjan Mariona31698b2021-03-10 14:35:28 +0100888 if (!unformat (line_input, "%U", unformat_vlib_node_variant, &march_variant))
Damjan Marion69abe442018-08-27 22:43:23 +0200889 {
Damjan Mariona31698b2021-03-10 14:35:28 +0100890 err = clib_error_return (0, "please specify node function variant");
Damjan Marion69abe442018-08-27 22:43:23 +0200891 goto done;
892 }
893
894 n = vlib_get_node (vm, node_index);
895
896 if (n->node_fn_registrations == 0)
897 {
Damjan Mariona31698b2021-03-10 14:35:28 +0100898 err = clib_error_return (0, "node doesn't have function variants");
Damjan Marion69abe442018-08-27 22:43:23 +0200899 goto done;
900 }
901
Damjan Mariona31698b2021-03-10 14:35:28 +0100902 if (vlib_node_set_march_variant (vm, node_index, march_variant))
Damjan Marion69abe442018-08-27 22:43:23 +0200903 {
Damjan Mariona31698b2021-03-10 14:35:28 +0100904 vlib_node_fn_variant_t *v;
905 v = vec_elt_at_index (vm->node_main.variants, march_variant);
906 err = clib_error_return (0, "node function variant '%s' not found",
907 v->suffix);
908 goto done;
Damjan Marion69abe442018-08-27 22:43:23 +0200909 }
910
Damjan Marion69abe442018-08-27 22:43:23 +0200911
912done:
Damjan Marion69abe442018-08-27 22:43:23 +0200913 unformat_free (line_input);
914 return err;
915}
916
917/* *INDENT-OFF* */
918VLIB_CLI_COMMAND (set_node_fn_command, static) = {
919 .path = "set node function",
920 .short_help = "set node function <node-name> <variant-name>",
921 .function = set_node_fn,
922};
923/* *INDENT-ON* */
924
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925/* Dummy function to get us linked in. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400926void
927vlib_node_cli_reference (void)
928{
929}
930
931/*
932 * fd.io coding-style-patch-verification: ON
933 *
934 * Local Variables:
935 * eval: (c-set-style "gnu")
936 * End:
937 */