FD.io VPP  v20.05.1-5-g09f167997
Vector Packet Processing
main_stub.c
Go to the documentation of this file.
1 /*
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 
19 int
20 main (int argc, char *argv[])
21 {
22  return vlib_unix_main (argc, argv);
23 }
24 
25 static clib_error_t *
27 {
28  clib_error_t *error = 0;
29 
30  return error;
31 }
32 
33 /* *INDENT-OFF* */
35 {
36  .runs_after = VLIB_INITS("unix_physmem_init", "unix_cli_init"),
37 };
38 /* *INDENT-ON* */
39 
40 #if 0
41 /* Node test code. */
42 typedef struct
43 {
44  int scalar;
45  int vector[0];
46 } my_frame_t;
47 
48 static u8 *
49 format_my_node_frame (u8 * s, va_list * va)
50 {
51  vlib_frame_t *f = va_arg (*va, vlib_frame_t *);
52  my_frame_t *g = vlib_frame_scalar_args (f);
53  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 
63 static uword
64 my_func (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
65 {
67  my_frame_t *y;
68  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  {
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);
85  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 
108 /* *INDENT-OFF* */
109 VLIB_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 };
120 /* *INDENT-ON* */
121 
122 /* *INDENT-OFF* */
123 VLIB_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 };
129 /* *INDENT-ON* */
130 
131 #endif
132 
133 #if 0
134 
135 typedef enum
136 {
137  MY_EVENT_TYPE1,
138  MY_EVENT_TYPE2,
139 } my_process_completion_type_t;
140 
141 typedef struct
142 {
143  int a;
144  f64 b;
145 } my_process_event_data_t;
146 
147 static u8 *
148 format_my_process_event_data (u8 * s, va_list * va)
149 {
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);
152 }
153 
154 static uword
155 my_proc (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
156 {
157  vlib_node_t *node;
158  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);
167  vlib_process_suspend (vm, 1e0 /* secs */ );
168  }
169 
170  vlib_cli_output (vm, "%v: return frame %p", node->name, f);
171 
172  if (0)
173  {
174  uword n_events_seen, type, *data = 0;
175 
176  for (n_events_seen = 0; n_events_seen < 2;)
177  {
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]);
184  _vec_len (data) = 0;
185  }
186 
187  vec_free (data);
188  }
189  else
190  {
191  uword n_events_seen, i, type;
192  my_process_event_data_t *data;
193  for (n_events_seen = 0; n_events_seen < 2;)
194  {
196  data = vlib_process_get_event_data (vm, &type);
197  vec_foreach_index (i, data)
198  {
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);
203  }
204  n_events_seen += vec_len (data);
205  vlib_process_put_event_data (vm, data);
206  }
207  }
208 
209  return i;
210 }
211 
212 /* *INDENT-OFF* */
213 VLIB_REGISTER_NODE (my_proc_node,static) = {
214  .function = my_proc,
215  .type = VLIB_NODE_TYPE_PROCESS,
216  .name = "my-proc",
217 };
218 /* *INDENT-ON* */
219 
220 static uword
221 my_proc_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
222 {
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  {
233  my_process_event_data_t *d;
234  f64 dt = 5;
236  i * dt,
237  my_proc_node.index,
238  i ==
239  1 ? MY_EVENT_TYPE1 :
240  MY_EVENT_TYPE2,
241  1 /* elts */ ,
242  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 
250  return 0;
251 }
252 
253 /* *INDENT-OFF* */
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",
258 };
259 /* *INDENT-ON* */
260 
261 static uword
262 _unformat_farith (unformat_input_t * i, va_list * args)
263 {
264  u32 prec = va_arg (*args, u32);
265  f64 *result = va_arg (*args, f64 *);
266  f64 tmp[2];
267 
268  /* Binary operations in from lowest to highest precedence. */
269  char *binops[] = {
270  "+%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  {
279  if (unformat (i, binops[p], _unformat_farith, prec + 0, &tmp[1]))
280  {
281  switch (binops[p][0])
282  {
283  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 ();
300  }
301  return 1;
302  }
303  }
304  result[0] = tmp[0];
305  return 1;
306  }
307 
308  else if (unformat (i, "-%U", _unformat_farith, prec + 0, &tmp[0]))
309  {
310  result[0] = -tmp[0];
311  return 1;
312  }
313 
314  else if (unformat (i, "(%U)", _unformat_farith, 0, &tmp[0]))
315  {
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 
327 static uword
328 unformat_farith (unformat_input_t * i, va_list * args)
329 {
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);
333 }
334 
335 static uword
336 unformat_integer (unformat_input_t * i, va_list * args)
337 {
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);
341 }
342 
343 static 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),};
348 
349 static 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),};
354 
355 static clib_error_t *
356 bar_command (vlib_main_t * vm,
357  unformat_input_t * input, vlib_cli_command_t * cmd)
358 {
359  switch (cmd->function_arg)
360  {
361  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  }
369 
370  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  }
376 
377  case 3:
378  {
379  f64 *d = vlib_cli_get_parse_rule_result (vm, 0);
380  vlib_cli_output (vm, "expr %.6f", d[0]);
381  }
382  }
383 
384  return 0;
385 }
386 
387 /* *INDENT-OFF* */
388 VLIB_CLI_COMMAND (bar_command2, static) = {
389  .path = "bar %decimal_integer",
390  .short_help = "bar1 command",
391  .function = bar_command,
392  .function_arg = 1,
393 };
394 VLIB_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 };
400 VLIB_CLI_COMMAND (bar_command3, static) = {
401  .path = "zap %float_expression",
402  .short_help = "bar3 command",
403  .function = bar_command,
404  .function_arg = 3,
405 };
406 /* *INDENT-ON* */
407 
408 #endif
409 
410 /*
411  * fd.io coding-style-patch-verification: ON
412  *
413  * Local Variables:
414  * eval: (c-set-style "gnu")
415  * End:
416  */
int vlib_unix_main(int argc, char *argv[])
Definition: main.c:682
#define vec_foreach_index(var, v)
Iterate over vector indices.
#define CLIB_UNUSED(x)
Definition: clib.h:86
#define VLIB_CLI_PARSE_RULE(x)
Definition: cli.h:180
a
Definition: bitmap.h:538
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:593
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:291
static uword unformat_integer(unformat_input_t *input, va_list *va, uword base, uword is_signed, uword data_bytes)
Definition: unformat.c:430
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
int main(int argc, char *argv[])
Definition: main_stub.c:20
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:422
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:516
unsigned int u32
Definition: types.h:88
#define VLIB_FRAME_SIZE
Definition: node.h:380
vl_api_fib_path_type_t type
Definition: fib_types.api:123
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:934
struct _unformat_input_t unformat_input_t
u32 node_index
Node index.
Definition: node.h:498
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:338
u8 * name
Definition: node.h:266
uword function_arg
Definition: cli.h:105
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
static void * vlib_process_signal_event_at_time(vlib_main_t *vm, f64 dt, uword node_index, uword type_opaque, uword n_data_elts, uword n_data_elt_bytes)
Definition: node_funcs.h:857
u16 n_vectors
Definition: node.h:399
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static void * vlib_frame_scalar_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:258
#define ARRAY_LEN(x)
Definition: clib.h:66
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:483
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1599
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
#define ASSERT(truth)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:689
u8 data[128]
Definition: ipsec_types.api:89
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:148
static void * vlib_process_get_event_data(vlib_main_t *vm, uword *return_event_type_opaque)
Definition: node_funcs.h:463
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
u8 * format_time_interval(u8 *s, va_list *args)
Definition: std-formats.c:138
static void vlib_process_put_event_data(vlib_main_t *vm, void *event_data)
Definition: node_funcs.h:500
#define STRUCT_SIZE_OF(t, f)
Definition: clib.h:71
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:59
#define VLIB_INITS(...)
Definition: init.h:344
static clib_error_t * main_stub_init(vlib_main_t *vm)
Definition: main_stub.c:26
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978