FD.io VPP  v19.04-6-g6f05f72
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;
29 
30  if ((error = unix_physmem_init (vm)))
31  return error;
32 
33  if ((error = vlib_call_init_function (vm, unix_cli_init)))
34  return error;
35 
36  return error;
37 }
38 
40 
41 #if 0
42 /* Node test code. */
43 typedef struct
44 {
45  int scalar;
46  int vector[0];
47 } my_frame_t;
48 
49 static u8 *
50 format_my_node_frame (u8 * s, va_list * va)
51 {
52  vlib_frame_t *f = va_arg (*va, vlib_frame_t *);
53  my_frame_t *g = vlib_frame_scalar_args (f);
54  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 
64 static uword
65 my_func (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
66 {
67  vlib_node_t *node;
68  my_frame_t *y;
69  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  {
83  vlib_frame_t *next = vlib_get_next_frame (vm, rt, /* next index */ 0);
84  n_left = VLIB_FRAME_SIZE - next->n_vectors;
85  y = vlib_frame_scalar_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 
109 /* *INDENT-OFF* */
110 VLIB_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 };
121 /* *INDENT-ON* */
122 
123 /* *INDENT-OFF* */
124 VLIB_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 };
130 /* *INDENT-ON* */
131 
132 #endif
133 
134 #if 0
135 
136 typedef enum
137 {
138  MY_EVENT_TYPE1,
139  MY_EVENT_TYPE2,
140 } my_process_completion_type_t;
141 
142 typedef struct
143 {
144  int a;
145  f64 b;
146 } my_process_event_data_t;
147 
148 static u8 *
149 format_my_process_event_data (u8 * s, va_list * va)
150 {
151  my_process_event_data_t *d = va_arg (*va, my_process_event_data_t *);
152  return format (s, "{ a %d b %.6f}", d->a, d->b);
153 }
154 
155 static uword
156 my_proc (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
157 {
158  vlib_node_t *node;
159  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);
168  vlib_process_suspend (vm, 1e0 /* secs */ );
169  }
170 
171  vlib_cli_output (vm, "%v: return frame %p", node->name, f);
172 
173  if (0)
174  {
175  uword n_events_seen, type, *data = 0;
176 
177  for (n_events_seen = 0; n_events_seen < 2;)
178  {
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",
183  format_time_interval, "h:m:s:u",
184  vlib_time_now (vm), node->name, i, type, data[0]);
185  _vec_len (data) = 0;
186  }
187 
188  vec_free (data);
189  }
190  else
191  {
192  uword n_events_seen, i, type;
193  my_process_event_data_t *data;
194  for (n_events_seen = 0; n_events_seen < 2;)
195  {
197  data = vlib_process_get_event_data (vm, &type);
198  vec_foreach_index (i, data)
199  {
200  vlib_cli_output (vm, "%U event type %d data %U",
201  format_time_interval, "h:m:s:u",
202  vlib_time_now (vm), type,
203  format_my_process_event_data, data);
204  }
205  n_events_seen += vec_len (data);
206  vlib_process_put_event_data (vm, data);
207  }
208  }
209 
210  return i;
211 }
212 
213 /* *INDENT-OFF* */
214 VLIB_REGISTER_NODE (my_proc_node,static) = {
215  .function = my_proc,
216  .type = VLIB_NODE_TYPE_PROCESS,
217  .name = "my-proc",
218 };
219 /* *INDENT-ON* */
220 
221 static uword
222 my_proc_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
223 {
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  {
234  my_process_event_data_t *d;
235  f64 dt = 5;
237  i * dt,
238  my_proc_node.index,
239  i ==
240  1 ? MY_EVENT_TYPE1 :
241  MY_EVENT_TYPE2,
242  1 /* elts */ ,
243  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 
251  return 0;
252 }
253 
254 /* *INDENT-OFF* */
255 VLIB_REGISTER_NODE (my_proc_input_node,static) = {
256  .function = my_proc_input,
257  .type = VLIB_NODE_TYPE_INPUT,
258  .name = "my-proc-input",
259 };
260 /* *INDENT-ON* */
261 
262 static uword
263 _unformat_farith (unformat_input_t * i, va_list * args)
264 {
265  u32 prec = va_arg (*args, u32);
266  f64 *result = va_arg (*args, f64 *);
267  f64 tmp[2];
268 
269  /* Binary operations in from lowest to highest precedence. */
270  char *binops[] = {
271  "+%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  {
280  if (unformat (i, binops[p], _unformat_farith, prec + 0, &tmp[1]))
281  {
282  switch (binops[p][0])
283  {
284  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 ();
301  }
302  return 1;
303  }
304  }
305  result[0] = tmp[0];
306  return 1;
307  }
308 
309  else if (unformat (i, "-%U", _unformat_farith, prec + 0, &tmp[0]))
310  {
311  result[0] = -tmp[0];
312  return 1;
313  }
314 
315  else if (unformat (i, "(%U)", _unformat_farith, 0, &tmp[0]))
316  {
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 
328 static uword
329 unformat_farith (unformat_input_t * i, va_list * args)
330 {
331  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
332  f64 *result = va_arg (*args, f64 *);
333  return unformat_user (i, _unformat_farith, 0, result);
334 }
335 
336 static uword
337 unformat_integer (unformat_input_t * i, va_list * args)
338 {
339  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
340  u32 *data = va_arg (*args, u32 *);
341  return unformat (i, "%d", data);
342 }
343 
344 static 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),};
349 
350 static 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),};
355 
356 static clib_error_t *
357 bar_command (vlib_main_t * vm,
358  unformat_input_t * input, vlib_cli_command_t * cmd)
359 {
360  switch (cmd->function_arg)
361  {
362  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  }
370 
371  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  }
377 
378  case 3:
379  {
380  f64 *d = vlib_cli_get_parse_rule_result (vm, 0);
381  vlib_cli_output (vm, "expr %.6f", d[0]);
382  }
383  }
384 
385  return 0;
386 }
387 
388 /* *INDENT-OFF* */
389 VLIB_CLI_COMMAND (bar_command2, static) = {
390  .path = "bar %decimal_integer",
391  .short_help = "bar1 command",
392  .function = bar_command,
393  .function_arg = 1,
394 };
395 VLIB_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 };
401 VLIB_CLI_COMMAND (bar_command3, static) = {
402  .path = "zap %float_expression",
403  .short_help = "bar3 command",
404  .function = bar_command,
405  .function_arg = 3,
406 };
407 /* *INDENT-ON* */
408 
409 #endif
410 
411 /*
412  * fd.io coding-style-patch-verification: ON
413  *
414  * Local Variables:
415  * eval: (c-set-style "gnu")
416  * End:
417  */
int vlib_unix_main(int argc, char *argv[])
Definition: main.c:641
#define vec_foreach_index(var, v)
Iterate over vector indices.
#define CLIB_UNUSED(x)
Definition: clib.h:82
#define VLIB_CLI_PARSE_RULE(x)
Definition: cli.h:183
a
Definition: bitmap.h:538
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:623
static void * vlib_cli_get_parse_rule_result(vlib_main_t *vm, uword index)
Definition: cli_funcs.h:44
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
int i
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
u8 data[128]
Definition: ipsec.api:248
int main(int argc, char *argv[])
Definition: main_stub.c:20
unsigned char u8
Definition: types.h:56
static clib_error_t * unix_cli_init(vlib_main_t *vm)
Definition: cli.c:3679
double f64
Definition: types.h:142
static uword unformat_integer(unformat_input_t *input, va_list *va, uword base, uword is_signed, uword data_bytes)
Definition: unformat.c:424
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:452
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
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:546
unsigned int u32
Definition: types.h:88
#define vlib_call_init_function(vm, x)
Definition: init.h:260
#define VLIB_FRAME_SIZE
Definition: node.h:376
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:964
struct _unformat_input_t unformat_input_t
u32 node_index
Node index.
Definition: node.h:495
#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:368
u8 * name
Definition: node.h:263
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:887
u16 n_vectors
Definition: node.h:395
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static void * vlib_frame_scalar_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:288
#define ARRAY_LEN(x)
Definition: clib.h:62
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:465
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
#define ASSERT(truth)
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:493
#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:122
static void vlib_process_put_event_data(vlib_main_t *vm, void *event_data)
Definition: node_funcs.h:530
#define STRUCT_SIZE_OF(t, f)
Definition: clib.h:67
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:59
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:725
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:972