VOM: fix cflags
[vpp.git] / src / vlibapi / node_unserialize.c
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,
34   foreach_state_string_code
35 #undef _
36 } state_string_enum_t;
37
38 static char *state_strings[] = {
39 #define _(a,b) b,
40   foreach_state_string_code
41 #undef _
42 };
43
44 vlib_node_t ***
45 vlib_node_unserialize (u8 * vector)
46 {
47   serialize_main_t _sm, *sm = &_sm;
48   u32 nnodes, nnexts;
49   u32 nstat_vms;
50   vlib_node_t *node;
51   vlib_node_t **nodes;
52   vlib_node_t ***nodes_by_thread = 0;
53   int i, j, k;
54   u64 l, v, c, d;
55   state_string_enum_t state_code;
56   int stats_present;
57
58   serialize_open_vector (sm, vector);
59
60   nstat_vms = unserialize_likely_small_unsigned_integer (sm);
61
62   vec_validate (nodes_by_thread, nstat_vms - 1);
63   _vec_len (nodes_by_thread) = 0;
64
65   for (i = 0; i < nstat_vms; i++)
66     {
67       nnodes = unserialize_likely_small_unsigned_integer (sm);
68
69       nodes = 0;
70       vec_validate (nodes, nnodes - 1);
71       vec_add1 (nodes_by_thread, nodes);
72
73       for (j = 0; j < nnodes; j++)
74         {
75           node = 0;
76           vec_validate (node, 0);
77           nodes[j] = node;
78
79           unserialize_cstring (sm, (char **) &(node->name));
80           state_code = unserialize_likely_small_unsigned_integer (sm);
81           node->state_string = (u8 *) state_strings[state_code];
82
83           node->type = unserialize_likely_small_unsigned_integer (sm);
84           nnexts = unserialize_likely_small_unsigned_integer (sm);
85           if (nnexts > 0)
86             vec_validate (node->next_nodes, nnexts - 1);
87           for (k = 0; k < nnexts; k++)
88             node->next_nodes[k] =
89               unserialize_likely_small_unsigned_integer (sm);
90
91           stats_present = unserialize_likely_small_unsigned_integer (sm);
92
93           if (stats_present)
94             {
95               /* total clocks */
96               unserialize_integer (sm, &l, 8);
97               node->stats_total.clocks = l;
98               node->stats_last_clear.clocks = 0;
99
100               /* Total calls */
101               unserialize_integer (sm, &c, 8);
102               node->stats_total.calls = c;
103
104               /* Total vectors */
105               unserialize_integer (sm, &v, 8);
106               node->stats_total.vectors = v;
107
108               /* Total suspends */
109               unserialize_integer (sm, &d, 8);
110               node->stats_total.suspends = d;
111             }
112         }
113     }
114   return nodes_by_thread;
115 }
116
117 #if TEST_CODE
118
119 static clib_error_t *
120 test_node_serialize_command_fn (vlib_main_t * vm,
121                                 unformat_input_t * input,
122                                 vlib_cli_command_t * cmd)
123 {
124   vlib_node_main_t *nm = &vm->node_main;
125   u8 *vector = 0;
126   vlib_node_t ***nodes_by_thread;
127   vlib_node_t **nodes;
128   vlib_node_t *node;
129   vlib_node_t *next_node;
130   int i, j, k;
131   u32 max_threads = (u32) ~ 0;
132   int include_nexts = 0;
133   int include_stats = 0;
134
135   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
136     {
137       if (unformat (input, "max-threads %d", &max_threads))
138         ;
139       else if (unformat (input, "stats"))
140         include_stats = 1;
141       else if (unformat (input, "nexts"))
142         include_nexts = 1;
143       else
144         break;
145     }
146
147   /*
148    * Keep the number of memcpy ops to a minimum (e.g. 1).
149    * The current size of the serialized vector is
150    * slightly under 4K.
151    */
152   vec_validate (vector, 16383);
153   vec_reset_length (vector);
154
155   vector = vlib_node_serialize (nm, vector, max_threads,
156                                 include_nexts, include_stats);
157
158   vlib_cli_output (vm, "result vector %d bytes", vec_len (vector));
159
160   nodes_by_thread = vlib_node_unserialize (vector);
161
162   vec_free (vector);
163
164   for (i = 0; i < vec_len (nodes_by_thread); i++)
165     {
166       nodes = nodes_by_thread[i];
167
168       vlib_cli_output (vm, "thread %d", i);
169
170       for (j = 0; j < vec_len (nodes); j++)
171         {
172           node = nodes[j];
173
174           vlib_cli_output (vm, "[%d] %s state %s", j, node->name,
175                            node->state_string);
176
177           vlib_cli_output
178             (vm, "    clocks %lld calls %lld suspends"
179              " %lld vectors %lld",
180              node->stats_total.clocks,
181              node->stats_total.calls,
182              node->stats_total.suspends, node->stats_total.vectors);
183
184           for (k = 0; k < vec_len (node->next_nodes); k++)
185             {
186               if (node->next_nodes[k] != ~0)
187                 {
188                   next_node = nodes[node->next_nodes[k]];
189                   vlib_cli_output (vm, "  [%d] %s", k, next_node->name);
190                 }
191             }
192         }
193     }
194
195   for (j = 0; j < vec_len (nodes_by_thread); j++)
196     {
197       nodes = nodes_by_thread[j];
198
199       for (i = 0; i < vec_len (nodes); i++)
200         {
201           vec_free (nodes[i]->name);
202           vec_free (nodes[i]->next_nodes);
203           vec_free (nodes[i]);
204         }
205       vec_free (nodes);
206     }
207   vec_free (nodes_by_thread);
208
209   return 0;
210 }
211
212 /* *INDENT-OFF* */
213 VLIB_CLI_COMMAND (test_node_serialize_node, static) = {
214     .path = "test node serialize",
215     .short_help = "test node serialize [max-threads NN] nexts stats",
216     .function = test_node_serialize_command_fn,
217 };
218 /* *INDENT-ON* */
219 #endif
220
221 /*
222  * fd.io coding-style-patch-verification: ON
223  *
224  * Local Variables:
225  * eval: (c-set-style "gnu")
226  * End:
227  */