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>
19 int main (int argc, char * argv[])
21 return vlib_unix_main (argc, argv);
25 main_stub_init (vlib_main_t * vm)
29 if ((error = unix_physmem_init (vm, /* fail_if_physical_memory_not_present */ 0)))
32 if ((error = vlib_call_init_function (vm, unix_cli_init)))
38 VLIB_INIT_FUNCTION (main_stub_init);
47 static u8 * format_my_node_frame (u8 * s, va_list * va)
49 vlib_frame_t * f = va_arg (*va, vlib_frame_t *);
50 my_frame_t * g = vlib_frame_args (f);
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]);
62 my_func (vlib_main_t * vm,
63 vlib_node_runtime_t * rt,
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_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);
108 VLIB_REGISTER_NODE (my_node1,static) = {
110 .type = VLIB_NODE_TYPE_INPUT,
112 .scalar_size = sizeof (my_frame_t),
113 .vector_size = STRUCT_SIZE_OF (my_frame_t, vector[0]),
120 VLIB_REGISTER_NODE (my_node2,static) = {
123 .scalar_size = sizeof (my_frame_t),
124 .vector_size = STRUCT_SIZE_OF (my_frame_t, vector[0]),
134 } my_process_completion_type_t;
139 } my_process_event_data_t;
141 static u8 * format_my_process_event_data (u8 * s, va_list * va)
143 my_process_event_data_t * d = va_arg (*va, my_process_event_data_t *);
144 return format (s, "{ a %d b %.6f}", d->a, d->b);
148 my_proc (vlib_main_t * vm,
149 vlib_node_runtime_t * rt,
155 node = vlib_get_node (vm, rt->node_index);
157 vlib_cli_output (vm, "%v: call frame %p", node->name, f);
159 for (i = 0; i < 5; i++)
161 vlib_cli_output (vm, "%v: %d", node->name, i);
162 vlib_process_suspend (vm, 1e0 /* secs */);
165 vlib_cli_output (vm, "%v: return frame %p", node->name, f);
169 uword n_events_seen, type, * data = 0;
171 for (n_events_seen = 0; n_events_seen < 2;)
173 vlib_process_wait_for_event (vm);
174 type = vlib_process_get_events (vm, &data);
175 n_events_seen += vec_len (data);
176 vlib_cli_output (vm, "%U %v: completion #%d type %d data 0x%wx",
177 format_time_interval, "h:m:s:u", vlib_time_now (vm),
178 node->name, i, type, data[0]);
186 uword n_events_seen, i, type;
187 my_process_event_data_t * data;
188 for (n_events_seen = 0; n_events_seen < 2;)
190 vlib_process_wait_for_event (vm);
191 data = vlib_process_get_event_data (vm, &type);
192 vec_foreach_index (i, data) {
193 vlib_cli_output (vm, "%U event type %d data %U",
194 format_time_interval, "h:m:s:u", vlib_time_now (vm),
195 type, format_my_process_event_data, data);
197 n_events_seen += vec_len (data);
198 vlib_process_put_event_data (vm, data);
205 VLIB_REGISTER_NODE (my_proc_node,static) = {
207 .type = VLIB_NODE_TYPE_PROCESS,
212 my_proc_input (vlib_main_t * vm,
213 vlib_node_runtime_t * rt,
221 vlib_process_signal_event (vm, my_proc_node.index,
222 i == 1 ? MY_EVENT_TYPE1 : MY_EVENT_TYPE2,
226 my_process_event_data_t * d;
228 d = vlib_process_signal_event_at_time (vm,
231 i == 1 ? MY_EVENT_TYPE1 : MY_EVENT_TYPE2,
235 d->b = vlib_time_now (vm);
239 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
244 VLIB_REGISTER_NODE (my_proc_input_node,static) = {
245 .function = my_proc_input,
246 .type = VLIB_NODE_TYPE_INPUT,
247 .name = "my-proc-input",
250 static uword _unformat_farith (unformat_input_t * i, va_list * args)
252 u32 prec = va_arg (*args, u32);
253 f64 * result = va_arg (*args, f64 *);
256 /* Binary operations in from lowest to highest precedence. */
258 "+%U", "-%U", "/%U", "*%U", "^%U",
261 if (prec <= ARRAY_LEN (binops) - 1
262 && unformat_user (i, _unformat_farith, prec + 1, &tmp[0]))
265 for (p = prec; p < ARRAY_LEN (binops); p++)
267 if (unformat (i, binops[p],
268 _unformat_farith, prec + 0, &tmp[1]))
270 switch (binops[p][0])
272 case '+': result[0] = tmp[0] + tmp[1]; break;
273 case '-': result[0] = tmp[0] - tmp[1]; break;
274 case '/': result[0] = tmp[0] / tmp[1]; break;
275 case '*': result[0] = tmp[0] * tmp[1]; break;
276 case '^': result[0] = pow (tmp[0], tmp[1]); break;
286 else if (unformat (i, "-%U",
287 _unformat_farith, prec + 0, &tmp[0]))
293 else if (unformat (i, "(%U)",
294 _unformat_farith, 0, &tmp[0]))
300 else if (unformat (i, "%f", result))
307 static uword unformat_farith (unformat_input_t * i, va_list * args)
309 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
310 f64 * result = va_arg (*args, f64 *);
311 return unformat_user (i, _unformat_farith, 0, result);
314 static uword unformat_integer (unformat_input_t * i, va_list * args)
316 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
317 u32 * data = va_arg (*args, u32 *);
318 return unformat (i, "%d", data);
321 static VLIB_CLI_PARSE_RULE (my_parse_rule1) = {
322 .name = "decimal_integer",
323 .short_help = "a decimal integer",
324 .unformat_function = unformat_integer,
325 .data_size = sizeof (u32),
328 static VLIB_CLI_PARSE_RULE (my_parse_rule2) = {
329 .name = "float_expression",
330 .short_help = "floating point expression",
331 .unformat_function = unformat_farith,
332 .data_size = sizeof (f64),
335 static clib_error_t *
336 bar_command (vlib_main_t * vm,
337 unformat_input_t * input,
338 vlib_cli_command_t * cmd)
340 switch (cmd->function_arg)
344 d = vlib_cli_get_parse_rule_result (vm, 0);
345 e = vlib_cli_get_parse_rule_result (vm, 1);
346 vlib_cli_output (vm, "bar2 %d %d", d[0], e[0]);
351 u32 * d = vlib_cli_get_parse_rule_result (vm, 0);
352 vlib_cli_output (vm, "bar1 %d", d[0]);
357 f64 * d = vlib_cli_get_parse_rule_result (vm, 0);
358 vlib_cli_output (vm, "expr %.6f", d[0]);
365 VLIB_CLI_COMMAND (bar_command2, static) = {
366 .path = "bar %decimal_integer",
367 .short_help = "bar1 command",
368 .function = bar_command,
371 VLIB_CLI_COMMAND (bar_command1, static) = {
372 .path = "bar %decimal_integer %decimal_integer",
373 .short_help = "bar2 command",
374 .function = bar_command,
377 VLIB_CLI_COMMAND (bar_command3, static) = {
378 .path = "zap %float_expression",
379 .short_help = "bar3 command",
380 .function = bar_command,