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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vlib/vlib.h>
16 #include <vlib/unix/unix.h>
20 main (int argc, char *argv[])
22 return vlib_unix_main (argc, argv);
26 main_stub_init (vlib_main_t * vm)
28 clib_error_t *error = 0;
34 VLIB_INIT_FUNCTION (main_stub_init) =
36 .runs_after = VLIB_INITS("unix_physmem_init", "unix_cli_init"),
49 format_my_node_frame (u8 * s, va_list * va)
51 vlib_frame_t *f = va_arg (*va, vlib_frame_t *);
52 my_frame_t *g = vlib_frame_scalar_args (f);
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]);
64 my_func (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
72 node = vlib_get_node (vm, rt->node_index);
77 vlib_cli_output (vm, "%v: call frame %p %U", node->name,
78 f, format_my_node_frame, f);
80 if (rt->n_next_nodes > 0)
82 vlib_frame_t *next = vlib_get_next_frame (vm, rt, /* next index */ 0);
83 n_left = VLIB_FRAME_SIZE - next->n_vectors;
84 y = vlib_frame_scalar_args (next);
90 for (i = 0; i < 5; i++)
96 y->vector[i] = y->scalar + i;
100 vlib_put_next_frame (vm, rt, /* next index */ 0, n_left);
103 vlib_cli_output (vm, "%v: return frame %p", node->name, f);
109 VLIB_REGISTER_NODE (my_node1,static) = {
111 .type = VLIB_NODE_TYPE_INPUT,
113 .scalar_size = sizeof (my_frame_t),
114 .vector_size = STRUCT_SIZE_OF (my_frame_t, vector[0]),
123 VLIB_REGISTER_NODE (my_node2,static) = {
126 .scalar_size = sizeof (my_frame_t),
127 .vector_size = STRUCT_SIZE_OF (my_frame_t, vector[0]),
139 } my_process_completion_type_t;
145 } my_process_event_data_t;
148 format_my_process_event_data (u8 * s, va_list * va)
150 my_process_event_data_t *d = va_arg (*va, my_process_event_data_t *);
151 return format (s, "{ a %d b %.6f}", d->a, d->b);
155 my_proc (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
160 node = vlib_get_node (vm, rt->node_index);
162 vlib_cli_output (vm, "%v: call frame %p", node->name, f);
164 for (i = 0; i < 5; i++)
166 vlib_cli_output (vm, "%v: %d", node->name, i);
167 vlib_process_suspend (vm, 1e0 /* secs */ );
170 vlib_cli_output (vm, "%v: return frame %p", node->name, f);
174 uword n_events_seen, type, *data = 0;
176 for (n_events_seen = 0; n_events_seen < 2;)
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",
182 format_time_interval, "h:m:s:u",
183 vlib_time_now (vm), node->name, i, type, data[0]);
191 uword n_events_seen, i, type;
192 my_process_event_data_t *data;
193 for (n_events_seen = 0; n_events_seen < 2;)
195 vlib_process_wait_for_event (vm);
196 data = vlib_process_get_event_data (vm, &type);
197 vec_foreach_index (i, data)
199 vlib_cli_output (vm, "%U event type %d data %U",
200 format_time_interval, "h:m:s:u",
201 vlib_time_now (vm), type,
202 format_my_process_event_data, data);
204 n_events_seen += vec_len (data);
205 vlib_process_put_event_data (vm, data);
213 VLIB_REGISTER_NODE (my_proc_node,static) = {
215 .type = VLIB_NODE_TYPE_PROCESS,
221 my_proc_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
228 vlib_process_signal_event (vm, my_proc_node.index,
229 i == 1 ? MY_EVENT_TYPE1 : MY_EVENT_TYPE2,
233 my_process_event_data_t *d;
235 d = vlib_process_signal_event_at_time (vm,
244 d->b = vlib_time_now (vm);
248 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
254 VLIB_REGISTER_NODE (my_proc_input_node,static) = {
255 .function = my_proc_input,
256 .type = VLIB_NODE_TYPE_INPUT,
257 .name = "my-proc-input",
262 _unformat_farith (unformat_input_t * i, va_list * args)
264 u32 prec = va_arg (*args, u32);
265 f64 *result = va_arg (*args, f64 *);
268 /* Binary operations in from lowest to highest precedence. */
270 "+%U", "-%U", "/%U", "*%U", "^%U",
273 if (prec <= ARRAY_LEN (binops) - 1
274 && unformat_user (i, _unformat_farith, prec + 1, &tmp[0]))
277 for (p = prec; p < ARRAY_LEN (binops); p++)
279 if (unformat (i, binops[p], _unformat_farith, prec + 0, &tmp[1]))
281 switch (binops[p][0])
284 result[0] = tmp[0] + tmp[1];
287 result[0] = tmp[0] - tmp[1];
290 result[0] = tmp[0] / tmp[1];
293 result[0] = tmp[0] * tmp[1];
296 result[0] = pow (tmp[0], tmp[1]);
308 else if (unformat (i, "-%U", _unformat_farith, prec + 0, &tmp[0]))
314 else if (unformat (i, "(%U)", _unformat_farith, 0, &tmp[0]))
320 else if (unformat (i, "%f", result))
328 unformat_farith (unformat_input_t * i, va_list * args)
330 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
331 f64 *result = va_arg (*args, f64 *);
332 return unformat_user (i, _unformat_farith, 0, result);
336 unformat_integer (unformat_input_t * i, va_list * args)
338 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
339 u32 *data = va_arg (*args, u32 *);
340 return unformat (i, "%d", data);
343 static VLIB_CLI_PARSE_RULE (my_parse_rule1) =
345 .name = "decimal_integer",.short_help =
346 "a decimal integer",.unformat_function = unformat_integer,.data_size =
349 static VLIB_CLI_PARSE_RULE (my_parse_rule2) =
351 .name = "float_expression",.short_help =
352 "floating point expression",.unformat_function =
353 unformat_farith,.data_size = sizeof (f64),};
355 static clib_error_t *
356 bar_command (vlib_main_t * vm,
357 unformat_input_t * input, vlib_cli_command_t * cmd)
359 switch (cmd->function_arg)
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]);
372 u32 *d = vlib_cli_get_parse_rule_result (vm, 0);
373 vlib_cli_output (vm, "bar1 %d", d[0]);
379 f64 *d = vlib_cli_get_parse_rule_result (vm, 0);
380 vlib_cli_output (vm, "expr %.6f", d[0]);
388 VLIB_CLI_COMMAND (bar_command2, static) = {
389 .path = "bar %decimal_integer",
390 .short_help = "bar1 command",
391 .function = bar_command,
394 VLIB_CLI_COMMAND (bar_command1, static) = {
395 .path = "bar %decimal_integer %decimal_integer",
396 .short_help = "bar2 command",
397 .function = bar_command,
400 VLIB_CLI_COMMAND (bar_command3, static) = {
401 .path = "zap %float_expression",
402 .short_help = "bar3 command",
403 .function = bar_command,
411 * fd.io coding-style-patch-verification: ON
414 * eval: (c-set-style "gnu")