2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vlib/vlib.h>
17 #include <vppinfra/serialize.h>
19 /* serialized representation of state strings */
21 #define foreach_state_string_code \
22 _(STATE_DONE, "done") \
23 _(STATE_DISABLED, "disabled") \
24 _(STATE_TIME_WAIT, "time wait") \
25 _(STATE_EVENT_WAIT, "event wait") \
26 _(STATE_ANY_WAIT, "any wait") \
27 _(STATE_POLLING, "polling") \
28 _(STATE_INTERRUPT_WAIT, "interrupt wait") \
29 _(STATE_INTERNAL, "internal")
34 foreach_state_string_code
36 } state_string_enum_t;
38 static char *state_strings[] = {
40 foreach_state_string_code
45 vlib_node_unserialize (u8 * vector)
47 serialize_main_t _sm, *sm = &_sm;
52 vlib_node_t ***nodes_by_thread = 0;
55 state_string_enum_t state_code;
58 serialize_open_vector (sm, vector);
60 nstat_vms = unserialize_likely_small_unsigned_integer (sm);
62 vec_validate (nodes_by_thread, nstat_vms - 1);
63 _vec_len (nodes_by_thread) = 0;
65 for (i = 0; i < nstat_vms; i++)
67 nnodes = unserialize_likely_small_unsigned_integer (sm);
70 vec_validate (nodes, nnodes - 1);
71 vec_add1 (nodes_by_thread, nodes);
73 for (j = 0; j < nnodes; j++)
76 vec_validate (node, 0);
79 unserialize_cstring (sm, (char **) &(node->name));
80 state_code = unserialize_likely_small_unsigned_integer (sm);
81 node->state_string = (u8 *) state_strings[state_code];
83 node->type = unserialize_likely_small_unsigned_integer (sm);
84 nnexts = unserialize_likely_small_unsigned_integer (sm);
86 vec_validate (node->next_nodes, nnexts - 1);
87 for (k = 0; k < nnexts; k++)
89 unserialize_likely_small_unsigned_integer (sm);
91 stats_present = unserialize_likely_small_unsigned_integer (sm);
96 unserialize_integer (sm, &l, 8);
97 node->stats_total.clocks = l;
98 node->stats_last_clear.clocks = 0;
101 unserialize_integer (sm, &c, 8);
102 node->stats_total.calls = c;
105 unserialize_integer (sm, &v, 8);
106 node->stats_total.vectors = v;
109 unserialize_integer (sm, &d, 8);
110 node->stats_total.suspends = d;
114 return nodes_by_thread;
119 static clib_error_t *
120 test_node_serialize_command_fn (vlib_main_t * vm,
121 unformat_input_t * input,
122 vlib_cli_command_t * cmd)
124 vlib_node_main_t *nm = &vm->node_main;
126 vlib_node_t ***nodes_by_thread;
129 vlib_node_t *next_node;
131 u32 max_threads = (u32) ~ 0;
132 int include_nexts = 0;
133 int include_stats = 0;
135 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
137 if (unformat (input, "max-threads %d", &max_threads))
139 else if (unformat (input, "stats"))
141 else if (unformat (input, "nexts"))
148 * Keep the number of memcpy ops to a minimum (e.g. 1).
149 * The current size of the serialized vector is
152 vec_validate (vector, 16383);
153 vec_reset_length (vector);
155 vector = vlib_node_serialize (nm, vector, max_threads,
156 include_nexts, include_stats);
158 vlib_cli_output (vm, "result vector %d bytes", vec_len (vector));
160 nodes_by_thread = vlib_node_unserialize (vector);
164 for (i = 0; i < vec_len (nodes_by_thread); i++)
166 nodes = nodes_by_thread[i];
168 vlib_cli_output (vm, "thread %d", i);
170 for (j = 0; j < vec_len (nodes); j++)
174 vlib_cli_output (vm, "[%d] %s state %s", j, node->name,
178 (vm, " clocks %lld calls %lld suspends"
179 " %lld vectors %lld",
180 node->stats_total.clocks,
181 node->stats_total.calls,
182 node->stats_total.suspends, node->stats_total.vectors);
184 for (k = 0; k < vec_len (node->next_nodes); k++)
186 if (node->next_nodes[k] != ~0)
188 next_node = nodes[node->next_nodes[k]];
189 vlib_cli_output (vm, " [%d] %s", k, next_node->name);
195 for (j = 0; j < vec_len (nodes_by_thread); j++)
197 nodes = nodes_by_thread[j];
199 for (i = 0; i < vec_len (nodes); i++)
201 vec_free (nodes[i]->name);
202 vec_free (nodes[i]->next_nodes);
207 vec_free (nodes_by_thread);
213 VLIB_CLI_COMMAND (test_node_serialize_node, static) = {
214 .path = "test node serialize",
215 .short_help = "test node serialize [max-threads NN] nexts stats",
216 .function = test_node_serialize_command_fn,
222 * fd.io coding-style-patch-verification: ON
225 * eval: (c-set-style "gnu")