blob: 3735055d7897f7be7e739a0c86cf180bb1780abc [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Dave Barach9b8ffd92016-07-08 08:13:45 -040019int
20main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -070021{
22 return vlib_unix_main (argc, argv);
23}
24
25static clib_error_t *
26main_stub_init (vlib_main_t * vm)
27{
Dave Barachf8d50682019-05-14 18:01:44 -040028 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Dave Barach9b8ffd92016-07-08 08:13:45 -040030 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070031}
32
Dave Barachf8d50682019-05-14 18:01:44 -040033/* *INDENT-OFF* */
34VLIB_INIT_FUNCTION (main_stub_init) =
35{
36 .runs_after = VLIB_INITS("unix_physmem_init", "unix_cli_init"),
37};
38/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070039
40#if 0
41/* Node test code. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040042typedef struct
43{
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 int scalar;
45 int vector[0];
46} my_frame_t;
47
Dave Barach9b8ffd92016-07-08 08:13:45 -040048static u8 *
49format_my_node_frame (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070050{
Dave Barach9b8ffd92016-07-08 08:13:45 -040051 vlib_frame_t *f = va_arg (*va, vlib_frame_t *);
Damjan Mariona3d59862018-11-10 10:23:00 +010052 my_frame_t *g = vlib_frame_scalar_args (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -070053 int i;
54
55 s = format (s, "scalar %d, vector { ", g->scalar);
56 for (i = 0; i < f->n_vectors; i++)
57 s = format (s, "%d, ", g->vector[i]);
58 s = format (s, " }");
59
60 return s;
61}
62
63static uword
Dave Barach9b8ffd92016-07-08 08:13:45 -040064my_func (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
Dave Barach9b8ffd92016-07-08 08:13:45 -040066 vlib_node_t *node;
67 my_frame_t *y;
Ed Warnickecb9cada2015-12-08 15:45:58 -070068 u32 i, n_left = 0;
69 static int serial;
70 int verbose;
71
72 node = vlib_get_node (vm, rt->node_index);
73
74 verbose = 0;
75
76 if (verbose && f)
77 vlib_cli_output (vm, "%v: call frame %p %U", node->name,
78 f, format_my_node_frame, f);
79
80 if (rt->n_next_nodes > 0)
81 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040082 vlib_frame_t *next = vlib_get_next_frame (vm, rt, /* next index */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 n_left = VLIB_FRAME_SIZE - next->n_vectors;
Damjan Mariona3d59862018-11-10 10:23:00 +010084 y = vlib_frame_scalar_args (next);
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 y->scalar = serial++;
86 }
87 else
88 y = 0;
89
90 for (i = 0; i < 5; i++)
91 {
92 if (y)
93 {
94 ASSERT (n_left > 0);
95 n_left--;
96 y->vector[i] = y->scalar + i;
97 }
98 }
99 if (y)
100 vlib_put_next_frame (vm, rt, /* next index */ 0, n_left);
101
102 if (verbose)
103 vlib_cli_output (vm, "%v: return frame %p", node->name, f);
104
105 return i;
106}
107
Dave Barach9b8ffd92016-07-08 08:13:45 -0400108/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109VLIB_REGISTER_NODE (my_node1,static) = {
110 .function = my_func,
111 .type = VLIB_NODE_TYPE_INPUT,
112 .name = "my-node1",
113 .scalar_size = sizeof (my_frame_t),
114 .vector_size = STRUCT_SIZE_OF (my_frame_t, vector[0]),
115 .n_next_nodes = 1,
116 .next_nodes = {
117 [0] = "my-node2",
118 },
119};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400120/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
Dave Barach9b8ffd92016-07-08 08:13:45 -0400122/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123VLIB_REGISTER_NODE (my_node2,static) = {
124 .function = my_func,
125 .name = "my-node2",
126 .scalar_size = sizeof (my_frame_t),
127 .vector_size = STRUCT_SIZE_OF (my_frame_t, vector[0]),
128};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400129/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
131#endif
132
133#if 0
134
Dave Barach9b8ffd92016-07-08 08:13:45 -0400135typedef enum
136{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 MY_EVENT_TYPE1,
138 MY_EVENT_TYPE2,
139} my_process_completion_type_t;
140
Dave Barach9b8ffd92016-07-08 08:13:45 -0400141typedef struct
142{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 int a;
144 f64 b;
145} my_process_event_data_t;
146
Dave Barach9b8ffd92016-07-08 08:13:45 -0400147static u8 *
148format_my_process_event_data (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400150 my_process_event_data_t *d = va_arg (*va, my_process_event_data_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 return format (s, "{ a %d b %.6f}", d->a, d->b);
152}
153
154static uword
Dave Barach9b8ffd92016-07-08 08:13:45 -0400155my_proc (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400157 vlib_node_t *node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 u32 i;
159
160 node = vlib_get_node (vm, rt->node_index);
161
162 vlib_cli_output (vm, "%v: call frame %p", node->name, f);
163
164 for (i = 0; i < 5; i++)
165 {
166 vlib_cli_output (vm, "%v: %d", node->name, i);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400167 vlib_process_suspend (vm, 1e0 /* secs */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 }
169
170 vlib_cli_output (vm, "%v: return frame %p", node->name, f);
171
172 if (0)
173 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400174 uword n_events_seen, type, *data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
176 for (n_events_seen = 0; n_events_seen < 2;)
177 {
178 vlib_process_wait_for_event (vm);
179 type = vlib_process_get_events (vm, &data);
180 n_events_seen += vec_len (data);
181 vlib_cli_output (vm, "%U %v: completion #%d type %d data 0x%wx",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400182 format_time_interval, "h:m:s:u",
183 vlib_time_now (vm), node->name, i, type, data[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 _vec_len (data) = 0;
185 }
186
187 vec_free (data);
188 }
189 else
190 {
191 uword n_events_seen, i, type;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400192 my_process_event_data_t *data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 for (n_events_seen = 0; n_events_seen < 2;)
194 {
195 vlib_process_wait_for_event (vm);
196 data = vlib_process_get_event_data (vm, &type);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400197 vec_foreach_index (i, data)
198 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199 vlib_cli_output (vm, "%U event type %d data %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400200 format_time_interval, "h:m:s:u",
201 vlib_time_now (vm), type,
202 format_my_process_event_data, data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203 }
204 n_events_seen += vec_len (data);
205 vlib_process_put_event_data (vm, data);
206 }
207 }
208
209 return i;
210}
211
Dave Barach9b8ffd92016-07-08 08:13:45 -0400212/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213VLIB_REGISTER_NODE (my_proc_node,static) = {
214 .function = my_proc,
215 .type = VLIB_NODE_TYPE_PROCESS,
216 .name = "my-proc",
217};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400218/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
220static uword
Dave Barach9b8ffd92016-07-08 08:13:45 -0400221my_proc_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222{
223 static int i;
224
225 if (i++ < 2)
226 {
227 if (0)
228 vlib_process_signal_event (vm, my_proc_node.index,
229 i == 1 ? MY_EVENT_TYPE1 : MY_EVENT_TYPE2,
230 0x12340000 + i);
231 else
232 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400233 my_process_event_data_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 f64 dt = 5;
235 d = vlib_process_signal_event_at_time (vm,
236 i * dt,
237 my_proc_node.index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400238 i ==
239 1 ? MY_EVENT_TYPE1 :
240 MY_EVENT_TYPE2,
241 1 /* elts */ ,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 sizeof (d[0]));
243 d->a = i;
244 d->b = vlib_time_now (vm);
245 }
246 }
247 else
248 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
249
Dave Barach9b8ffd92016-07-08 08:13:45 -0400250 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251}
252
Dave Barach9b8ffd92016-07-08 08:13:45 -0400253/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254VLIB_REGISTER_NODE (my_proc_input_node,static) = {
255 .function = my_proc_input,
256 .type = VLIB_NODE_TYPE_INPUT,
257 .name = "my-proc-input",
258};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400259/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260
Dave Barach9b8ffd92016-07-08 08:13:45 -0400261static uword
262_unformat_farith (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263{
264 u32 prec = va_arg (*args, u32);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400265 f64 *result = va_arg (*args, f64 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266 f64 tmp[2];
267
268 /* Binary operations in from lowest to highest precedence. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400269 char *binops[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270 "+%U", "-%U", "/%U", "*%U", "^%U",
271 };
272
273 if (prec <= ARRAY_LEN (binops) - 1
274 && unformat_user (i, _unformat_farith, prec + 1, &tmp[0]))
275 {
276 int p;
277 for (p = prec; p < ARRAY_LEN (binops); p++)
278 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400279 if (unformat (i, binops[p], _unformat_farith, prec + 0, &tmp[1]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280 {
281 switch (binops[p][0])
282 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400283 case '+':
284 result[0] = tmp[0] + tmp[1];
285 break;
286 case '-':
287 result[0] = tmp[0] - tmp[1];
288 break;
289 case '/':
290 result[0] = tmp[0] / tmp[1];
291 break;
292 case '*':
293 result[0] = tmp[0] * tmp[1];
294 break;
295 case '^':
296 result[0] = pow (tmp[0], tmp[1]);
297 break;
298 default:
299 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300 }
301 return 1;
302 }
303 }
304 result[0] = tmp[0];
305 return 1;
306 }
307
Dave Barach9b8ffd92016-07-08 08:13:45 -0400308 else if (unformat (i, "-%U", _unformat_farith, prec + 0, &tmp[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 {
310 result[0] = -tmp[0];
311 return 1;
312 }
313
Dave Barach9b8ffd92016-07-08 08:13:45 -0400314 else if (unformat (i, "(%U)", _unformat_farith, 0, &tmp[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 {
316 result[0] = tmp[0];
317 return 1;
318 }
319
320 else if (unformat (i, "%f", result))
321 return 1;
322
323 else
324 return 0;
325}
326
Dave Barach9b8ffd92016-07-08 08:13:45 -0400327static uword
328unformat_farith (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329{
330 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400331 f64 *result = va_arg (*args, f64 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 return unformat_user (i, _unformat_farith, 0, result);
333}
334
Dave Barach9b8ffd92016-07-08 08:13:45 -0400335static uword
336unformat_integer (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337{
338 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400339 u32 *data = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 return unformat (i, "%d", data);
341}
342
Dave Barach9b8ffd92016-07-08 08:13:45 -0400343static VLIB_CLI_PARSE_RULE (my_parse_rule1) =
344{
345.name = "decimal_integer",.short_help =
346 "a decimal integer",.unformat_function = unformat_integer,.data_size =
347 sizeof (u32),};
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
Dave Barach9b8ffd92016-07-08 08:13:45 -0400349static VLIB_CLI_PARSE_RULE (my_parse_rule2) =
350{
351.name = "float_expression",.short_help =
352 "floating point expression",.unformat_function =
353 unformat_farith,.data_size = sizeof (f64),};
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
355static clib_error_t *
356bar_command (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400357 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358{
359 switch (cmd->function_arg)
360 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400361 case 2:
362 {
363 u32 *d, *e;
364 d = vlib_cli_get_parse_rule_result (vm, 0);
365 e = vlib_cli_get_parse_rule_result (vm, 1);
366 vlib_cli_output (vm, "bar2 %d %d", d[0], e[0]);
367 break;
368 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
Dave Barach9b8ffd92016-07-08 08:13:45 -0400370 case 1:
371 {
372 u32 *d = vlib_cli_get_parse_rule_result (vm, 0);
373 vlib_cli_output (vm, "bar1 %d", d[0]);
374 break;
375 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376
Dave Barach9b8ffd92016-07-08 08:13:45 -0400377 case 3:
378 {
379 f64 *d = vlib_cli_get_parse_rule_result (vm, 0);
380 vlib_cli_output (vm, "expr %.6f", d[0]);
381 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382 }
383
384 return 0;
385}
386
Dave Barach9b8ffd92016-07-08 08:13:45 -0400387/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388VLIB_CLI_COMMAND (bar_command2, static) = {
389 .path = "bar %decimal_integer",
390 .short_help = "bar1 command",
391 .function = bar_command,
392 .function_arg = 1,
393};
394VLIB_CLI_COMMAND (bar_command1, static) = {
395 .path = "bar %decimal_integer %decimal_integer",
396 .short_help = "bar2 command",
397 .function = bar_command,
398 .function_arg = 2,
399};
400VLIB_CLI_COMMAND (bar_command3, static) = {
401 .path = "zap %float_expression",
402 .short_help = "bar3 command",
403 .function = bar_command,
404 .function_arg = 3,
405};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400406/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407
408#endif
409
Dave Barach9b8ffd92016-07-08 08:13:45 -0400410/*
411 * fd.io coding-style-patch-verification: ON
412 *
413 * Local Variables:
414 * eval: (c-set-style "gnu")
415 * End:
416 */