3b19c53f720271cb3f59515713bf046557eb6a99
[vpp.git] / src / examples / vlib / main_stub.c
1 /*
2  * Copyright (c) 2015 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 #include <vlib/unix/unix.h>
17 #include <math.h>
18
19 int
20 main (int argc, char *argv[])
21 {
22   return vlib_unix_main (argc, argv);
23 }
24
25 static clib_error_t *
26 main_stub_init (vlib_main_t * vm)
27 {
28   clib_error_t *error;
29
30   if ((error = unix_physmem_init (vm)))
31     return error;
32
33   if ((error = vlib_call_init_function (vm, unix_cli_init)))
34     return error;
35
36   return error;
37 }
38
39 VLIB_INIT_FUNCTION (main_stub_init);
40
41 #if 0
42 /* Node test code. */
43 typedef struct
44 {
45   int scalar;
46   int vector[0];
47 } my_frame_t;
48
49 static u8 *
50 format_my_node_frame (u8 * s, va_list * va)
51 {
52   vlib_frame_t *f = va_arg (*va, vlib_frame_t *);
53   my_frame_t *g = vlib_frame_args (f);
54   int i;
55
56   s = format (s, "scalar %d, vector { ", g->scalar);
57   for (i = 0; i < f->n_vectors; i++)
58     s = format (s, "%d, ", g->vector[i]);
59   s = format (s, " }");
60
61   return s;
62 }
63
64 static uword
65 my_func (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
66 {
67   vlib_node_t *node;
68   my_frame_t *y;
69   u32 i, n_left = 0;
70   static int serial;
71   int verbose;
72
73   node = vlib_get_node (vm, rt->node_index);
74
75   verbose = 0;
76
77   if (verbose && f)
78     vlib_cli_output (vm, "%v: call frame %p %U", node->name,
79                      f, format_my_node_frame, f);
80
81   if (rt->n_next_nodes > 0)
82     {
83       vlib_frame_t *next = vlib_get_next_frame (vm, rt, /* next index */ 0);
84       n_left = VLIB_FRAME_SIZE - next->n_vectors;
85       y = vlib_frame_args (next);
86       y->scalar = serial++;
87     }
88   else
89     y = 0;
90
91   for (i = 0; i < 5; i++)
92     {
93       if (y)
94         {
95           ASSERT (n_left > 0);
96           n_left--;
97           y->vector[i] = y->scalar + i;
98         }
99     }
100   if (y)
101     vlib_put_next_frame (vm, rt, /* next index */ 0, n_left);
102
103   if (verbose)
104     vlib_cli_output (vm, "%v: return frame %p", node->name, f);
105
106   return i;
107 }
108
109 /* *INDENT-OFF* */
110 VLIB_REGISTER_NODE (my_node1,static) = {
111   .function = my_func,
112   .type = VLIB_NODE_TYPE_INPUT,
113   .name = "my-node1",
114   .scalar_size = sizeof (my_frame_t),
115   .vector_size = STRUCT_SIZE_OF (my_frame_t, vector[0]),
116   .n_next_nodes = 1,
117   .next_nodes = {
118     [0] = "my-node2",
119   },
120 };
121 /* *INDENT-ON* */
122
123 /* *INDENT-OFF* */
124 VLIB_REGISTER_NODE (my_node2,static) = {
125   .function = my_func,
126   .name = "my-node2",
127   .scalar_size = sizeof (my_frame_t),
128   .vector_size = STRUCT_SIZE_OF (my_frame_t, vector[0]),
129 };
130 /* *INDENT-ON* */
131
132 #endif
133
134 #if 0
135
136 typedef enum
137 {
138   MY_EVENT_TYPE1,
139   MY_EVENT_TYPE2,
140 } my_process_completion_type_t;
141
142 typedef struct
143 {
144   int a;
145   f64 b;
146 } my_process_event_data_t;
147
148 static u8 *
149 format_my_process_event_data (u8 * s, va_list * va)
150 {
151   my_process_event_data_t *d = va_arg (*va, my_process_event_data_t *);
152   return format (s, "{ a %d b %.6f}", d->a, d->b);
153 }
154
155 static uword
156 my_proc (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
157 {
158   vlib_node_t *node;
159   u32 i;
160
161   node = vlib_get_node (vm, rt->node_index);
162
163   vlib_cli_output (vm, "%v: call frame %p", node->name, f);
164
165   for (i = 0; i < 5; i++)
166     {
167       vlib_cli_output (vm, "%v: %d", node->name, i);
168       vlib_process_suspend (vm, 1e0 /* secs */ );
169     }
170
171   vlib_cli_output (vm, "%v: return frame %p", node->name, f);
172
173   if (0)
174     {
175       uword n_events_seen, type, *data = 0;
176
177       for (n_events_seen = 0; n_events_seen < 2;)
178         {
179           vlib_process_wait_for_event (vm);
180           type = vlib_process_get_events (vm, &data);
181           n_events_seen += vec_len (data);
182           vlib_cli_output (vm, "%U %v: completion #%d type %d data 0x%wx",
183                            format_time_interval, "h:m:s:u",
184                            vlib_time_now (vm), node->name, i, type, data[0]);
185           _vec_len (data) = 0;
186         }
187
188       vec_free (data);
189     }
190   else
191     {
192       uword n_events_seen, i, type;
193       my_process_event_data_t *data;
194       for (n_events_seen = 0; n_events_seen < 2;)
195         {
196           vlib_process_wait_for_event (vm);
197           data = vlib_process_get_event_data (vm, &type);
198           vec_foreach_index (i, data)
199           {
200             vlib_cli_output (vm, "%U event type %d data %U",
201                              format_time_interval, "h:m:s:u",
202                              vlib_time_now (vm), type,
203                              format_my_process_event_data, data);
204           }
205           n_events_seen += vec_len (data);
206           vlib_process_put_event_data (vm, data);
207         }
208     }
209
210   return i;
211 }
212
213 /* *INDENT-OFF* */
214 VLIB_REGISTER_NODE (my_proc_node,static) = {
215   .function = my_proc,
216   .type = VLIB_NODE_TYPE_PROCESS,
217   .name = "my-proc",
218 };
219 /* *INDENT-ON* */
220
221 static uword
222 my_proc_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
223 {
224   static int i;
225
226   if (i++ < 2)
227     {
228       if (0)
229         vlib_process_signal_event (vm, my_proc_node.index,
230                                    i == 1 ? MY_EVENT_TYPE1 : MY_EVENT_TYPE2,
231                                    0x12340000 + i);
232       else
233         {
234           my_process_event_data_t *d;
235           f64 dt = 5;
236           d = vlib_process_signal_event_at_time (vm,
237                                                  i * dt,
238                                                  my_proc_node.index,
239                                                  i ==
240                                                  1 ? MY_EVENT_TYPE1 :
241                                                  MY_EVENT_TYPE2,
242                                                  1 /* elts */ ,
243                                                  sizeof (d[0]));
244           d->a = i;
245           d->b = vlib_time_now (vm);
246         }
247     }
248   else
249     vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
250
251   return 0;
252 }
253
254 /* *INDENT-OFF* */
255 VLIB_REGISTER_NODE (my_proc_input_node,static) = {
256   .function = my_proc_input,
257   .type = VLIB_NODE_TYPE_INPUT,
258   .name = "my-proc-input",
259 };
260 /* *INDENT-ON* */
261
262 static uword
263 _unformat_farith (unformat_input_t * i, va_list * args)
264 {
265   u32 prec = va_arg (*args, u32);
266   f64 *result = va_arg (*args, f64 *);
267   f64 tmp[2];
268
269   /* Binary operations in from lowest to highest precedence. */
270   char *binops[] = {
271     "+%U", "-%U", "/%U", "*%U", "^%U",
272   };
273
274   if (prec <= ARRAY_LEN (binops) - 1
275       && unformat_user (i, _unformat_farith, prec + 1, &tmp[0]))
276     {
277       int p;
278       for (p = prec; p < ARRAY_LEN (binops); p++)
279         {
280           if (unformat (i, binops[p], _unformat_farith, prec + 0, &tmp[1]))
281             {
282               switch (binops[p][0])
283                 {
284                 case '+':
285                   result[0] = tmp[0] + tmp[1];
286                   break;
287                 case '-':
288                   result[0] = tmp[0] - tmp[1];
289                   break;
290                 case '/':
291                   result[0] = tmp[0] / tmp[1];
292                   break;
293                 case '*':
294                   result[0] = tmp[0] * tmp[1];
295                   break;
296                 case '^':
297                   result[0] = pow (tmp[0], tmp[1]);
298                   break;
299                 default:
300                   abort ();
301                 }
302               return 1;
303             }
304         }
305       result[0] = tmp[0];
306       return 1;
307     }
308
309   else if (unformat (i, "-%U", _unformat_farith, prec + 0, &tmp[0]))
310     {
311       result[0] = -tmp[0];
312       return 1;
313     }
314
315   else if (unformat (i, "(%U)", _unformat_farith, 0, &tmp[0]))
316     {
317       result[0] = tmp[0];
318       return 1;
319     }
320
321   else if (unformat (i, "%f", result))
322     return 1;
323
324   else
325     return 0;
326 }
327
328 static uword
329 unformat_farith (unformat_input_t * i, va_list * args)
330 {
331   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
332   f64 *result = va_arg (*args, f64 *);
333   return unformat_user (i, _unformat_farith, 0, result);
334 }
335
336 static uword
337 unformat_integer (unformat_input_t * i, va_list * args)
338 {
339   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
340   u32 *data = va_arg (*args, u32 *);
341   return unformat (i, "%d", data);
342 }
343
344 static VLIB_CLI_PARSE_RULE (my_parse_rule1) =
345 {
346 .name = "decimal_integer",.short_help =
347     "a decimal integer",.unformat_function = unformat_integer,.data_size =
348     sizeof (u32),};
349
350 static VLIB_CLI_PARSE_RULE (my_parse_rule2) =
351 {
352 .name = "float_expression",.short_help =
353     "floating point expression",.unformat_function =
354     unformat_farith,.data_size = sizeof (f64),};
355
356 static clib_error_t *
357 bar_command (vlib_main_t * vm,
358              unformat_input_t * input, vlib_cli_command_t * cmd)
359 {
360   switch (cmd->function_arg)
361     {
362     case 2:
363       {
364         u32 *d, *e;
365         d = vlib_cli_get_parse_rule_result (vm, 0);
366         e = vlib_cli_get_parse_rule_result (vm, 1);
367         vlib_cli_output (vm, "bar2 %d %d", d[0], e[0]);
368         break;
369       }
370
371     case 1:
372       {
373         u32 *d = vlib_cli_get_parse_rule_result (vm, 0);
374         vlib_cli_output (vm, "bar1 %d", d[0]);
375         break;
376       }
377
378     case 3:
379       {
380         f64 *d = vlib_cli_get_parse_rule_result (vm, 0);
381         vlib_cli_output (vm, "expr %.6f", d[0]);
382       }
383     }
384
385   return 0;
386 }
387
388 /* *INDENT-OFF* */
389 VLIB_CLI_COMMAND (bar_command2, static) = {
390   .path = "bar %decimal_integer",
391   .short_help = "bar1 command",
392   .function = bar_command,
393   .function_arg = 1,
394 };
395 VLIB_CLI_COMMAND (bar_command1, static) = {
396   .path = "bar %decimal_integer %decimal_integer",
397   .short_help = "bar2 command",
398   .function = bar_command,
399   .function_arg = 2,
400 };
401 VLIB_CLI_COMMAND (bar_command3, static) = {
402   .path = "zap %float_expression",
403   .short_help = "bar3 command",
404   .function = bar_command,
405   .function_arg = 3,
406 };
407 /* *INDENT-ON* */
408
409 #endif
410
411 /*
412  * fd.io coding-style-patch-verification: ON
413  *
414  * Local Variables:
415  * eval: (c-set-style "gnu")
416  * End:
417  */