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 * Serialize a vlib_node_main_t. Appends the result to vector.
46 * Pass 0 to create a new vector, use vec_reset_length(vector)
47 * to recycle a vector / avoid memory allocation, etc.
48 * Switch heaps before/after to serialize into API client shared memory.
51 vlib_node_serialize (vlib_main_t * vm, vlib_node_t *** node_dups, u8 * vector,
52 int include_nexts, int include_stats)
54 serialize_main_t _sm, *sm = &_sm;
61 state_string_enum_t state_code;
63 serialize_open_vector (sm, vector);
64 serialize_likely_small_unsigned_integer (sm, vec_len (node_dups));
66 for (j = 0; j < vec_len (node_dups); j++)
70 serialize_likely_small_unsigned_integer (sm, vec_len (nodes));
72 for (i = 0; i < vec_len (nodes); i++)
76 l = n->stats_total.clocks - n->stats_last_clear.clocks;
77 v = n->stats_total.vectors - n->stats_last_clear.vectors;
78 c = n->stats_total.calls - n->stats_last_clear.calls;
79 d = n->stats_total.suspends - n->stats_last_clear.suspends;
80 pmc = n->stats_total.perf_counter_ticks
81 - n->stats_last_clear.perf_counter_ticks;
83 state_code = STATE_INTERNAL;
85 if (n->type == VLIB_NODE_TYPE_PROCESS)
87 vlib_process_t *p = vlib_get_process_from_node (vm, n);
90 & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
91 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
94 if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
95 state_code = STATE_DONE;
98 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
99 state_code = STATE_TIME_WAIT;
102 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
103 state_code = STATE_EVENT_WAIT;
106 case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
112 else if (n->type != VLIB_NODE_TYPE_INTERNAL)
114 state_code = STATE_POLLING;
115 if (n->state == VLIB_NODE_STATE_DISABLED)
116 state_code = STATE_DISABLED;
117 else if (n->state == VLIB_NODE_STATE_INTERRUPT)
118 state_code = STATE_INTERRUPT_WAIT;
121 /* See unserialize_cstring */
122 name_bytes = vec_len (n->name);
123 serialize_likely_small_unsigned_integer (sm, name_bytes);
124 namep = serialize_get (sm, name_bytes);
125 memcpy (namep, n->name, name_bytes);
127 serialize_likely_small_unsigned_integer (sm, (u64) state_code);
128 serialize_likely_small_unsigned_integer (sm, n->type);
129 serialize_likely_small_unsigned_integer (sm, n->flags);
133 serialize_likely_small_unsigned_integer
134 (sm, vec_len (n->next_nodes));
135 for (k = 0; k < vec_len (n->next_nodes); k++)
136 serialize_likely_small_unsigned_integer (sm,
140 serialize_likely_small_unsigned_integer (sm, 0);
145 serialize_likely_small_unsigned_integer (sm, 1);
147 serialize_integer (sm, l, 8);
149 serialize_integer (sm, c, 8);
151 serialize_integer (sm, v, 8);
153 serialize_integer (sm, d, 8);
155 serialize_integer (sm, pmc, 8);
158 serialize_likely_small_unsigned_integer (sm, 0);
161 return (serialize_close_vector (sm));
165 vlib_node_unserialize (u8 * vector)
167 serialize_main_t _sm, *sm = &_sm;
172 vlib_node_t ***nodes_by_thread = 0;
175 state_string_enum_t state_code;
178 serialize_open_vector (sm, vector);
180 nstat_vms = unserialize_likely_small_unsigned_integer (sm);
182 vec_validate (nodes_by_thread, nstat_vms - 1);
183 _vec_len (nodes_by_thread) = 0;
185 for (i = 0; i < nstat_vms; i++)
187 nnodes = unserialize_likely_small_unsigned_integer (sm);
190 vec_validate (nodes, nnodes - 1);
191 vec_add1 (nodes_by_thread, nodes);
193 for (j = 0; j < nnodes; j++)
196 vec_validate (node, 0);
199 unserialize_cstring (sm, (char **) &(node->name));
200 state_code = unserialize_likely_small_unsigned_integer (sm);
201 node->state_string = (u8 *) state_strings[state_code];
203 node->type = unserialize_likely_small_unsigned_integer (sm);
204 node->flags = unserialize_likely_small_unsigned_integer (sm);
205 nnexts = unserialize_likely_small_unsigned_integer (sm);
207 vec_validate (node->next_nodes, nnexts - 1);
208 for (k = 0; k < nnexts; k++)
209 node->next_nodes[k] =
210 unserialize_likely_small_unsigned_integer (sm);
212 stats_present = unserialize_likely_small_unsigned_integer (sm);
217 unserialize_integer (sm, &l, 8);
218 node->stats_total.clocks = l;
219 node->stats_last_clear.clocks = 0;
222 unserialize_integer (sm, &c, 8);
223 node->stats_total.calls = c;
226 unserialize_integer (sm, &v, 8);
227 node->stats_total.vectors = v;
230 unserialize_integer (sm, &d, 8);
231 node->stats_total.suspends = d;
233 unserialize_integer (sm, &pmc, 8);
234 node->stats_total.perf_counter_ticks = pmc;
238 return nodes_by_thread;
243 static clib_error_t *
244 test_node_serialize_command_fn (vlib_main_t * vm,
245 unformat_input_t * input,
246 vlib_cli_command_t * cmd)
248 vlib_node_main_t *nm = &vm->node_main;
250 vlib_node_t ***nodes_by_thread;
253 vlib_node_t *next_node;
255 u32 max_threads = (u32) ~ 0;
256 int include_nexts = 0;
257 int include_stats = 0;
259 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
261 if (unformat (input, "max-threads %d", &max_threads))
263 else if (unformat (input, "stats"))
265 else if (unformat (input, "nexts"))
272 * Keep the number of memcpy ops to a minimum (e.g. 1).
273 * The current size of the serialized vector is
276 vec_validate (vector, 16383);
277 vec_reset_length (vector);
279 vector = vlib_node_serialize (nm, vector, max_threads,
280 include_nexts, include_stats);
282 vlib_cli_output (vm, "result vector %d bytes", vec_len (vector));
284 nodes_by_thread = vlib_node_unserialize (vector);
288 for (i = 0; i < vec_len (nodes_by_thread); i++)
290 nodes = nodes_by_thread[i];
292 vlib_cli_output (vm, "thread %d", i);
294 for (j = 0; j < vec_len (nodes); j++)
298 vlib_cli_output (vm, "[%d] %s state %s", j, node->name,
302 (vm, " clocks %lld calls %lld suspends"
303 " %lld vectors %lld",
304 node->stats_total.clocks,
305 node->stats_total.calls,
306 node->stats_total.suspends, node->stats_total.vectors);
308 for (k = 0; k < vec_len (node->next_nodes); k++)
310 if (node->next_nodes[k] != ~0)
312 next_node = nodes[node->next_nodes[k]];
313 vlib_cli_output (vm, " [%d] %s", k, next_node->name);
319 for (j = 0; j < vec_len (nodes_by_thread); j++)
321 nodes = nodes_by_thread[j];
323 for (i = 0; i < vec_len (nodes); i++)
325 vec_free (nodes[i]->name);
326 vec_free (nodes[i]->next_nodes);
331 vec_free (nodes_by_thread);
337 VLIB_CLI_COMMAND (test_node_serialize_node, static) = {
338 .path = "test node serialize",
339 .short_help = "test node serialize [max-threads NN] nexts stats",
340 .function = test_node_serialize_command_fn,
346 * fd.io coding-style-patch-verification: ON
349 * eval: (c-set-style "gnu")