blob: 575de11b8cc32babfd1519b8a8f509d8624f411b [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;
60 u64 l, v, c, d;
61 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 Barach6931f592016-05-13 12:55:01 -040080
Dave Barach371e4e12016-07-08 09:38:52 -040081 state_code = STATE_INTERNAL;
Dave Barach6931f592016-05-13 12:55:01 -040082
Dave Barach371e4e12016-07-08 09:38:52 -040083 if (n->type == VLIB_NODE_TYPE_PROCESS)
84 {
85 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Dave Barach6931f592016-05-13 12:55:01 -040086
Dave Barach371e4e12016-07-08 09:38:52 -040087 switch (p->flags
88 & (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
89 | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT))
90 {
91 default:
92 if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
93 state_code = STATE_DONE;
94 break;
Dave Barach6931f592016-05-13 12:55:01 -040095
Dave Barach371e4e12016-07-08 09:38:52 -040096 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK:
97 state_code = STATE_TIME_WAIT;
98 break;
Dave Barach6931f592016-05-13 12:55:01 -040099
Dave Barach371e4e12016-07-08 09:38:52 -0400100 case VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT:
101 state_code = STATE_EVENT_WAIT;
102 break;
Dave Barach6931f592016-05-13 12:55:01 -0400103
Dave Barach371e4e12016-07-08 09:38:52 -0400104 case (VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK):
105 state_code =
106 STATE_ANY_WAIT;
107 break;
108 }
109 }
110 else if (n->type != VLIB_NODE_TYPE_INTERNAL)
111 {
112 state_code = STATE_POLLING;
113 if (n->state == VLIB_NODE_STATE_DISABLED)
114 state_code = STATE_DISABLED;
115 else if (n->state == VLIB_NODE_STATE_INTERRUPT)
116 state_code = STATE_INTERRUPT_WAIT;
117 }
Dave Barach6931f592016-05-13 12:55:01 -0400118
Dave Barach371e4e12016-07-08 09:38:52 -0400119 /* See unserialize_cstring */
120 name_bytes = vec_len (n->name);
121 serialize_likely_small_unsigned_integer (sm, name_bytes);
122 namep = serialize_get (sm, name_bytes);
123 memcpy (namep, n->name, name_bytes);
124
125 serialize_likely_small_unsigned_integer (sm, (u64) state_code);
126 serialize_likely_small_unsigned_integer (sm, n->type);
127
128 if (include_nexts)
129 {
130 serialize_likely_small_unsigned_integer
131 (sm, vec_len (n->next_nodes));
132 for (k = 0; k < vec_len (n->next_nodes); k++)
133 serialize_likely_small_unsigned_integer (sm,
134 n->next_nodes[k]);
135 }
136 else
137 serialize_likely_small_unsigned_integer (sm, 0);
138
139 if (include_stats)
140 {
141 /* stats present */
142 serialize_likely_small_unsigned_integer (sm, 1);
143 /* total clocks */
144 serialize_integer (sm, l, 8);
145 /* Total calls */
146 serialize_integer (sm, c, 8);
147 /* Total vectors */
148 serialize_integer (sm, v, 8);
149 /* Total suspends */
150 serialize_integer (sm, d, 8);
151 }
152 else /* no stats */
153 serialize_likely_small_unsigned_integer (sm, 0);
154 }
Dave Barach6931f592016-05-13 12:55:01 -0400155 vec_free (nodes);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500156 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500157 return (serialize_close_vector (sm));
158}
159
Dave Barach371e4e12016-07-08 09:38:52 -0400160vlib_node_t ***
161vlib_node_unserialize (u8 * vector)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500162{
Dave Barach371e4e12016-07-08 09:38:52 -0400163 serialize_main_t _sm, *sm = &_sm;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500164 u32 nnodes, nnexts;
Dave Barach6931f592016-05-13 12:55:01 -0400165 u32 nstat_vms;
Dave Barach371e4e12016-07-08 09:38:52 -0400166 vlib_node_t *node;
167 vlib_node_t **nodes;
168 vlib_node_t ***nodes_by_thread = 0;
Dave Barach6931f592016-05-13 12:55:01 -0400169 int i, j, k;
170 u64 l, v, c, d;
171 state_string_enum_t state_code;
172 int stats_present;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500173
174 serialize_open_vector (sm, vector);
Dave Barach371e4e12016-07-08 09:38:52 -0400175
Dave Barach6931f592016-05-13 12:55:01 -0400176 nstat_vms = unserialize_likely_small_unsigned_integer (sm);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500177
Dave Barach6931f592016-05-13 12:55:01 -0400178 vec_validate (nodes_by_thread, nstat_vms - 1);
179 _vec_len (nodes_by_thread) = 0;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500180
Dave Barach6931f592016-05-13 12:55:01 -0400181 for (i = 0; i < nstat_vms; i++)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500182 {
Dave Barach6931f592016-05-13 12:55:01 -0400183 nnodes = unserialize_likely_small_unsigned_integer (sm);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500184
Dave Barach6931f592016-05-13 12:55:01 -0400185 nodes = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400186 vec_validate (nodes, nnodes - 1);
Dave Barach6931f592016-05-13 12:55:01 -0400187 vec_add1 (nodes_by_thread, nodes);
188
189 for (j = 0; j < nnodes; j++)
Dave Barach371e4e12016-07-08 09:38:52 -0400190 {
191 node = 0;
192 vec_validate (node, 0);
193 nodes[j] = node;
Dave Barach6931f592016-05-13 12:55:01 -0400194
Dave Barach371e4e12016-07-08 09:38:52 -0400195 unserialize_cstring (sm, (char **) &(node->name));
196 state_code = unserialize_likely_small_unsigned_integer (sm);
197 node->state_string = (u8 *) state_strings[state_code];
Dave Barach6931f592016-05-13 12:55:01 -0400198
Dave Barach371e4e12016-07-08 09:38:52 -0400199 node->type = unserialize_likely_small_unsigned_integer (sm);
200 nnexts = unserialize_likely_small_unsigned_integer (sm);
201 if (nnexts > 0)
202 vec_validate (node->next_nodes, nnexts - 1);
203 for (k = 0; k < nnexts; k++)
204 node->next_nodes[k] =
205 unserialize_likely_small_unsigned_integer (sm);
Dave Barach6931f592016-05-13 12:55:01 -0400206
Dave Barach371e4e12016-07-08 09:38:52 -0400207 stats_present = unserialize_likely_small_unsigned_integer (sm);
Dave Barach6931f592016-05-13 12:55:01 -0400208
Dave Barach371e4e12016-07-08 09:38:52 -0400209 if (stats_present)
210 {
211 /* total clocks */
212 unserialize_integer (sm, &l, 8);
213 node->stats_total.clocks = l;
214 node->stats_last_clear.clocks = 0;
215
216 /* Total calls */
217 unserialize_integer (sm, &c, 8);
218 node->stats_total.calls = c;
219
220 /* Total vectors */
221 unserialize_integer (sm, &v, 8);
222 node->stats_total.vectors = v;
223
224 /* Total suspends */
225 unserialize_integer (sm, &d, 8);
226 node->stats_total.suspends = d;
227 }
228 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500229 }
Dave Barach371e4e12016-07-08 09:38:52 -0400230 return nodes_by_thread;
Dave Barachb44e9bc2016-02-19 09:06:23 -0500231}
232
Dave Barach80f54e22017-03-08 19:08:56 -0500233#if TEST_CODE
Dave Barachb44e9bc2016-02-19 09:06:23 -0500234
235static clib_error_t *
236test_node_serialize_command_fn (vlib_main_t * vm,
Dave Barach371e4e12016-07-08 09:38:52 -0400237 unformat_input_t * input,
238 vlib_cli_command_t * cmd)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500239{
Dave Barach371e4e12016-07-08 09:38:52 -0400240 vlib_node_main_t *nm = &vm->node_main;
241 u8 *vector = 0;
242 vlib_node_t ***nodes_by_thread;
243 vlib_node_t **nodes;
244 vlib_node_t *node;
245 vlib_node_t *next_node;
Dave Barach6931f592016-05-13 12:55:01 -0400246 int i, j, k;
Dave Barach371e4e12016-07-08 09:38:52 -0400247 u32 max_threads = (u32) ~ 0;
Dave Barach6931f592016-05-13 12:55:01 -0400248 int include_nexts = 0;
Dave Barach371e4e12016-07-08 09:38:52 -0400249 int include_stats = 0;
Dave Barach6931f592016-05-13 12:55:01 -0400250
Dave Barach371e4e12016-07-08 09:38:52 -0400251 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Dave Barach6931f592016-05-13 12:55:01 -0400252 {
253 if (unformat (input, "max-threads %d", &max_threads))
Dave Barach371e4e12016-07-08 09:38:52 -0400254 ;
Dave Barach6931f592016-05-13 12:55:01 -0400255 else if (unformat (input, "stats"))
Dave Barach371e4e12016-07-08 09:38:52 -0400256 include_stats = 1;
Dave Barach6931f592016-05-13 12:55:01 -0400257 else if (unformat (input, "nexts"))
Dave Barach371e4e12016-07-08 09:38:52 -0400258 include_nexts = 1;
Dave Barach6931f592016-05-13 12:55:01 -0400259 else
Dave Barach371e4e12016-07-08 09:38:52 -0400260 break;
Dave Barach6931f592016-05-13 12:55:01 -0400261 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500262
Dave Barach371e4e12016-07-08 09:38:52 -0400263 /*
Dave Barachb44e9bc2016-02-19 09:06:23 -0500264 * Keep the number of memcpy ops to a minimum (e.g. 1).
265 * The current size of the serialized vector is
266 * slightly under 4K.
267 */
Dave Barach6931f592016-05-13 12:55:01 -0400268 vec_validate (vector, 16383);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500269 vec_reset_length (vector);
270
Dave Barach371e4e12016-07-08 09:38:52 -0400271 vector = vlib_node_serialize (nm, vector, max_threads,
272 include_nexts, include_stats);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500273
Dave Barach371e4e12016-07-08 09:38:52 -0400274 vlib_cli_output (vm, "result vector %d bytes", vec_len (vector));
Dave Barach6931f592016-05-13 12:55:01 -0400275
276 nodes_by_thread = vlib_node_unserialize (vector);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500277
278 vec_free (vector);
Dave Barach371e4e12016-07-08 09:38:52 -0400279
280 for (i = 0; i < vec_len (nodes_by_thread); i++)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500281 {
Dave Barach6931f592016-05-13 12:55:01 -0400282 nodes = nodes_by_thread[i];
283
284 vlib_cli_output (vm, "thread %d", i);
285
Dave Barach371e4e12016-07-08 09:38:52 -0400286 for (j = 0; j < vec_len (nodes); j++)
287 {
288 node = nodes[j];
Dave Barach6931f592016-05-13 12:55:01 -0400289
Dave Barach371e4e12016-07-08 09:38:52 -0400290 vlib_cli_output (vm, "[%d] %s state %s", j, node->name,
291 node->state_string);
Dave Barach6931f592016-05-13 12:55:01 -0400292
Dave Barach371e4e12016-07-08 09:38:52 -0400293 vlib_cli_output
294 (vm, " clocks %lld calls %lld suspends"
295 " %lld vectors %lld",
296 node->stats_total.clocks,
297 node->stats_total.calls,
298 node->stats_total.suspends, node->stats_total.vectors);
Dave Barach6931f592016-05-13 12:55:01 -0400299
Dave Barach371e4e12016-07-08 09:38:52 -0400300 for (k = 0; k < vec_len (node->next_nodes); k++)
301 {
302 if (node->next_nodes[k] != ~0)
Ed Warnicke853e7202016-08-12 11:42:26 -0700303 {
304 next_node = nodes[node->next_nodes[k]];
305 vlib_cli_output (vm, " [%d] %s", k, next_node->name);
306 }
Dave Barach371e4e12016-07-08 09:38:52 -0400307 }
308 }
309 }
Dave Barachb44e9bc2016-02-19 09:06:23 -0500310
Dave Barach371e4e12016-07-08 09:38:52 -0400311 for (j = 0; j < vec_len (nodes_by_thread); j++)
Dave Barachb44e9bc2016-02-19 09:06:23 -0500312 {
Dave Barach6931f592016-05-13 12:55:01 -0400313 nodes = nodes_by_thread[j];
314
Dave Barach371e4e12016-07-08 09:38:52 -0400315 for (i = 0; i < vec_len (nodes); i++)
316 {
317 vec_free (nodes[i]->name);
318 vec_free (nodes[i]->next_nodes);
319 vec_free (nodes[i]);
320 }
321 vec_free (nodes);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500322 }
Dave Barach6931f592016-05-13 12:55:01 -0400323 vec_free (nodes_by_thread);
Dave Barachb44e9bc2016-02-19 09:06:23 -0500324
325 return 0;
326}
327
Dave Barach371e4e12016-07-08 09:38:52 -0400328/* *INDENT-OFF* */
Dave Barachb44e9bc2016-02-19 09:06:23 -0500329VLIB_CLI_COMMAND (test_node_serialize_node, static) = {
330 .path = "test node serialize",
Dave Barach6931f592016-05-13 12:55:01 -0400331 .short_help = "test node serialize [max-threads NN] nexts stats",
Dave Barachb44e9bc2016-02-19 09:06:23 -0500332 .function = test_node_serialize_command_fn,
333};
Dave Barach371e4e12016-07-08 09:38:52 -0400334/* *INDENT-ON* */
Dave Barachb44e9bc2016-02-19 09:06:23 -0500335#endif
Dave Barach371e4e12016-07-08 09:38:52 -0400336
337/*
338 * fd.io coding-style-patch-verification: ON
339 *
340 * Local Variables:
341 * eval: (c-set-style "gnu")
342 * End:
343 */