FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
node_serialize.c
Go to the documentation of this file.
1 /*
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 
19 /* 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 
31 typedef enum
32 {
33 #define _(a,b) a,
35 #undef _
37 
38 static char *state_strings[] = {
39 #define _(a,b) b,
41 #undef _
42 };
43 
44 /*
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.
49  */
50 u8 *
51 vlib_node_serialize (vlib_main_t * vm, vlib_node_t *** node_dups, u8 * vector,
52  int include_nexts, int include_stats)
53 {
54  serialize_main_t _sm, *sm = &_sm;
55  vlib_node_t *n;
56  vlib_node_t **nodes;
57  u8 *namep;
58  u32 name_bytes;
59  uword i, j, k;
60  u64 l, v, c, d, pmc;
61  state_string_enum_t state_code;
62 
63  serialize_open_vector (sm, vector);
65 
66  for (j = 0; j < vec_len (node_dups); j++)
67  {
68  nodes = node_dups[j];
69 
70  serialize_likely_small_unsigned_integer (sm, vec_len (nodes));
71 
72  for (i = 0; i < vec_len (nodes); i++)
73  {
74  n = nodes[i];
75 
82 
83  state_code = STATE_INTERNAL;
84 
85  if (n->type == VLIB_NODE_TYPE_PROCESS)
86  {
88 
89  switch (p->flags
92  {
93  default:
94  if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
95  state_code = STATE_DONE;
96  break;
97 
99  state_code = STATE_TIME_WAIT;
100  break;
101 
103  state_code = STATE_EVENT_WAIT;
104  break;
105 
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  }
120 
121  /* See unserialize_cstring */
122  name_bytes = vec_len (n->name);
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);
130 
131  if (include_nexts)
132  {
134  (sm, vec_len (n->next_nodes));
135  for (k = 0; k < vec_len (n->next_nodes); k++)
137  n->next_nodes[k]);
138  }
139  else
141 
142  if (include_stats)
143  {
144  /* stats present */
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);
154  /* PMC counter */
155  serialize_integer (sm, pmc, 8);
156  }
157  else /* no stats */
159  }
160  }
161  return (serialize_close_vector (sm));
162 }
163 
164 vlib_node_t ***
166 {
167  serialize_main_t _sm, *sm = &_sm;
168  u32 nnodes, nnexts;
169  u32 nstat_vms;
170  vlib_node_t *node;
171  vlib_node_t **nodes;
172  vlib_node_t ***nodes_by_thread = 0;
173  int i, j, k;
174  u64 l, v, c, d, pmc;
175  state_string_enum_t state_code;
176  int stats_present;
177 
178  serialize_open_vector (sm, vector);
179 
181 
182  vec_validate (nodes_by_thread, nstat_vms - 1);
183  _vec_len (nodes_by_thread) = 0;
184 
185  for (i = 0; i < nstat_vms; i++)
186  {
188 
189  nodes = 0;
190  vec_validate (nodes, nnodes - 1);
191  vec_add1 (nodes_by_thread, nodes);
192 
193  for (j = 0; j < nnodes; j++)
194  {
195  node = 0;
196  vec_validate (node, 0);
197  nodes[j] = node;
198 
199  unserialize_cstring (sm, (char **) &(node->name));
201  node->state_string = (u8 *) state_strings[state_code];
202 
206  if (nnexts > 0)
207  vec_validate (node->next_nodes, nnexts - 1);
208  for (k = 0; k < nnexts; k++)
209  node->next_nodes[k] =
211 
212  stats_present = unserialize_likely_small_unsigned_integer (sm);
213 
214  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;
232  /* PMC counter */
233  unserialize_integer (sm, &pmc, 8);
234  node->stats_total.perf_counter_ticks = pmc;
235  }
236  }
237  }
238  return nodes_by_thread;
239 }
240 
241 #if TEST_CODE
242 
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)
247 {
248  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;
254  int i, j, k;
255  u32 max_threads = (u32) ~ 0;
256  int include_nexts = 0;
257  int include_stats = 0;
258 
260  {
261  if (unformat (input, "max-threads %d", &max_threads))
262  ;
263  else if (unformat (input, "stats"))
264  include_stats = 1;
265  else if (unformat (input, "nexts"))
266  include_nexts = 1;
267  else
268  break;
269  }
270 
271  /*
272  * 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  */
276  vec_validate (vector, 16383);
277  vec_reset_length (vector);
278 
279  vector = vlib_node_serialize (nm, vector, max_threads,
280  include_nexts, include_stats);
281 
282  vlib_cli_output (vm, "result vector %d bytes", vec_len (vector));
283 
284  nodes_by_thread = vlib_node_unserialize (vector);
285 
286  vec_free (vector);
287 
288  for (i = 0; i < vec_len (nodes_by_thread); i++)
289  {
290  nodes = nodes_by_thread[i];
291 
292  vlib_cli_output (vm, "thread %d", i);
293 
294  for (j = 0; j < vec_len (nodes); j++)
295  {
296  node = nodes[j];
297 
298  vlib_cli_output (vm, "[%d] %s state %s", j, node->name,
299  node->state_string);
300 
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);
307 
308  for (k = 0; k < vec_len (node->next_nodes); k++)
309  {
310  if (node->next_nodes[k] != ~0)
311  {
312  next_node = nodes[node->next_nodes[k]];
313  vlib_cli_output (vm, " [%d] %s", k, next_node->name);
314  }
315  }
316  }
317  }
318 
319  for (j = 0; j < vec_len (nodes_by_thread); j++)
320  {
321  nodes = nodes_by_thread[j];
322 
323  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);
330  }
331  vec_free (nodes_by_thread);
332 
333  return 0;
334 }
335 
336 /* *INDENT-OFF* */
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,
341 };
342 /* *INDENT-ON* */
343 #endif
344 
345 /*
346  * fd.io coding-style-patch-verification: ON
347  *
348  * Local Variables:
349  * eval: (c-set-style "gnu")
350  * End:
351  */
u32 * next_nodes
Definition: node.h:358
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
u8 * state_string
Definition: node.h:392
static u64 unserialize_likely_small_unsigned_integer(serialize_main_t *m)
Definition: serialize.h:254
unsigned long u64
Definition: types.h:89
u16 flags
Definition: node.h:313
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:525
int i
state_string_enum_t
#define foreach_state_string_code
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
u8 * vlib_node_serialize(vlib_main_t *vm, vlib_node_t ***node_dups, u8 *vector, int include_nexts, int include_stats)
u8 state
Definition: node.h:332
vlib_node_stats_t stats_last_clear
Definition: node.h:298
static void * serialize_get(serialize_main_t *m, uword n_bytes)
Definition: serialize.h:178
u64 perf_counter_ticks
Definition: node.h:261
#define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
Definition: node.h:590
unsigned int u32
Definition: types.h:88
#define VLIB_PROCESS_IS_RUNNING
Definition: node.h:596
vlib_node_stats_t stats_total
Definition: node.h:294
static void serialize_likely_small_unsigned_integer(serialize_main_t *m, u64 x)
Definition: serialize.h:218
struct _unformat_input_t unformat_input_t
u8 name[64]
Definition: memclnt.api:152
void unserialize_cstring(serialize_main_t *m, char **s)
Definition: serialize.c:178
u8 * name
Definition: node.h:288
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
svmdb_client_t * c
void serialize_open_vector(serialize_main_t *m, u8 *vector)
Definition: serialize.c:909
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static void unserialize_integer(serialize_main_t *m, void *x, u32 n_bytes)
Definition: serialize.h:201
static void serialize_integer(serialize_main_t *m, u64 x, u32 n_bytes)
Definition: serialize.h:185
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_node_main_t node_main
Definition: main.h:129
u64 uword
Definition: types.h:112
static char * state_strings[]
u16 flags
Definition: node.h:589
vlib_node_type_t type
Definition: node.h:301
static vlib_process_t * vlib_get_process_from_node(vlib_main_t *vm, vlib_node_t *node)
Definition: node_funcs.h:208
#define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT
Definition: node.h:591
vlib_node_t *** vlib_node_unserialize(u8 *vector)
void * serialize_close_vector(serialize_main_t *m)
Definition: serialize.c:919
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:762
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170