blob: be16a2b98bc6e532622fb285c4e0869c5b88eddf [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 -040033VLIB_INIT_FUNCTION (main_stub_init) =
34{
35 .runs_after = VLIB_INITS("unix_physmem_init", "unix_cli_init"),
36};
Ed Warnickecb9cada2015-12-08 15:45:58 -070037
38#if 0
39/* Node test code. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040040typedef struct
41{
Ed Warnickecb9cada2015-12-08 15:45:58 -070042 int scalar;
43 int vector[0];
44} my_frame_t;
45
Dave Barach9b8ffd92016-07-08 08:13:45 -040046static u8 *
47format_my_node_frame (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070048{
Dave Barach9b8ffd92016-07-08 08:13:45 -040049 vlib_frame_t *f = va_arg (*va, vlib_frame_t *);
Damjan Mariona3d59862018-11-10 10:23:00 +010050 my_frame_t *g = vlib_frame_scalar_args (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 int i;
52
53 s = format (s, "scalar %d, vector { ", g->scalar);
54 for (i = 0; i < f->n_vectors; i++)
55 s = format (s, "%d, ", g->vector[i]);
56 s = format (s, " }");
57
58 return s;
59}
60
61static uword
Dave Barach9b8ffd92016-07-08 08:13:45 -040062my_func (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -070063{
Dave Barach9b8ffd92016-07-08 08:13:45 -040064 vlib_node_t *node;
65 my_frame_t *y;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 u32 i, n_left = 0;
67 static int serial;
68 int verbose;
69
70 node = vlib_get_node (vm, rt->node_index);
71
72 verbose = 0;
73
74 if (verbose && f)
75 vlib_cli_output (vm, "%v: call frame %p %U", node->name,
76 f, format_my_node_frame, f);
77
78 if (rt->n_next_nodes > 0)
79 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040080 vlib_frame_t *next = vlib_get_next_frame (vm, rt, /* next index */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 n_left = VLIB_FRAME_SIZE - next->n_vectors;
Damjan Mariona3d59862018-11-10 10:23:00 +010082 y = vlib_frame_scalar_args (next);
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 y->scalar = serial++;
84 }
85 else
86 y = 0;
87
88 for (i = 0; i < 5; i++)
89 {
90 if (y)
91 {
92 ASSERT (n_left > 0);
93 n_left--;
94 y->vector[i] = y->scalar + i;
95 }
96 }
97 if (y)
98 vlib_put_next_frame (vm, rt, /* next index */ 0, n_left);
99
100 if (verbose)
101 vlib_cli_output (vm, "%v: return frame %p", node->name, f);
102
103 return i;
104}
105
106VLIB_REGISTER_NODE (my_node1,static) = {
107 .function = my_func,
108 .type = VLIB_NODE_TYPE_INPUT,
109 .name = "my-node1",
110 .scalar_size = sizeof (my_frame_t),
111 .vector_size = STRUCT_SIZE_OF (my_frame_t, vector[0]),
112 .n_next_nodes = 1,
113 .next_nodes = {
114 [0] = "my-node2",
115 },
116};
117
118VLIB_REGISTER_NODE (my_node2,static) = {
119 .function = my_func,
120 .name = "my-node2",
121 .scalar_size = sizeof (my_frame_t),
122 .vector_size = STRUCT_SIZE_OF (my_frame_t, vector[0]),
123};
124
125#endif
126
127#if 0
128
Dave Barach9b8ffd92016-07-08 08:13:45 -0400129typedef enum
130{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 MY_EVENT_TYPE1,
132 MY_EVENT_TYPE2,
133} my_process_completion_type_t;
134
Dave Barach9b8ffd92016-07-08 08:13:45 -0400135typedef struct
136{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 int a;
138 f64 b;
139} my_process_event_data_t;
140
Dave Barach9b8ffd92016-07-08 08:13:45 -0400141static u8 *
142format_my_process_event_data (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400144 my_process_event_data_t *d = va_arg (*va, my_process_event_data_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 return format (s, "{ a %d b %.6f}", d->a, d->b);
146}
147
148static uword
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149my_proc (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400151 vlib_node_t *node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 u32 i;
153
154 node = vlib_get_node (vm, rt->node_index);
155
156 vlib_cli_output (vm, "%v: call frame %p", node->name, f);
157
158 for (i = 0; i < 5; i++)
159 {
160 vlib_cli_output (vm, "%v: %d", node->name, i);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400161 vlib_process_suspend (vm, 1e0 /* secs */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 }
163
164 vlib_cli_output (vm, "%v: return frame %p", node->name, f);
165
166 if (0)
167 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400168 uword n_events_seen, type, *data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169
170 for (n_events_seen = 0; n_events_seen < 2;)
171 {
172 vlib_process_wait_for_event (vm);
173 type = vlib_process_get_events (vm, &data);
174 n_events_seen += vec_len (data);
175 vlib_cli_output (vm, "%U %v: completion #%d type %d data 0x%wx",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 format_time_interval, "h:m:s:u",
177 vlib_time_now (vm), node->name, i, type, data[0]);
Damjan Marion8bea5892022-04-04 22:40:45 +0200178 vec_set_len (data, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 }
180
181 vec_free (data);
182 }
183 else
184 {
185 uword n_events_seen, i, type;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400186 my_process_event_data_t *data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 for (n_events_seen = 0; n_events_seen < 2;)
188 {
189 vlib_process_wait_for_event (vm);
190 data = vlib_process_get_event_data (vm, &type);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400191 vec_foreach_index (i, data)
192 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 vlib_cli_output (vm, "%U event type %d data %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400194 format_time_interval, "h:m:s:u",
195 vlib_time_now (vm), type,
196 format_my_process_event_data, data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197 }
198 n_events_seen += vec_len (data);
199 vlib_process_put_event_data (vm, data);
200 }
201 }
202
203 return i;
204}
205
206VLIB_REGISTER_NODE (my_proc_node,static) = {
207 .function = my_proc,
208 .type = VLIB_NODE_TYPE_PROCESS,
209 .name = "my-proc",
210};
211
212static uword
Dave Barach9b8ffd92016-07-08 08:13:45 -0400213my_proc_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214{
215 static int i;
216
217 if (i++ < 2)
218 {
219 if (0)
220 vlib_process_signal_event (vm, my_proc_node.index,
221 i == 1 ? MY_EVENT_TYPE1 : MY_EVENT_TYPE2,
222 0x12340000 + i);
223 else
224 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400225 my_process_event_data_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 f64 dt = 5;
227 d = vlib_process_signal_event_at_time (vm,
228 i * dt,
229 my_proc_node.index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400230 i ==
231 1 ? MY_EVENT_TYPE1 :
232 MY_EVENT_TYPE2,
233 1 /* elts */ ,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 sizeof (d[0]));
235 d->a = i;
236 d->b = vlib_time_now (vm);
237 }
238 }
239 else
240 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
241
Dave Barach9b8ffd92016-07-08 08:13:45 -0400242 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243}
244
245VLIB_REGISTER_NODE (my_proc_input_node,static) = {
246 .function = my_proc_input,
247 .type = VLIB_NODE_TYPE_INPUT,
248 .name = "my-proc-input",
249};
250
Dave Barach9b8ffd92016-07-08 08:13:45 -0400251static uword
252_unformat_farith (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253{
254 u32 prec = va_arg (*args, u32);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400255 f64 *result = va_arg (*args, f64 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256 f64 tmp[2];
257
258 /* Binary operations in from lowest to highest precedence. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400259 char *binops[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 "+%U", "-%U", "/%U", "*%U", "^%U",
261 };
262
263 if (prec <= ARRAY_LEN (binops) - 1
264 && unformat_user (i, _unformat_farith, prec + 1, &tmp[0]))
265 {
266 int p;
267 for (p = prec; p < ARRAY_LEN (binops); p++)
268 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400269 if (unformat (i, binops[p], _unformat_farith, prec + 0, &tmp[1]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270 {
271 switch (binops[p][0])
272 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400273 case '+':
274 result[0] = tmp[0] + tmp[1];
275 break;
276 case '-':
277 result[0] = tmp[0] - tmp[1];
278 break;
279 case '/':
280 result[0] = tmp[0] / tmp[1];
281 break;
282 case '*':
283 result[0] = tmp[0] * tmp[1];
284 break;
285 case '^':
286 result[0] = pow (tmp[0], tmp[1]);
287 break;
288 default:
289 abort ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 }
291 return 1;
292 }
293 }
294 result[0] = tmp[0];
295 return 1;
296 }
297
Dave Barach9b8ffd92016-07-08 08:13:45 -0400298 else if (unformat (i, "-%U", _unformat_farith, prec + 0, &tmp[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299 {
300 result[0] = -tmp[0];
301 return 1;
302 }
303
Dave Barach9b8ffd92016-07-08 08:13:45 -0400304 else if (unformat (i, "(%U)", _unformat_farith, 0, &tmp[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305 {
306 result[0] = tmp[0];
307 return 1;
308 }
309
310 else if (unformat (i, "%f", result))
311 return 1;
312
313 else
314 return 0;
315}
316
Dave Barach9b8ffd92016-07-08 08:13:45 -0400317static uword
318unformat_farith (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319{
320 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400321 f64 *result = va_arg (*args, f64 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 return unformat_user (i, _unformat_farith, 0, result);
323}
324
Dave Barach9b8ffd92016-07-08 08:13:45 -0400325static uword
326unformat_integer (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327{
328 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400329 u32 *data = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 return unformat (i, "%d", data);
331}
332
Dave Barach9b8ffd92016-07-08 08:13:45 -0400333static VLIB_CLI_PARSE_RULE (my_parse_rule1) =
334{
335.name = "decimal_integer",.short_help =
336 "a decimal integer",.unformat_function = unformat_integer,.data_size =
337 sizeof (u32),};
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
Dave Barach9b8ffd92016-07-08 08:13:45 -0400339static VLIB_CLI_PARSE_RULE (my_parse_rule2) =
340{
341.name = "float_expression",.short_help =
342 "floating point expression",.unformat_function =
343 unformat_farith,.data_size = sizeof (f64),};
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
345static clib_error_t *
346bar_command (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400347 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348{
349 switch (cmd->function_arg)
350 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400351 case 2:
352 {
353 u32 *d, *e;
354 d = vlib_cli_get_parse_rule_result (vm, 0);
355 e = vlib_cli_get_parse_rule_result (vm, 1);
356 vlib_cli_output (vm, "bar2 %d %d", d[0], e[0]);
357 break;
358 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360 case 1:
361 {
362 u32 *d = vlib_cli_get_parse_rule_result (vm, 0);
363 vlib_cli_output (vm, "bar1 %d", d[0]);
364 break;
365 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366
Dave Barach9b8ffd92016-07-08 08:13:45 -0400367 case 3:
368 {
369 f64 *d = vlib_cli_get_parse_rule_result (vm, 0);
370 vlib_cli_output (vm, "expr %.6f", d[0]);
371 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 }
373
374 return 0;
375}
376
377VLIB_CLI_COMMAND (bar_command2, static) = {
378 .path = "bar %decimal_integer",
379 .short_help = "bar1 command",
380 .function = bar_command,
381 .function_arg = 1,
382};
383VLIB_CLI_COMMAND (bar_command1, static) = {
384 .path = "bar %decimal_integer %decimal_integer",
385 .short_help = "bar2 command",
386 .function = bar_command,
387 .function_arg = 2,
388};
389VLIB_CLI_COMMAND (bar_command3, static) = {
390 .path = "zap %float_expression",
391 .short_help = "bar3 command",
392 .function = bar_command,
393 .function_arg = 3,
394};
395
396#endif
397
Dave Barach9b8ffd92016-07-08 08:13:45 -0400398/*
399 * fd.io coding-style-patch-verification: ON
400 *
401 * Local Variables:
402 * eval: (c-set-style "gnu")
403 * End:
404 */