blob: 0774eea251eccc9e0d19a445784f866dbc0f10fb [file] [log] [blame]
Dave Barachb44e9bc2016-02-19 09:06:23 -05001/*
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:
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#include <vlib/vlib.h>
16
17#include <vppinfra/serialize.h>
18
Dave Barach6931f592016-05-13 12:55:01 -040019/* serialized representation of state strings */
20
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")
30
Dave Barach371e4e12016-07-08 09:38:52 -040031typedef enum
32{
Dave Barach6931f592016-05-13 12:55:01 -040033#define _(a,b) a,
34 foreach_state_string_code
35#undef _
36} state_string_enum_t;
37
Dave Barach371e4e12016-07-08 09:38:52 -040038static char *state_strings[] = {
Dave Barach6931f592016-05-13 12:55:01 -040039#define _(a,b) b,
Dave Barach371e4e12016-07-08 09:38:52 -040040 foreach_state_string_code
Dave Barach6931f592016-05-13 12:55:01 -040041#undef _
Dave Barach371e4e12016-07-08 09:38:52 -040042};
Dave Barach6931f592016-05-13 12:55:01 -040043
Dave Barach371e4e12016-07-08 09:38:52 -040044/*
Dave Barachb44e9bc2016-02-19 09:06:23 -050045 * 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.
49 */
Dave Barach371e4e12016-07-08 09:38:52 -040050u8 *
Florin Corase86a8ed2018-01-05 03:20:25 -080051vlib_node_serialize (vlib_main_t * vm, vlib_node_t *** node_dups, u8 * vector,
52 int include_nexts, int include_stats)
Dave Barachb44e9bc2016-02-19 09:06:23 -050053{
Dave Barach371e4e12016-07-08 09:38:52 -040054 serialize_main_t _sm, *sm = &_sm;
Dave Barach371e4e12016-07-08 09:38:52 -040055 vlib_node_t *n;
Dave Barach371e4e12016-07-08 09:38:52 -040056 vlib_node_t **nodes;
Dave Barach371e4e12016-07-08 09:38:52 -040057 u8 *namep;
Dave Barach6931f592016-05-13 12:55:01 -040058 u32 name_bytes;
59 uword i, j, k;
Dave Barach4d1a8662018-09-10 12:31:15 -040060 u64 l, v, c, d, pmc;
Dave Barach6931f592016-05-13 12:55:01 -040061 state_string_enum_t state_code;
Dave Barach371e4e12016-07-08 09:38:52 -040062
Dave Barachb44e9bc2016-02-19 09:06:23 -050063 serialize_open_vector (sm, vector);
Florin Corase86a8ed2018-01-05 03:20:25 -080064 serialize_likely_small_unsigned_integer (sm, vec_len (node_dups));
Dave Barach6931f592016-05-13 12:55:01 -040065
Florin Corase86a8ed2018-01-05 03:20:25 -080066 for (j = 0; j < vec_len (node_dups); j++)
Dave Barachb44e9bc2016-02-19 09:06:23 -050067 {
Dave Barach6931f592016-05-13 12:55:01 -040068 nodes = node_dups[j];
69
Dave Barach371e4e12016-07-08 09:38:52 -040070 serialize_likely_small_unsigned_integer (sm, vec_len (nodes));
Dave Barach6931f592016-05-13 12:55:01 -040071
72 for (i = 0; i < vec_len (nodes); i++)
Dave Barach371e4e12016-07-08 09:38:52 -040073 {
74 n = nodes[i];
Dave Barach6931f592016-05-13 12:55:01 -040075
Dave Barach371e4e12016-07-08 09:38:52 -040076 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;
Dave Barach4d1a8662018-09-10 12:31:15 -040080 pmc = n->stats_total.perf_counter_ticks
81 - n->stats_last_clear.perf_counter_ticks;
Dave Barach6931f592016-05-13 12:55:01 -040082
Dave Barach371e4e12016-07-08 09:38:52 -040083 state_code = STATE_INTERNAL;
Dave Barach6931f592016-05-13 12:55:01 -040084
Dave Barach371e4e12016-07-08 09:38:52 -040085 if (n->type == VLIB_NODE_TYPE_PROCESS)
86 {
87 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Dave Barach6931f592016-05-13 12:55:01 -040088
Dave Barach371e4e12016-07-08 09:38:52 -040089 switch (p->flags
90 & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
91 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
92 {
93 default:
94 if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
95 state_code = STATE_DONE;
96 break;
Dave Barach6931f592016-05-13 12:55:01 -040097
Dave Barach371e4e12016-07-08 09:38:52 -040098 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
99 state_code = STATE_TIME_WAIT;
100 break;
Dave Barach6931f592016-05-13 12:55:01 -0400101
Dave Barach371e4e12016-07-08 09:38:52 -0400102 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
103 state_code = STATE_EVENT_WAIT;
104 break;
Dave Barach6931f592016-05-13 12:55:01 -0400105
Dave Barach371e4e12016-07-08 09:38:52 -0400106 case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
107 state_code =
108 STATE_ANY_WAIT;
109 break;
110 }
111 }
112 else if (n->type != VLIB_NODE_TYPE_INTERNAL)
113 {
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;
119 }
Dave Barach6931f592016-05-13 12:55:01 -0400120
Dave Barach371e4e12016-07-08 09:38:52 -0400121 /* 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);
126
127 serialize_likely_small_unsigned_integer (sm, (u64) state_code);
128 serialize_likely_small_unsigned_integer (sm, n->type);
Dave Barach1ddbc012018-06-13 09:26:05 -0400129 serialize_likely_small_unsigned_integer (sm, n->flags);
Dave Barach371e4e12016-07-08 09:38:52 -0400130
131 if (include_nexts)
132 {
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,
137 n->next_nodes[k]);
138 }
139 else
140 serialize_likely_small_unsigned_integer (sm, 0);
141
142 if (include_stats)
143 {
144 /* stats present */
145 serialize_likely_small_unsigned_integer (sm, 1);
146 /* total clocks */
147 serialize_integer (sm, l, 8);
148 /* Total calls */
149 serialize_integer (sm, c, 8);
150 /* Total vectors */
151 serialize_integer (sm, v, 8);
152 /* Total suspends */
153 serialize_integer (sm, d, 8);
Dave Barach4d1a8662018-09-10 12:31:15 -0400154 /* PMC counter */
155 serialize_integer (sm, pmc, 8);
Dave Barach371e4e12016-07-08 09:38:52 -0400156 }
157 else /* no stats */
158 serialize_likely_small_unsigned_integer (sm, 0);
159 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500160 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500161 return (serialize_close_vector (sm));
162}
163
Dave Barach371e4e12016-07-08 09:38:52 -0400164vlib_node_t ***
165vlib_node_unserialize (u8 * vector)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500166{
Dave Barach371e4e12016-07-08 09:38:52 -0400167 serialize_main_t _sm, *sm = &_sm;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500168 u32 nnodes, nnexts;
Dave Barach6931f592016-05-13 12:55:01 -0400169 u32 nstat_vms;
Dave Barach371e4e12016-07-08 09:38:52 -0400170 vlib_node_t *node;
171 vlib_node_t **nodes;
172 vlib_node_t ***nodes_by_thread = 0;
Dave Barach6931f592016-05-13 12:55:01 -0400173 int i, j, k;
Dave Barach4d1a8662018-09-10 12:31:15 -0400174 u64 l, v, c, d, pmc;
Dave Barach6931f592016-05-13 12:55:01 -0400175 state_string_enum_t state_code;
176 int stats_present;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500177
178 serialize_open_vector (sm, vector);
Dave Barach371e4e12016-07-08 09:38:52 -0400179
Dave Barach6931f592016-05-13 12:55:01 -0400180 nstat_vms = unserialize_likely_small_unsigned_integer (sm);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500181
Dave Barach6931f592016-05-13 12:55:01 -0400182 vec_validate (nodes_by_thread, nstat_vms - 1);
183 _vec_len (nodes_by_thread) = 0;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500184
Dave Barach6931f592016-05-13 12:55:01 -0400185 for (i = 0; i < nstat_vms; i++)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500186 {
Dave Barach6931f592016-05-13 12:55:01 -0400187 nnodes = unserialize_likely_small_unsigned_integer (sm);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500188
Dave Barach6931f592016-05-13 12:55:01 -0400189 nodes = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400190 vec_validate (nodes, nnodes - 1);
Dave Barach6931f592016-05-13 12:55:01 -0400191 vec_add1 (nodes_by_thread, nodes);
192
193 for (j = 0; j < nnodes; j++)
Dave Barach371e4e12016-07-08 09:38:52 -0400194 {
195 node = 0;
196 vec_validate (node, 0);
197 nodes[j] = node;
Dave Barach6931f592016-05-13 12:55:01 -0400198
Dave Barach371e4e12016-07-08 09:38:52 -0400199 unserialize_cstring (sm, (char **) &(node->name));
200 state_code = unserialize_likely_small_unsigned_integer (sm);
201 node->state_string = (u8 *) state_strings[state_code];
Dave Barach6931f592016-05-13 12:55:01 -0400202
Dave Barach371e4e12016-07-08 09:38:52 -0400203 node->type = unserialize_likely_small_unsigned_integer (sm);
Dave Barach1ddbc012018-06-13 09:26:05 -0400204 node->flags = unserialize_likely_small_unsigned_integer (sm);
Dave Barach371e4e12016-07-08 09:38:52 -0400205 nnexts = unserialize_likely_small_unsigned_integer (sm);
206 if (nnexts > 0)
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);
Dave Barach6931f592016-05-13 12:55:01 -0400211
Dave Barach371e4e12016-07-08 09:38:52 -0400212 stats_present = unserialize_likely_small_unsigned_integer (sm);
Dave Barach6931f592016-05-13 12:55:01 -0400213
Dave Barach371e4e12016-07-08 09:38:52 -0400214 if (stats_present)
215 {
216 /* total clocks */
217 unserialize_integer (sm, &l, 8);
218 node->stats_total.clocks = l;
219 node->stats_last_clear.clocks = 0;
220
221 /* Total calls */
222 unserialize_integer (sm, &c, 8);
223 node->stats_total.calls = c;
224
225 /* Total vectors */
226 unserialize_integer (sm, &v, 8);
227 node->stats_total.vectors = v;
228
229 /* Total suspends */
230 unserialize_integer (sm, &d, 8);
231 node->stats_total.suspends = d;
Dave Barach4d1a8662018-09-10 12:31:15 -0400232 /* PMC counter */
233 unserialize_integer (sm, &pmc, 8);
234 node->stats_total.perf_counter_ticks = pmc;
Dave Barach371e4e12016-07-08 09:38:52 -0400235 }
236 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500237 }
Dave Barach371e4e12016-07-08 09:38:52 -0400238 return nodes_by_thread;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500239}
240
Dave Barach80f54e22017-03-08 19:08:56 -0500241#if TEST_CODE
Dave Barachb44e9bc2016-02-19 09:06:23 -0500242
243static clib_error_t *
244test_node_serialize_command_fn (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400245 unformat_input_t * input,
246 vlib_cli_command_t * cmd)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500247{
Dave Barach371e4e12016-07-08 09:38:52 -0400248 vlib_node_main_t *nm = &vm->node_main;
249 u8 *vector = 0;
250 vlib_node_t ***nodes_by_thread;
251 vlib_node_t **nodes;
252 vlib_node_t *node;
253 vlib_node_t *next_node;
Dave Barach6931f592016-05-13 12:55:01 -0400254 int i, j, k;
Dave Barach371e4e12016-07-08 09:38:52 -0400255 u32 max_threads = (u32) ~ 0;
Dave Barach6931f592016-05-13 12:55:01 -0400256 int include_nexts = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400257 int include_stats = 0;
Dave Barach6931f592016-05-13 12:55:01 -0400258
Dave Barach371e4e12016-07-08 09:38:52 -0400259 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Dave Barach6931f592016-05-13 12:55:01 -0400260 {
261 if (unformat (input, "max-threads %d", &max_threads))
Dave Barach371e4e12016-07-08 09:38:52 -0400262 ;
Dave Barach6931f592016-05-13 12:55:01 -0400263 else if (unformat (input, "stats"))
Dave Barach371e4e12016-07-08 09:38:52 -0400264 include_stats = 1;
Dave Barach6931f592016-05-13 12:55:01 -0400265 else if (unformat (input, "nexts"))
Dave Barach371e4e12016-07-08 09:38:52 -0400266 include_nexts = 1;
Dave Barach6931f592016-05-13 12:55:01 -0400267 else
Dave Barach371e4e12016-07-08 09:38:52 -0400268 break;
Dave Barach6931f592016-05-13 12:55:01 -0400269 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500270
Dave Barach371e4e12016-07-08 09:38:52 -0400271 /*
Dave Barachb44e9bc2016-02-19 09:06:23 -0500272 * Keep the number of memcpy ops to a minimum (e.g. 1).
273 * The current size of the serialized vector is
274 * slightly under 4K.
275 */
Dave Barach6931f592016-05-13 12:55:01 -0400276 vec_validate (vector, 16383);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500277 vec_reset_length (vector);
278
Dave Barach371e4e12016-07-08 09:38:52 -0400279 vector = vlib_node_serialize (nm, vector, max_threads,
280 include_nexts, include_stats);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500281
Dave Barach371e4e12016-07-08 09:38:52 -0400282 vlib_cli_output (vm, "result vector %d bytes", vec_len (vector));
Dave Barach6931f592016-05-13 12:55:01 -0400283
284 nodes_by_thread = vlib_node_unserialize (vector);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500285
286 vec_free (vector);
Dave Barach371e4e12016-07-08 09:38:52 -0400287
288 for (i = 0; i < vec_len (nodes_by_thread); i++)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500289 {
Dave Barach6931f592016-05-13 12:55:01 -0400290 nodes = nodes_by_thread[i];
291
292 vlib_cli_output (vm, "thread %d", i);
293
Dave Barach371e4e12016-07-08 09:38:52 -0400294 for (j = 0; j < vec_len (nodes); j++)
295 {
296 node = nodes[j];
Dave Barach6931f592016-05-13 12:55:01 -0400297
Dave Barach371e4e12016-07-08 09:38:52 -0400298 vlib_cli_output (vm, "[%d] %s state %s", j, node->name,
299 node->state_string);
Dave Barach6931f592016-05-13 12:55:01 -0400300
Dave Barach371e4e12016-07-08 09:38:52 -0400301 vlib_cli_output
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);
Dave Barach6931f592016-05-13 12:55:01 -0400307
Dave Barach371e4e12016-07-08 09:38:52 -0400308 for (k = 0; k < vec_len (node->next_nodes); k++)
309 {
310 if (node->next_nodes[k] != ~0)
Ed Warnicke853e7202016-08-12 11:42:26 -0700311 {
312 next_node = nodes[node->next_nodes[k]];
313 vlib_cli_output (vm, " [%d] %s", k, next_node->name);
314 }
Dave Barach371e4e12016-07-08 09:38:52 -0400315 }
316 }
317 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500318
Dave Barach371e4e12016-07-08 09:38:52 -0400319 for (j = 0; j < vec_len (nodes_by_thread); j++)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500320 {
Dave Barach6931f592016-05-13 12:55:01 -0400321 nodes = nodes_by_thread[j];
322
Dave Barach371e4e12016-07-08 09:38:52 -0400323 for (i = 0; i < vec_len (nodes); i++)
324 {
325 vec_free (nodes[i]->name);
326 vec_free (nodes[i]->next_nodes);
327 vec_free (nodes[i]);
328 }
329 vec_free (nodes);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500330 }
Dave Barach6931f592016-05-13 12:55:01 -0400331 vec_free (nodes_by_thread);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500332
333 return 0;
334}
335
Dave Barach371e4e12016-07-08 09:38:52 -0400336/* *INDENT-OFF* */
Dave Barachb44e9bc2016-02-19 09:06:23 -0500337VLIB_CLI_COMMAND (test_node_serialize_node, static) = {
338 .path = "test node serialize",
Dave Barach6931f592016-05-13 12:55:01 -0400339 .short_help = "test node serialize [max-threads NN] nexts stats",
Dave Barachb44e9bc2016-02-19 09:06:23 -0500340 .function = test_node_serialize_command_fn,
341};
Dave Barach371e4e12016-07-08 09:38:52 -0400342/* *INDENT-ON* */
Dave Barachb44e9bc2016-02-19 09:06:23 -0500343#endif
Dave Barach371e4e12016-07-08 09:38:52 -0400344
345/*
346 * fd.io coding-style-patch-verification: ON
347 *
348 * Local Variables:
349 * eval: (c-set-style "gnu")
350 * End:
351 */