blob: 075430e47615011945447d92735d4fcf6abd7162 [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>
Damjan Marion8973b072022-03-01 15:51:18 +010045#include <vlib/stats/stats.h>
Arthur de Kerhor156158f2021-02-18 03:09:42 -080046#include <math.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
Matus Fabiand2dc3df2015-12-14 10:31:33 -050048static int
Dave Barach9b8ffd92016-07-08 08:13:45 -040049node_cmp (void *a1, void *a2)
Matus Fabiand2dc3df2015-12-14 10:31:33 -050050{
Dave Barach9b8ffd92016-07-08 08:13:45 -040051 vlib_node_t **n1 = a1;
52 vlib_node_t **n2 = a2;
Matus Fabiand2dc3df2015-12-14 10:31:33 -050053
54 return vec_cmp (n1[0]->name, n2[0]->name);
55}
56
Ed Warnickecb9cada2015-12-08 15:45:58 -070057static clib_error_t *
58show_node_graph (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -040059 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070060{
Dave Barach9b8ffd92016-07-08 08:13:45 -040061 vlib_node_main_t *nm = &vm->node_main;
62 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -070063 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 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040074 vlib_node_t **nodes = vec_dup (nm->nodes);
Ed Warnickecb9cada2015-12-08 15:45:58 -070075 uword i;
76
Matus Fabiand2dc3df2015-12-14 10:31:33 -050077 vec_sort_with_function (nodes, node_cmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
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
Dave Barach9b8ffd92016-07-08 08:13:45 -040088/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070089VLIB_CLI_COMMAND (show_node_graph_command, static) = {
90 .path = "show vlib graph",
91 .short_help = "Show packet processing node graph",
92 .function = show_node_graph,
93};
Dave Barach9b8ffd92016-07-08 08:13:45 -040094/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
Benoît Ganne362456a2019-01-21 17:41:25 +010096static clib_error_t *
97show_node_graphviz (vlib_main_t * vm,
98 unformat_input_t * input, vlib_cli_command_t * cmd)
99{
100 clib_error_t *error = 0;
101 vlib_node_main_t *nm = &vm->node_main;
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800102 vlib_node_t **nodes = nm->nodes;
Benoît Ganne362456a2019-01-21 17:41:25 +0100103 u8 *chroot_filename = 0;
104 int fd;
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800105 uword *active = 0;
106 u32 i, j;
107 unformat_input_t _line_input, *line_input = &_line_input;
108 u8 filter = 0, calls_filter = 0, vectors_filter = 0, both = 0;
Benoît Ganne362456a2019-01-21 17:41:25 +0100109
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800110 fd = -1;
111 /* Get a line of input. */
112 if (unformat_user (input, unformat_line_input, line_input))
Benoît Ganne362456a2019-01-21 17:41:25 +0100113 {
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800114 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
115 {
116 if (unformat (line_input, "filter"))
117 filter = 1;
118 else if (unformat (line_input, "calls") && filter)
119 calls_filter = 1;
120 else if (unformat (line_input, "vectors") && filter)
121 vectors_filter = 1;
122 else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
123 &chroot_filename))
124 {
125 fd = open ((char *) chroot_filename,
126 O_CREAT | O_TRUNC | O_WRONLY, 0664);
127 }
128 else
129 return clib_error_return (0, "unknown input `%U'",
130 format_unformat_error, input);
131 }
132 unformat_free (line_input);
Benoît Ganne362456a2019-01-21 17:41:25 +0100133 }
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800134
135 /*both is set to true if calls_filter and vectors_filter are, or neither */
136 both = filter & (!(calls_filter ^ vectors_filter));
Benoît Ganne362456a2019-01-21 17:41:25 +0100137
138#define format__(vm__, fd__, ...) \
139 if ((fd) < 0) \
140 { \
141 vlib_cli_output((vm__), ## __VA_ARGS__); \
142 } \
143 else \
144 { \
145 fdformat((fd__), ## __VA_ARGS__); \
146 }
147
148 format__ (vm, fd, "%s", "digraph {\n");
149
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800150 clib_bitmap_alloc (active, vec_len (nodes));
151 clib_bitmap_set_region (active, 0, 1, vec_len (nodes));
152 if (filter)
Benoît Ganne362456a2019-01-21 17:41:25 +0100153 {
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800154 /*Adding the legend to the dot file*/
155 format__ (vm, fd, "%s",
156 " rankdir=\"LR\"\n nodesep=2\n subgraph cluster_legend {\n "
157 " label=\"Legend\"\n style=\"solid\"\n labelloc = b\n "
158 " subgraph cluster_colors {\n label=\"Packets/Call\"\n "
159 " style=\"solid\"\n labelloc = b\n");
160 format__ (vm, fd, "%s",
161 " 0 [label=\"No packet\", fixedsize=true shape=circle "
162 "width=2 fontsize=17]\n"
163 " 1 [label=\"1-32\", fillcolor=1 style=filled "
164 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
165 "fontsize=17]\n"
166 " 2 [label=\"33-64\", fillcolor=2 style=filled "
167 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
168 "fontsize=17]\n"
169 " 3 [label=\"65-96\", fillcolor=3 style=filled "
170 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
171 "fontsize=17]\n"
172 " 4 [label=\"97-128\", fillcolor=4 style=filled "
173 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
174 "fontsize=17]\n"
175 " 5 [label=\"129-160\", fillcolor=5 style=filled "
176 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
177 "fontsize=17]\n"
178 " 6 [label=\"161-192\", fillcolor=6 style=filled "
179 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
180 "fontsize=17]\n"
181 " 7 [label=\"193-224\", fillcolor=7 style=filled "
182 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
183 "fontsize=17]\n"
184 " 8 [label=\"224+\", fillcolor=8 style=filled "
185 "colorscheme=ylorrd8 fixedsize=true shape=circle width=2 "
186 "fontsize=17]\n");
187 format__ (vm, fd, "%s",
188 " 0 -> 1 -> 2 -> 3 -> 4 [style=\"invis\",weight =100]\n "
189 " 5 -> 6 -> 7 -> 8 [style=\"invis\",weight =100]\n }\n "
190 " subgraph cluster_size {\n label=\"Cycles/Packet\"\n "
191 " style=\"solid\"\n labelloc = b\n");
192 format__ (
193 vm, fd, "%s",
194 " a[label=\"0\",fixedsize=true shape=circle width=1] \n"
195 " b[label=\"10\",fixedsize=true shape=circle width=2 "
196 "fontsize=17]\n"
197 " c[label=\"100\",fixedsize=true shape=circle width=3 "
198 "fontsize=20]\n"
199 " d[label=\"1000\",fixedsize=true shape=circle width=4 "
200 "fontsize=23]\n"
201 " a -> b -> c -> d [style=\"invis\",weight =100]\n }\n }\n");
202
203 vlib_worker_thread_barrier_sync (vm);
204 for (j = 0; j < vec_len (nm->nodes); j++)
Benoît Ganne362456a2019-01-21 17:41:25 +0100205 {
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800206 vlib_node_t *n;
207 n = nm->nodes[j];
208 vlib_node_sync_stats (vm, n);
209 }
Benoît Ganne362456a2019-01-21 17:41:25 +0100210
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800211 /* Updating the stats for multithreaded use cases.
212 * We need to dup the nodes to sum the stats from all threads.*/
213 nodes = vec_dup (nm->nodes);
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100214 for (i = 1; i < vlib_get_n_threads (); i++)
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800215 {
216 vlib_node_main_t *nm_clone;
217 vlib_main_t *vm_clone;
218 vlib_node_runtime_t *rt;
219 vlib_node_t *n;
Benoît Ganne362456a2019-01-21 17:41:25 +0100220
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100221 vm_clone = vlib_get_main_by_index (i);
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800222 nm_clone = &vm_clone->node_main;
223
224 for (j = 0; j < vec_len (nm_clone->nodes); j++)
225 {
226 n = nm_clone->nodes[j];
227
228 rt = vlib_node_get_runtime (vm_clone, n->index);
229 /* Sync the stats directly in the duplicated node.*/
230 vlib_node_runtime_sync_stats_node (nodes[j], rt, 0, 0, 0);
231 }
232 }
233 vlib_worker_thread_barrier_release (vm);
234
235 for (i = 0; i < vec_len (nodes); i++)
236 {
237 u64 p, c, l;
238 c = nodes[i]->stats_total.calls - nodes[i]->stats_last_clear.calls;
239 p =
240 nodes[i]->stats_total.vectors - nodes[i]->stats_last_clear.vectors;
241 l = nodes[i]->stats_total.clocks - nodes[i]->stats_last_clear.clocks;
242
243 if ((both && c > 0 && p > 0) || (calls_filter && c > 0) ||
244 (vectors_filter && p > 0))
245 {
246 format__ (vm, fd, " \"%v\" [shape=circle", nodes[i]->name);
247 /*Changing the size and the font of nodes that receive packets*/
248 if (p > 0)
249 {
250 f64 x = (f64) l / (f64) p;
251 f64 size_ratio = (1 + log10 (x + 1));
252 format__ (vm, fd, " width=%.2f fontsize=%.2f fixedsize=true",
253 size_ratio, 11 + 3 * size_ratio);
254 /*Coloring nodes that are indeed called*/
255 if (c > 0)
256 {
257 u64 color = ((p - 1) / (32 * c)) + 1;
258 color = clib_min (color, 8);
259 format__ (
260 vm, fd,
261 " fillcolor=%u style=filled colorscheme=ylorrd8",
262 color);
263 }
264 }
265 format__ (vm, fd, "]\n");
266 }
267 else
268 {
269 clib_bitmap_set (active, i, 0);
270 }
Benoît Ganne362456a2019-01-21 17:41:25 +0100271 }
272 }
273
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800274 clib_bitmap_foreach (i, active)
275 {
276 for (j = 0; j < vec_len (nodes[i]->next_nodes); j++)
277 {
278 if (nodes[i]->next_nodes[j] == VLIB_INVALID_NODE_INDEX)
279 continue;
280
281 if (!filter || clib_bitmap_get (active, nodes[i]->next_nodes[j]))
282 {
283 format__ (vm, fd, " \"%v\" -> \"%v\"\n", nodes[i]->name,
284 nodes[nodes[i]->next_nodes[j]]->name);
285 }
286 }
287 }
288
289 format__ (vm, fd, "}\n");
Benoît Ganne362456a2019-01-21 17:41:25 +0100290
291 if (fd >= 0)
292 {
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800293 /*Dumping all the nodes saturates dot capacities to render a directed
294 * graph. In this case, prefer using he fdp command to generate an
295 * undirected graph. */
296 const char *soft = filter ? "dot" : "fdp";
297 vlib_cli_output (
298 vm, "vlib graph dumped into `%s'. Run eg. `%s -Tsvg -O %s'.",
299 chroot_filename, soft, chroot_filename);
Benoît Ganne362456a2019-01-21 17:41:25 +0100300 }
301
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800302 clib_bitmap_free (active);
Benoît Ganne362456a2019-01-21 17:41:25 +0100303 vec_free (chroot_filename);
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800304 if (filter)
305 vec_free (nodes);
Benoît Ganne362456a2019-01-21 17:41:25 +0100306 if (fd >= 0)
307 close (fd);
308 return error;
309}
310
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800311/*?
312 * Dump dot files data to draw a graph of all the nodes.
313 * If the argument 'filter' is provided, only the active nodes (since the last
Nathan Skrzypczakecd6b1a2021-09-29 15:35:31 +0200314 * "clear run" command) are selected and they are scaled and colored according
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800315 * to their utilization. You can choose to filter nodes that are called,
316 * nodes that receive vectors or both (default).
317 * The 'file' option allows to save data in a temp file.
318 *
319 * @cliexpar
320 * @clistart
321 * show vlib graphviz
322 * show vlib graphviz filter file tmpfile
323 * show vlib graphviz filter calls file tmpfile
324 * @cliend
325 * @cliexcmd{show vlib graphviz [filter][calls][vectors][file <filename>]}
326?*/
Benoît Ganne362456a2019-01-21 17:41:25 +0100327/* *INDENT-OFF* */
328VLIB_CLI_COMMAND (show_node_graphviz_command, static) = {
329 .path = "show vlib graphviz",
330 .short_help = "Dump packet processing node graph as a graphviz dotfile",
331 .function = show_node_graphviz,
Arthur de Kerhor156158f2021-02-18 03:09:42 -0800332 .is_mp_safe = 1,
Benoît Ganne362456a2019-01-21 17:41:25 +0100333};
334/* *INDENT-ON* */
335
Dave Barach9b8ffd92016-07-08 08:13:45 -0400336static u8 *
Damjan Marion69abe442018-08-27 22:43:23 +0200337format_vlib_node_state (u8 * s, va_list * va)
338{
339 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
340 vlib_node_t *n = va_arg (*va, vlib_node_t *);
341 char *state;
342
343 state = "active";
344 if (n->type == VLIB_NODE_TYPE_PROCESS)
345 {
346 vlib_process_t *p = vlib_get_process_from_node (vm, n);
347
348 switch (p->flags & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
349 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
350 {
351 default:
352 if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
353 state = "done";
354 break;
355
356 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
357 state = "time wait";
358 break;
359
360 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
361 state = "event wait";
362 break;
363
364 case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
365 state =
366 "any wait";
367 break;
368 }
369 }
370 else if (n->type != VLIB_NODE_TYPE_INTERNAL)
371 {
372 state = "polling";
373 if (n->state == VLIB_NODE_STATE_DISABLED)
374 state = "disabled";
375 else if (n->state == VLIB_NODE_STATE_INTERRUPT)
376 state = "interrupt wait";
377 }
378
379 return format (s, "%s", state);
380}
381
382static u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400383format_vlib_node_stats (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400385 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
386 vlib_node_t *n = va_arg (*va, vlib_node_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387 int max = va_arg (*va, int);
388 f64 v;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400389 u8 *ns;
390 u8 *misc_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 u64 c, p, l, d;
392 f64 x;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400393 f64 maxc, maxcn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 u32 maxn;
Christophe Fontained3c008d2017-10-02 18:10:54 +0200395 u32 indent;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
Dave Barach9b8ffd92016-07-08 08:13:45 -0400397 if (!n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400399 if (max)
Dave Barach4d1a8662018-09-10 12:31:15 -0400400 s = format (s,
401 "%=30s%=17s%=16s%=16s%=16s%=16s",
402 "Name", "Max Node Clocks", "Vectors at Max",
403 "Max Clocks", "Avg Clocks", "Avg Vectors/Call");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 else
Dave Barach4d1a8662018-09-10 12:31:15 -0400405 s = format (s,
406 "%=30s%=12s%=16s%=16s%=16s%=16s%=16s",
407 "Name", "State", "Calls", "Vectors", "Suspends",
408 "Clocks", "Vectors/Call");
Dave Barach4d1a8662018-09-10 12:31:15 -0400409 return s;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 }
411
412 indent = format_get_indent (s);
413
414 l = n->stats_total.clocks - n->stats_last_clear.clocks;
415 c = n->stats_total.calls - n->stats_last_clear.calls;
416 p = n->stats_total.vectors - n->stats_last_clear.vectors;
417 d = n->stats_total.suspends - n->stats_last_clear.suspends;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400418 maxc = (f64) n->stats_total.max_clock;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419 maxn = n->stats_total.max_clock_n;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400420 if (n->stats_total.max_clock_n)
421 maxcn = (f64) n->stats_total.max_clock / (f64) maxn;
422 else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 maxcn = 0.0;
424
425 /* Clocks per packet, per call or per suspend. */
426 x = 0;
427 if (p > 0)
428 x = (f64) l / (f64) p;
429 else if (c > 0)
430 x = (f64) l / (f64) c;
431 else if (d > 0)
432 x = (f64) l / (f64) d;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400433
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434 if (c > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400435 v = (double) p / (double) c;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 else
437 v = 0;
438
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 if (n->type == VLIB_NODE_TYPE_PROCESS)
440 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400441 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
443 /* Show processes with events pending. This helps spot bugs where events are not
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 being handled. */
445 if (!clib_bitmap_is_zero (p->non_empty_event_type_bitmap))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 misc_info = format (misc_info, "events pending, ");
Dave Barach9b8ffd92016-07-08 08:13:45 -0400447 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 ns = n->name;
449
450 if (max)
451 s = format (s, "%-30v%=17.2e%=16d%=16.2e%=16.2e%=16.2e",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400452 ns, maxc, maxn, maxcn, x, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 else
Damjan Marion69abe442018-08-27 22:43:23 +0200454 s = format (s, "%-30v%=12U%16Ld%16Ld%16Ld%16.2e%16.2f", ns,
455 format_vlib_node_state, vm, n, c, p, d, x, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
457 if (ns != n->name)
458 vec_free (ns);
459
460 if (misc_info)
461 {
462 s = format (s, "\n%U%v", format_white_space, indent + 4, misc_info);
463 vec_free (misc_info);
464 }
465
466 return s;
467}
468
469static clib_error_t *
470show_node_runtime (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400471 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400473 vlib_node_main_t *nm = &vm->node_main;
474 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475 f64 time_now;
476 u32 node_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400477 vlib_node_t ***node_dups = 0;
Dave Baracha8df85c2019-10-01 13:34:23 -0400478 f64 *internal_node_vector_rates = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479
480 time_now = vlib_time_now (vm);
481
482 if (unformat (input, "%U", unformat_vlib_node, vm, &node_index))
483 {
484 n = vlib_get_node (vm, node_index);
485 vlib_node_sync_stats (vm, n);
486 vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, 0, 0);
487 vlib_cli_output (vm, "%U\n", format_vlib_node_stats, vm, n, 0);
488 }
489 else
490 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400491 vlib_node_t **nodes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 uword i, j;
493 f64 dt;
494 u64 n_input, n_output, n_drop, n_punt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495 u64 n_clocks, l, v, c, d;
496 int brief = 1;
Dave Barachac78f8a2019-10-04 09:59:00 -0400497 int summary = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 int max = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400499 vlib_main_t **stat_vms = 0, *stat_vm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
501 /* Suppress nodes with zero calls since last clear */
502 if (unformat (input, "brief") || unformat (input, "b"))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400503 brief = 1;
504 if (unformat (input, "verbose") || unformat (input, "v"))
505 brief = 0;
506 if (unformat (input, "max") || unformat (input, "m"))
507 max = 1;
Dave Barachac78f8a2019-10-04 09:59:00 -0400508 if (unformat (input, "summary") || unformat (input, "sum")
509 || unformat (input, "su"))
510 summary = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100512 for (i = 0; i < vlib_get_n_threads (); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400513 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100514 stat_vm = vlib_get_main_by_index (i);
Dave Barach80f54e22017-03-08 19:08:56 -0500515 if (stat_vm)
516 vec_add1 (stat_vms, stat_vm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400517 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518
Dave Barach9b8ffd92016-07-08 08:13:45 -0400519 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 * Barrier sync across stats scraping.
521 * Otherwise, the counts will be grossly inaccurate.
522 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400523 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
525 for (j = 0; j < vec_len (stat_vms); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400526 {
527 stat_vm = stat_vms[j];
528 nm = &stat_vm->node_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530 for (i = 0; i < vec_len (nm->nodes); i++)
531 {
532 n = nm->nodes[i];
533 vlib_node_sync_stats (stat_vm, n);
534 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535
Dave Barach9b8ffd92016-07-08 08:13:45 -0400536 nodes = vec_dup (nm->nodes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537
Dave Barach9b8ffd92016-07-08 08:13:45 -0400538 vec_add1 (node_dups, nodes);
Dave Baracha8df85c2019-10-01 13:34:23 -0400539 vec_add1 (internal_node_vector_rates,
540 vlib_internal_node_vector_rate (stat_vm));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400541 }
542 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
544
545 for (j = 0; j < vec_len (stat_vms); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400546 {
547 stat_vm = stat_vms[j];
548 nodes = node_dups[j];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549
Dave Barach9b8ffd92016-07-08 08:13:45 -0400550 vec_sort_with_function (nodes, node_cmp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700551
Dave Barach9b8ffd92016-07-08 08:13:45 -0400552 n_input = n_output = n_drop = n_punt = n_clocks = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400553 for (i = 0; i < vec_len (nodes); i++)
554 {
555 n = nodes[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556
Dave Barach9b8ffd92016-07-08 08:13:45 -0400557 l = n->stats_total.clocks - n->stats_last_clear.clocks;
558 n_clocks += l;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559
Dave Barach9b8ffd92016-07-08 08:13:45 -0400560 v = n->stats_total.vectors - n->stats_last_clear.vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561
Dave Barach9b8ffd92016-07-08 08:13:45 -0400562 switch (n->type)
563 {
564 default:
565 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566
Dave Barach9b8ffd92016-07-08 08:13:45 -0400567 case VLIB_NODE_TYPE_INTERNAL:
568 n_output += (n->flags & VLIB_NODE_FLAG_IS_OUTPUT) ? v : 0;
569 n_drop += (n->flags & VLIB_NODE_FLAG_IS_DROP) ? v : 0;
570 n_punt += (n->flags & VLIB_NODE_FLAG_IS_PUNT) ? v : 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400571 if (n->flags & VLIB_NODE_FLAG_IS_HANDOFF)
572 n_input += v;
573 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574
Dave Barach9b8ffd92016-07-08 08:13:45 -0400575 case VLIB_NODE_TYPE_INPUT:
576 n_input += v;
577 break;
578 }
579 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100581 if (vlib_get_n_threads () > 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400582 {
583 vlib_worker_thread_t *w = vlib_worker_threads + j;
584 if (j > 0)
585 vlib_cli_output (vm, "---------------");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200587 if (w->cpu_id > -1)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400588 vlib_cli_output (vm, "Thread %d %s (lcore %u)", j, w->name,
Mohsin Kazmi5d64c782018-09-11 20:27:09 +0200589 w->cpu_id);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400590 else
591 vlib_cli_output (vm, "Thread %d %s", j, w->name);
592 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Dave Barach9b8ffd92016-07-08 08:13:45 -0400594 dt = time_now - nm->time_last_runtime_stats_clear;
Damjan Marion8973b072022-03-01 15:51:18 +0100595 vlib_cli_output (
596 vm,
597 "Time %.1f, %f sec internal node vector rate %.2f loops/sec %.2f\n"
598 " vector rates in %.4e, out %.4e, drop %.4e, punt %.4e",
599 dt, vlib_stats_get_segment_update_rate (),
600 internal_node_vector_rates[j], stat_vm->loops_per_second,
601 (f64) n_input / dt, (f64) n_output / dt, (f64) n_drop / dt,
602 (f64) n_punt / dt);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
Dave Barachac78f8a2019-10-04 09:59:00 -0400604 if (summary == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400605 {
Dave Barachac78f8a2019-10-04 09:59:00 -0400606 vlib_cli_output (vm, "%U", format_vlib_node_stats, stat_vm,
607 0, max);
608 for (i = 0; i < vec_len (nodes); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400609 {
Dave Barachac78f8a2019-10-04 09:59:00 -0400610 c =
611 nodes[i]->stats_total.calls -
612 nodes[i]->stats_last_clear.calls;
613 d =
614 nodes[i]->stats_total.suspends -
615 nodes[i]->stats_last_clear.suspends;
616 if (c || d || !brief)
617 {
618 vlib_cli_output (vm, "%U", format_vlib_node_stats,
619 stat_vm, nodes[i], max);
620 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400621 }
622 }
623 vec_free (nodes);
624 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625 vec_free (stat_vms);
626 vec_free (node_dups);
Dave Baracha8df85c2019-10-01 13:34:23 -0400627 vec_free (internal_node_vector_rates);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628 }
629
630 return 0;
631}
632
Dave Barach9b8ffd92016-07-08 08:13:45 -0400633/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634VLIB_CLI_COMMAND (show_node_runtime_command, static) = {
635 .path = "show runtime",
636 .short_help = "Show packet processing runtime",
637 .function = show_node_runtime,
638 .is_mp_safe = 1,
639};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400640/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641
642static clib_error_t *
643clear_node_runtime (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400644 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400646 vlib_node_main_t *nm;
647 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648 int i, j;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400649 vlib_main_t **stat_vms = 0, *stat_vm;
650 vlib_node_runtime_t *r;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100652 for (i = 0; i < vlib_get_n_threads (); i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100654 stat_vm = vlib_get_main_by_index (i);
Dave Barach80f54e22017-03-08 19:08:56 -0500655 if (stat_vm)
656 vec_add1 (stat_vms, stat_vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400658
659 vlib_worker_thread_barrier_sync (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660
661 for (j = 0; j < vec_len (stat_vms); j++)
662 {
663 stat_vm = stat_vms[j];
664 nm = &stat_vm->node_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665
Dave Barach9b8ffd92016-07-08 08:13:45 -0400666 for (i = 0; i < vec_len (nm->nodes); i++)
667 {
668 n = nm->nodes[i];
669 vlib_node_sync_stats (stat_vm, n);
670 n->stats_last_clear = n->stats_total;
671
672 r = vlib_node_get_runtime (stat_vm, n->index);
673 r->max_clock = 0;
674 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675 /* Note: input/output rates computed using vlib_global_main */
676 nm->time_last_runtime_stats_clear = vlib_time_now (vm);
677 }
678
Damjan Marion8973b072022-03-01 15:51:18 +0100679 vlib_stats_set_timestamp (STAT_COUNTER_LAST_STATS_CLEAR,
680 vm->node_main.time_last_runtime_stats_clear);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400681 vlib_worker_thread_barrier_release (vm);
682
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 vec_free (stat_vms);
684
685 return 0;
686}
687
Dave Barach9b8ffd92016-07-08 08:13:45 -0400688/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689VLIB_CLI_COMMAND (clear_node_runtime_command, static) = {
690 .path = "clear runtime",
691 .short_help = "Clear packet processing runtime statistics",
692 .function = clear_node_runtime,
693};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400694/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695
Damjan Marion69abe442018-08-27 22:43:23 +0200696static clib_error_t *
697show_node (vlib_main_t * vm, unformat_input_t * input,
698 vlib_cli_command_t * cmd)
699{
700 unformat_input_t _line_input, *line_input = &_line_input;
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700701 clib_error_t *error = 0;
Damjan Marion69abe442018-08-27 22:43:23 +0200702 vlib_node_main_t *nm = &vm->node_main;
703 vlib_node_t *n;
704 u8 *s = 0, *s2 = 0;
Florin Coras178ecc02020-03-12 23:26:11 +0000705 u32 i, node_index = ~0, verbose = 0;
Damjan Marion69abe442018-08-27 22:43:23 +0200706 char *type_str;
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700707 u8 valid_node_name = 0;
Florin Coras178ecc02020-03-12 23:26:11 +0000708 u64 cl, ca, v;
Damjan Marion69abe442018-08-27 22:43:23 +0200709
710 if (!unformat_user (input, unformat_line_input, line_input))
711 return 0;
712
713 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
714 {
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700715 if (unformat (line_input, "index %u", &node_index))
Damjan Marion69abe442018-08-27 22:43:23 +0200716 ;
Florin Coras178ecc02020-03-12 23:26:11 +0000717 else if (unformat (line_input, "verbose"))
718 verbose = 1;
Damjan Marion69abe442018-08-27 22:43:23 +0200719 else
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700720 if (unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
721 valid_node_name = 1;
722 else if (!valid_node_name)
723 error = clib_error_return (0, "unknown node name: '%U'",
724 format_unformat_error, line_input);
725 else
726 error = clib_error_return (0, "unknown input '%U'",
727 format_unformat_error, line_input);
Filip Tehlarbe0ffbc2019-07-09 13:52:26 +0000728
729 if (error)
730 break;
Damjan Marion69abe442018-08-27 22:43:23 +0200731 }
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700732
Damjan Marion69abe442018-08-27 22:43:23 +0200733 unformat_free (line_input);
734
Paul Vinciguerra98afc712018-09-25 10:02:07 -0700735 if (error)
736 return error;
737
Damjan Marion69abe442018-08-27 22:43:23 +0200738 if (node_index >= vec_len (vm->node_main.nodes))
739 return clib_error_return (0, "please specify valid node");
740
741 n = vlib_get_node (vm, node_index);
742 vlib_node_sync_stats (vm, n);
743
744 switch (n->type)
745 {
746 case VLIB_NODE_TYPE_INTERNAL:
747 type_str = "internal";
748 break;
749 case VLIB_NODE_TYPE_INPUT:
750 type_str = "input";
751 break;
752 case VLIB_NODE_TYPE_PRE_INPUT:
753 type_str = "pre-input";
754 break;
755 case VLIB_NODE_TYPE_PROCESS:
756 type_str = "process";
757 break;
758 default:
759 type_str = "unknown";
760 }
761
762 if (n->sibling_of)
763 s = format (s, ", sibling-of %s", n->sibling_of);
764
Benoît Ganne62d9fda2019-12-16 15:49:47 +0100765 vlib_cli_output (vm, "node %v, type %s, state %U, index %d%v\n",
Damjan Marion69abe442018-08-27 22:43:23 +0200766 n->name, type_str, format_vlib_node_state, vm, n,
767 n->index, s);
768 vec_reset_length (s);
769
770 if (n->node_fn_registrations)
771 {
772 vlib_node_fn_registration_t *fnr = n->node_fn_registrations;
Damjan Mariona31698b2021-03-10 14:35:28 +0100773 vlib_node_fn_variant_t *v;
Damjan Marion69abe442018-08-27 22:43:23 +0200774 while (fnr)
775 {
Damjan Mariona31698b2021-03-10 14:35:28 +0100776 v = vec_elt_at_index (vm->node_main.variants, fnr->march_variant);
Damjan Marion69abe442018-08-27 22:43:23 +0200777 if (vec_len (s) == 0)
Damjan Mariona31698b2021-03-10 14:35:28 +0100778 s = format (s, "\n %-15s %=8s %6s %s", "Name", "Priority",
779 "Active", "Description");
780 s = format (s, "\n %-15s %8d %=6s %s", v->suffix, v->priority,
781 fnr->function == n->function ? "yes" : "", v->desc);
Damjan Marion69abe442018-08-27 22:43:23 +0200782 fnr = fnr->next_registration;
783 }
784 }
785 else
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700786 s = format (s, "\n default only");
Damjan Marion69abe442018-08-27 22:43:23 +0200787 vlib_cli_output (vm, " node function variants:%v\n", s);
788 vec_reset_length (s);
789
790 for (i = 0; i < vec_len (n->next_nodes); i++)
791 {
792 vlib_node_t *pn;
793 if (n->next_nodes[i] == VLIB_INVALID_NODE_INDEX)
794 continue;
795
796 pn = vec_elt (nm->nodes, n->next_nodes[i]);
797
798 if (vec_len (s) == 0)
799 s = format (s, "\n %10s %10s %=30s %8s",
800 "next-index", "node-index", "Node", "Vectors");
801
Florin Coras178ecc02020-03-12 23:26:11 +0000802 s = format (s, "\n %=10u %=10u %=30v %=8llu", i, n->next_nodes[i],
Damjan Marion69abe442018-08-27 22:43:23 +0200803 pn->name, vec_elt (n->n_vectors_by_next_node, i));
804 }
805
806 if (vec_len (s) == 0)
807 s = format (s, "\n none");
808 vlib_cli_output (vm, "\n next nodes:%v\n", s);
809 vec_reset_length (s);
810
811 if (n->type == VLIB_NODE_TYPE_INTERNAL)
812 {
813 int j = 0;
814 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100815 clib_bitmap_foreach (i, n->prev_node_bitmap) {
Damjan Marion69abe442018-08-27 22:43:23 +0200816 vlib_node_t *pn = vlib_get_node (vm, i);
817 if (j++ % 3 == 0)
818 s = format (s, "\n ");
819 s2 = format (s2, "%v (%u)", pn->name, i);
820 s = format (s, "%-35v", s2);
821 vec_reset_length (s2);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100822 }
Damjan Marion69abe442018-08-27 22:43:23 +0200823 /* *INDENT-ON* */
824
825 if (vec_len (s) == 0)
826 s = format (s, "\n none");
827 vlib_cli_output (vm, "\n known previous nodes:%v\n", s);
828 vec_reset_length (s);
Florin Coras178ecc02020-03-12 23:26:11 +0000829 vec_free (s2);
Damjan Marion69abe442018-08-27 22:43:23 +0200830 }
831
Florin Coras178ecc02020-03-12 23:26:11 +0000832 if (!verbose)
833 goto done;
834
835 s = format (s, "\n%8s %=12s %=12s %=12s %=12s %=12s\n", "Thread", "Calls",
836 "Clocks", "Vectors", "Max Clock", "Max Vectors");
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100837 for (i = 0; i < vlib_get_n_threads (); i++)
Florin Coras178ecc02020-03-12 23:26:11 +0000838 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100839 n = vlib_get_node (vlib_get_main_by_index (i), node_index);
840 vlib_node_sync_stats (vlib_get_main_by_index (i), n);
Florin Coras178ecc02020-03-12 23:26:11 +0000841
842 cl = n->stats_total.clocks - n->stats_last_clear.clocks;
843 ca = n->stats_total.calls - n->stats_last_clear.calls;
844 v = n->stats_total.vectors - n->stats_last_clear.vectors;
845
846 s = format (s, "%=8u %=12lu %=12lu %=12lu %=12u %=12u\n", i, ca, cl, v,
847 n->stats_total.max_clock, n->stats_total.max_clock_n);
848 }
849
850 vlib_cli_output (vm, "%v", s);
851
852done:
853
Damjan Marion69abe442018-08-27 22:43:23 +0200854 vec_free (s);
Damjan Marion69abe442018-08-27 22:43:23 +0200855 return 0;
856}
857
858/* *INDENT-OFF* */
859VLIB_CLI_COMMAND (show_node_command, static) = {
860 .path = "show node",
861 .short_help = "show node [index] <node-name | node-index>",
862 .function = show_node,
863};
864
865static clib_error_t *
866set_node_fn(vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
867{
868 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Mariona31698b2021-03-10 14:35:28 +0100869 u32 node_index, march_variant;
Damjan Marion69abe442018-08-27 22:43:23 +0200870 vlib_node_t *n;
871 clib_error_t *err = 0;
Damjan Marion69abe442018-08-27 22:43:23 +0200872
873 if (!unformat_user (input, unformat_line_input, line_input))
874 return 0;
875
876 if (!unformat (line_input, "%U", unformat_vlib_node, vm, &node_index))
877 {
878 err = clib_error_return (0, "please specify valid node name");
879 goto done;
880 }
881
Damjan Mariona31698b2021-03-10 14:35:28 +0100882 if (!unformat (line_input, "%U", unformat_vlib_node_variant, &march_variant))
Damjan Marion69abe442018-08-27 22:43:23 +0200883 {
Damjan Mariona31698b2021-03-10 14:35:28 +0100884 err = clib_error_return (0, "please specify node function variant");
Damjan Marion69abe442018-08-27 22:43:23 +0200885 goto done;
886 }
887
888 n = vlib_get_node (vm, node_index);
889
890 if (n->node_fn_registrations == 0)
891 {
Damjan Mariona31698b2021-03-10 14:35:28 +0100892 err = clib_error_return (0, "node doesn't have function variants");
Damjan Marion69abe442018-08-27 22:43:23 +0200893 goto done;
894 }
895
Damjan Mariona31698b2021-03-10 14:35:28 +0100896 if (vlib_node_set_march_variant (vm, node_index, march_variant))
Damjan Marion69abe442018-08-27 22:43:23 +0200897 {
Damjan Mariona31698b2021-03-10 14:35:28 +0100898 vlib_node_fn_variant_t *v;
899 v = vec_elt_at_index (vm->node_main.variants, march_variant);
900 err = clib_error_return (0, "node function variant '%s' not found",
901 v->suffix);
902 goto done;
Damjan Marion69abe442018-08-27 22:43:23 +0200903 }
904
Damjan Marion69abe442018-08-27 22:43:23 +0200905
906done:
Damjan Marion69abe442018-08-27 22:43:23 +0200907 unformat_free (line_input);
908 return err;
909}
910
911/* *INDENT-OFF* */
912VLIB_CLI_COMMAND (set_node_fn_command, static) = {
913 .path = "set node function",
914 .short_help = "set node function <node-name> <variant-name>",
915 .function = set_node_fn,
916};
917/* *INDENT-ON* */
918
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919/* Dummy function to get us linked in. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400920void
921vlib_node_cli_reference (void)
922{
923}
924
925/*
926 * fd.io coding-style-patch-verification: ON
927 *
928 * Local Variables:
929 * eval: (c-set-style "gnu")
930 * End:
931 */