FD.io VPP  v17.01-9-ge7dcee4
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 extern void vl_msg_api_barrier_sync (void);
20 extern void vl_msg_api_barrier_release (void);
21 
22 /* serialized representation of state strings */
23 
24 #define foreach_state_string_code \
25 _(STATE_DONE, "done") \
26 _(STATE_DISABLED, "disabled") \
27 _(STATE_TIME_WAIT, "time wait") \
28 _(STATE_EVENT_WAIT, "event wait") \
29 _(STATE_ANY_WAIT, "any wait") \
30 _(STATE_POLLING, "polling") \
31 _(STATE_INTERRUPT_WAIT, "interrupt wait") \
32 _(STATE_INTERNAL, "internal")
33 
34 typedef enum
35 {
36 #define _(a,b) a,
38 #undef _
40 
41 static char *state_strings[] = {
42 #define _(a,b) b,
44 #undef _
45 };
46 
47 /*
48  * Serialize a vlib_node_main_t. Appends the result to vector.
49  * Pass 0 to create a new vector, use vec_reset_length(vector)
50  * to recycle a vector / avoid memory allocation, etc.
51  * Switch heaps before/after to serialize into API client shared memory.
52  */
53 
54 u8 *
56  u32 max_threads, int include_nexts, int include_stats)
57 {
58  serialize_main_t _sm, *sm = &_sm;
59  vlib_main_t *vm = vlib_get_main ();
60  vlib_node_t *n;
61  static vlib_node_t ***node_dups;
62  vlib_node_t **nodes;
63  static vlib_main_t **stat_vms;
64  vlib_main_t *stat_vm;
65  u8 *namep;
66  u32 name_bytes;
67  uword i, j, k;
68  u64 l, v, c, d;
69  state_string_enum_t state_code;
70  u32 threads_to_serialize;
71 
72  vec_reset_length (node_dups);
73 
74  if (vec_len (stat_vms) == 0)
75  {
76  if (vec_len (vlib_mains) == 0)
77  vec_add1 (stat_vms, vm);
78  else
79  {
80  for (i = 0; i < vec_len (vlib_mains); i++)
81  {
82  stat_vm = vlib_mains[i];
83  if (stat_vm)
84  vec_add1 (stat_vms, stat_vm);
85  }
86  }
87  }
88 
89  threads_to_serialize = clib_min (max_threads, vec_len (stat_vms));
90 
91  /*
92  * Barrier sync across stats scraping.
93  * Otherwise, the counts will be grossly inaccurate.
94  */
96 
97  for (j = 0; j < threads_to_serialize; j++)
98  {
99  stat_vm = stat_vms[j];
100  nm = &stat_vm->node_main;
101 
102  if (include_stats)
103  {
104  for (i = 0; i < vec_len (nm->nodes); i++)
105  {
106  n = nm->nodes[i];
107  vlib_node_sync_stats (stat_vm, n);
108  }
109  }
110 
111  nodes = vec_dup (nm->nodes);
112 
113  vec_add1 (node_dups, nodes);
114  }
116 
117  serialize_open_vector (sm, vector);
118 
120 
121  for (j = 0; j < vec_len (stat_vms); j++)
122  {
123  stat_vm = stat_vms[j];
124  nodes = node_dups[j];
125 
126  serialize_likely_small_unsigned_integer (sm, vec_len (nodes));
127 
128  for (i = 0; i < vec_len (nodes); i++)
129  {
130  n = nodes[i];
131 
136 
137  state_code = STATE_INTERNAL;
138 
139  if (n->type == VLIB_NODE_TYPE_PROCESS)
140  {
142 
143  switch (p->flags
146  {
147  default:
148  if (!(p->flags & VLIB_PROCESS_IS_RUNNING))
149  state_code = STATE_DONE;
150  break;
151 
153  state_code = STATE_TIME_WAIT;
154  break;
155 
157  state_code = STATE_EVENT_WAIT;
158  break;
159 
161  state_code =
162  STATE_ANY_WAIT;
163  break;
164  }
165  }
166  else if (n->type != VLIB_NODE_TYPE_INTERNAL)
167  {
168  state_code = STATE_POLLING;
169  if (n->state == VLIB_NODE_STATE_DISABLED)
170  state_code = STATE_DISABLED;
171  else if (n->state == VLIB_NODE_STATE_INTERRUPT)
172  state_code = STATE_INTERRUPT_WAIT;
173  }
174 
175  /* See unserialize_cstring */
176  name_bytes = vec_len (n->name);
178  namep = serialize_get (sm, name_bytes);
179  memcpy (namep, n->name, name_bytes);
180 
181  serialize_likely_small_unsigned_integer (sm, (u64) state_code);
183 
184  if (include_nexts)
185  {
187  (sm, vec_len (n->next_nodes));
188  for (k = 0; k < vec_len (n->next_nodes); k++)
190  n->next_nodes[k]);
191  }
192  else
194 
195  if (include_stats)
196  {
197  /* stats present */
199  /* total clocks */
200  serialize_integer (sm, l, 8);
201  /* Total calls */
202  serialize_integer (sm, c, 8);
203  /* Total vectors */
204  serialize_integer (sm, v, 8);
205  /* Total suspends */
206  serialize_integer (sm, d, 8);
207  }
208  else /* no stats */
210  }
211  vec_free (nodes);
212  }
213  return (serialize_close_vector (sm));
214 }
215 
216 vlib_node_t ***
218 {
219  serialize_main_t _sm, *sm = &_sm;
220  u32 nnodes, nnexts;
221  u32 nstat_vms;
222  vlib_node_t *node;
223  vlib_node_t **nodes;
224  vlib_node_t ***nodes_by_thread = 0;
225  int i, j, k;
226  u64 l, v, c, d;
227  state_string_enum_t state_code;
228  int stats_present;
229 
230  serialize_open_vector (sm, vector);
231 
233 
234  vec_validate (nodes_by_thread, nstat_vms - 1);
235  _vec_len (nodes_by_thread) = 0;
236 
237  for (i = 0; i < nstat_vms; i++)
238  {
240 
241  nodes = 0;
242  vec_validate (nodes, nnodes - 1);
243  vec_add1 (nodes_by_thread, nodes);
244 
245  for (j = 0; j < nnodes; j++)
246  {
247  node = 0;
248  vec_validate (node, 0);
249  nodes[j] = node;
250 
251  unserialize_cstring (sm, (char **) &(node->name));
253  node->state_string = (u8 *) state_strings[state_code];
254 
257  if (nnexts > 0)
258  vec_validate (node->next_nodes, nnexts - 1);
259  for (k = 0; k < nnexts; k++)
260  node->next_nodes[k] =
262 
263  stats_present = unserialize_likely_small_unsigned_integer (sm);
264 
265  if (stats_present)
266  {
267  /* total clocks */
268  unserialize_integer (sm, &l, 8);
269  node->stats_total.clocks = l;
270  node->stats_last_clear.clocks = 0;
271 
272  /* Total calls */
273  unserialize_integer (sm, &c, 8);
274  node->stats_total.calls = c;
275 
276  /* Total vectors */
277  unserialize_integer (sm, &v, 8);
278  node->stats_total.vectors = v;
279 
280  /* Total suspends */
281  unserialize_integer (sm, &d, 8);
282  node->stats_total.suspends = d;
283  }
284  }
285  }
286  return nodes_by_thread;
287 }
288 
289 #if CLIB_DEBUG > 0
290 
291 static clib_error_t *
293  unformat_input_t * input,
294  vlib_cli_command_t * cmd)
295 {
296  vlib_node_main_t *nm = &vm->node_main;
297  u8 *vector = 0;
298  vlib_node_t ***nodes_by_thread;
299  vlib_node_t **nodes;
300  vlib_node_t *node;
301  vlib_node_t *next_node;
302  int i, j, k;
303  u32 max_threads = (u32) ~ 0;
304  int include_nexts = 0;
305  int include_stats = 0;
306 
308  {
309  if (unformat (input, "max-threads %d", &max_threads))
310  ;
311  else if (unformat (input, "stats"))
312  include_stats = 1;
313  else if (unformat (input, "nexts"))
314  include_nexts = 1;
315  else
316  break;
317  }
318 
319  /*
320  * Keep the number of memcpy ops to a minimum (e.g. 1).
321  * The current size of the serialized vector is
322  * slightly under 4K.
323  */
324  vec_validate (vector, 16383);
325  vec_reset_length (vector);
326 
327  vector = vlib_node_serialize (nm, vector, max_threads,
328  include_nexts, include_stats);
329 
330  vlib_cli_output (vm, "result vector %d bytes", vec_len (vector));
331 
332  nodes_by_thread = vlib_node_unserialize (vector);
333 
334  vec_free (vector);
335 
336  for (i = 0; i < vec_len (nodes_by_thread); i++)
337  {
338  nodes = nodes_by_thread[i];
339 
340  vlib_cli_output (vm, "thread %d", i);
341 
342  for (j = 0; j < vec_len (nodes); j++)
343  {
344  node = nodes[j];
345 
346  vlib_cli_output (vm, "[%d] %s state %s", j, node->name,
347  node->state_string);
348 
350  (vm, " clocks %lld calls %lld suspends"
351  " %lld vectors %lld",
352  node->stats_total.clocks,
353  node->stats_total.calls,
354  node->stats_total.suspends, node->stats_total.vectors);
355 
356  for (k = 0; k < vec_len (node->next_nodes); k++)
357  {
358  if (node->next_nodes[k] != ~0)
359  {
360  next_node = nodes[node->next_nodes[k]];
361  vlib_cli_output (vm, " [%d] %s", k, next_node->name);
362  }
363  }
364  }
365  }
366 
367  for (j = 0; j < vec_len (nodes_by_thread); j++)
368  {
369  nodes = nodes_by_thread[j];
370 
371  for (i = 0; i < vec_len (nodes); i++)
372  {
373  vec_free (nodes[i]->name);
374  vec_free (nodes[i]->next_nodes);
375  vec_free (nodes[i]);
376  }
377  vec_free (nodes);
378  }
379  vec_free (nodes_by_thread);
380 
381  return 0;
382 }
383 
384 /* *INDENT-OFF* */
385 VLIB_CLI_COMMAND (test_node_serialize_node, static) = {
386  .path = "test node serialize",
387  .short_help = "test node serialize [max-threads NN] nexts stats",
388  .function = test_node_serialize_command_fn,
389 };
390 /* *INDENT-ON* */
391 #endif
392 
393 /*
394  * fd.io coding-style-patch-verification: ON
395  *
396  * Local Variables:
397  * eval: (c-set-style "gnu")
398  * End:
399  */
u32 * next_nodes
Definition: node.h:288
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
u8 * state_string
Definition: node.h:322
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
#define clib_min(x, y)
Definition: clib.h:326
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
static u64 unserialize_likely_small_unsigned_integer(serialize_main_t *m)
Definition: serialize.h:254
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
state_string_enum_t
#define foreach_state_string_code
u8 * vlib_node_serialize(vlib_node_main_t *nm, u8 *vector, u32 max_threads, int include_nexts, int include_stats)
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
u8 state
Definition: node.h:265
vlib_node_stats_t stats_last_clear
Definition: node.h:231
vlib_node_t ** nodes
Definition: node.h:630
static void * serialize_get(serialize_main_t *m, uword n_bytes)
Definition: serialize.h:178
unsigned long u64
Definition: types.h:89
vlib_node_stats_t stats_total
Definition: node.h:227
static void serialize_likely_small_unsigned_integer(serialize_main_t *m, u64 x)
Definition: serialize.h:218
#define v
Definition: acl.c:314
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:334
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:576
void unserialize_cstring(serialize_main_t *m, char **s)
Definition: serialize.c:178
u8 * name
Definition: node.h:221
svmdb_client_t * c
void serialize_open_vector(serialize_main_t *m, u8 *vector)
Definition: serialize.c:908
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
void vl_msg_api_barrier_sync(void)
Definition: api_shared.c:43
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
static clib_error_t * test_node_serialize_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT
Definition: node.h:506
unsigned int u32
Definition: types.h:88
u64 uword
Definition: types.h:112
void vl_msg_api_barrier_release(void)
Definition: api_shared.c:49
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
vlib_node_main_t node_main
Definition: main.h:115
vlib_main_t ** vlib_mains
static char * state_strings[]
#define VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK
Definition: node.h:505
void vlib_node_sync_stats(vlib_main_t *vm, vlib_node_t *n)
Definition: main.c:575
u16 flags
Definition: node.h:504
vlib_node_type_t type
Definition: node.h:234
static vlib_process_t * vlib_get_process_from_node(vlib_main_t *vm, vlib_node_t *node)
Definition: node_funcs.h:190
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
vlib_node_t *** vlib_node_unserialize(u8 *vector)
void * serialize_close_vector(serialize_main_t *m)
Definition: serialize.c:918
struct _unformat_input_t unformat_input_t
#define VLIB_PROCESS_IS_RUNNING
Definition: node.h:511