blob: 3b19c53f720271cb3f59515713bf046557eb6a99 [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 Barach9b8ffd92016-07-08 08:13:45 -040028 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Damjan Marion49d66f12017-07-20 18:10:35 +020030 if ((error = unix_physmem_init (vm)))
Ed Warnickecb9cada2015-12-08 15:45:58 -070031 return error;
32
Dave Barach9b8ffd92016-07-08 08:13:45 -040033 if ((error = vlib_call_init_function (vm, unix_cli_init)))
34 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070035
Dave Barach9b8ffd92016-07-08 08:13:45 -040036 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070037}
38
39VLIB_INIT_FUNCTION (main_stub_init);
40
41#if 0
42/* Node test code. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040043typedef struct
44{
Ed Warnickecb9cada2015-12-08 15:45:58 -070045 int scalar;
46 int vector[0];
47} my_frame_t;
48
Dave Barach9b8ffd92016-07-08 08:13:45 -040049static u8 *
50format_my_node_frame (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070051{
Dave Barach9b8ffd92016-07-08 08:13:45 -040052 vlib_frame_t *f = va_arg (*va, vlib_frame_t *);
53 my_frame_t *g = vlib_frame_args (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 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
64static uword
Dave Barach9b8ffd92016-07-08 08:13:45 -040065my_func (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -070066{
Dave Barach9b8ffd92016-07-08 08:13:45 -040067 vlib_node_t *node;
68 my_frame_t *y;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 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 {
Dave Barach9b8ffd92016-07-08 08:13:45 -040083 vlib_frame_t *next = vlib_get_next_frame (vm, rt, /* next index */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 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
Dave Barach9b8ffd92016-07-08 08:13:45 -0400109/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110VLIB_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};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400121/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122
Dave Barach9b8ffd92016-07-08 08:13:45 -0400123/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124VLIB_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};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400130/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
132#endif
133
134#if 0
135
Dave Barach9b8ffd92016-07-08 08:13:45 -0400136typedef enum
137{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 MY_EVENT_TYPE1,
139 MY_EVENT_TYPE2,
140} my_process_completion_type_t;
141
Dave Barach9b8ffd92016-07-08 08:13:45 -0400142typedef struct
143{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 int a;
145 f64 b;
146} my_process_event_data_t;
147
Dave Barach9b8ffd92016-07-08 08:13:45 -0400148static u8 *
149format_my_process_event_data (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400151 my_process_event_data_t *d = va_arg (*va, my_process_event_data_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 return format (s, "{ a %d b %.6f}", d->a, d->b);
153}
154
155static uword
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156my_proc (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400158 vlib_node_t *node;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 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);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400168 vlib_process_suspend (vm, 1e0 /* secs */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 }
170
171 vlib_cli_output (vm, "%v: return frame %p", node->name, f);
172
173 if (0)
174 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400175 uword n_events_seen, type, *data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
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",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400183 format_time_interval, "h:m:s:u",
184 vlib_time_now (vm), node->name, i, type, data[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 _vec_len (data) = 0;
186 }
187
188 vec_free (data);
189 }
190 else
191 {
192 uword n_events_seen, i, type;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400193 my_process_event_data_t *data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 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);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198 vec_foreach_index (i, data)
199 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200 vlib_cli_output (vm, "%U event type %d data %U",
Dave Barach9b8ffd92016-07-08 08:13:45 -0400201 format_time_interval, "h:m:s:u",
202 vlib_time_now (vm), type,
203 format_my_process_event_data, data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 }
205 n_events_seen += vec_len (data);
206 vlib_process_put_event_data (vm, data);
207 }
208 }
209
210 return i;
211}
212
Dave Barach9b8ffd92016-07-08 08:13:45 -0400213/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214VLIB_REGISTER_NODE (my_proc_node,static) = {
215 .function = my_proc,
216 .type = VLIB_NODE_TYPE_PROCESS,
217 .name = "my-proc",
218};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400219/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
221static uword
Dave Barach9b8ffd92016-07-08 08:13:45 -0400222my_proc_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223{
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 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400234 my_process_event_data_t *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235 f64 dt = 5;
236 d = vlib_process_signal_event_at_time (vm,
237 i * dt,
238 my_proc_node.index,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400239 i ==
240 1 ? MY_EVENT_TYPE1 :
241 MY_EVENT_TYPE2,
242 1 /* elts */ ,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 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
Dave Barach9b8ffd92016-07-08 08:13:45 -0400251 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252}
253
Dave Barach9b8ffd92016-07-08 08:13:45 -0400254/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255VLIB_REGISTER_NODE (my_proc_input_node,static) = {
256 .function = my_proc_input,
257 .type = VLIB_NODE_TYPE_INPUT,
258 .name = "my-proc-input",
259};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400260/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Dave Barach9b8ffd92016-07-08 08:13:45 -0400262static uword
263_unformat_farith (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264{
265 u32 prec = va_arg (*args, u32);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400266 f64 *result = va_arg (*args, f64 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 f64 tmp[2];
268
269 /* Binary operations in from lowest to highest precedence. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400270 char *binops[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 "+%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 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400280 if (unformat (i, binops[p], _unformat_farith, prec + 0, &tmp[1]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281 {
282 switch (binops[p][0])
283 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400284 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 ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 }
302 return 1;
303 }
304 }
305 result[0] = tmp[0];
306 return 1;
307 }
308
Dave Barach9b8ffd92016-07-08 08:13:45 -0400309 else if (unformat (i, "-%U", _unformat_farith, prec + 0, &tmp[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310 {
311 result[0] = -tmp[0];
312 return 1;
313 }
314
Dave Barach9b8ffd92016-07-08 08:13:45 -0400315 else if (unformat (i, "(%U)", _unformat_farith, 0, &tmp[0]))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316 {
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
Dave Barach9b8ffd92016-07-08 08:13:45 -0400328static uword
329unformat_farith (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330{
331 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400332 f64 *result = va_arg (*args, f64 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 return unformat_user (i, _unformat_farith, 0, result);
334}
335
Dave Barach9b8ffd92016-07-08 08:13:45 -0400336static uword
337unformat_integer (unformat_input_t * i, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338{
339 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400340 u32 *data = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 return unformat (i, "%d", data);
342}
343
Dave Barach9b8ffd92016-07-08 08:13:45 -0400344static 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),};
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
Dave Barach9b8ffd92016-07-08 08:13:45 -0400350static 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),};
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355
356static clib_error_t *
357bar_command (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400358 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359{
360 switch (cmd->function_arg)
361 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400362 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
Dave Barach9b8ffd92016-07-08 08:13:45 -0400371 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377
Dave Barach9b8ffd92016-07-08 08:13:45 -0400378 case 3:
379 {
380 f64 *d = vlib_cli_get_parse_rule_result (vm, 0);
381 vlib_cli_output (vm, "expr %.6f", d[0]);
382 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 }
384
385 return 0;
386}
387
Dave Barach9b8ffd92016-07-08 08:13:45 -0400388/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389VLIB_CLI_COMMAND (bar_command2, static) = {
390 .path = "bar %decimal_integer",
391 .short_help = "bar1 command",
392 .function = bar_command,
393 .function_arg = 1,
394};
395VLIB_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};
401VLIB_CLI_COMMAND (bar_command3, static) = {
402 .path = "zap %float_expression",
403 .short_help = "bar3 command",
404 .function = bar_command,
405 .function_arg = 3,
406};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400407/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
409#endif
410
Dave Barach9b8ffd92016-07-08 08:13:45 -0400411/*
412 * fd.io coding-style-patch-verification: ON
413 *
414 * Local Variables:
415 * eval: (c-set-style "gnu")
416 * End:
417 */