FD.io VPP  v21.06-3-gbb25fbf28
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
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;
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
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  {
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  {
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);
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  */
vlib.h
tmp
u32 * tmp
Definition: interface_output.c:1078
vlib_frame_t::n_vectors
u16 n_vectors
Definition: node.h:387
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
vlib_process_wait_for_event
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:660
f
vlib_frame_t * f
Definition: interface_output.c:1080
vlib_node_set_state
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:175
next
u16 * next
Definition: nat44_ei_out2in.c:718
VLIB_FRAME_SIZE
#define VLIB_FRAME_SIZE
Definition: node.h:368
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
vlib_cli_command_t::path
char * path
Definition: cli.h:96
VLIB_NODE_TYPE_INPUT
@ VLIB_NODE_TYPE_INPUT
Definition: node.h:76
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
unformat_input_t
struct _unformat_input_t unformat_input_t
vlib_frame_t
Definition: node.h:372
vlib_process_signal_event
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1019
error
Definition: cJSON.c:88
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
vlib_unix_main
int vlib_unix_main(int argc, char *argv[])
Definition: main.c:694
vlib_process_get_events
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:583
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
math.h
vlib_process_get_event_data
static void * vlib_process_get_event_data(vlib_main_t *vm, uword *return_event_type_opaque)
Definition: node_funcs.h:530
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vec_foreach_index
#define vec_foreach_index(var, v)
Iterate over vector indices.
Definition: vec_bootstrap.h:220
uword
u64 uword
Definition: types.h:112
vlib_get_node
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:86
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
f64
double f64
Definition: types.h:142
format_time_interval
u8 * format_time_interval(u8 *s, va_list *args)
Definition: std-formats.c:138
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
vlib_frame_scalar_args
static void * vlib_frame_scalar_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:315
data
u8 data[128]
Definition: ipsec_types.api:92
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
main_stub_init
static clib_error_t * main_stub_init(vlib_main_t *vm)
Definition: main_stub.c:26
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
vlib_put_next_frame
vlib_put_next_frame(vm, node, next_index, 0)
vlib_process_suspend
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:486
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
vlib_process_put_event_data
static void vlib_process_put_event_data(vlib_main_t *vm, void *event_data)
Definition: node_funcs.h:567
n_left
u32 n_left
Definition: interface_output.c:1078
VLIB_NODE_TYPE_PROCESS
@ VLIB_NODE_TYPE_PROCESS
Definition: node.h:84
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
VLIB_INITS
#define VLIB_INITS(...)
Definition: init.h:352
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
a
a
Definition: bitmap.h:544
unix.h
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:399
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
VLIB_CLI_PARSE_RULE
#define VLIB_CLI_PARSE_RULE(x)
Definition: cli.h:191
vlib_time_now
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
vlib_node_runtime_t
Definition: node.h:454
main
int main(int argc, char *argv[])
Definition: main_stub.c:20
vlib_cli_command_t::function_arg
uword function_arg
Definition: cli.h:106
vlib_cli_command_t
Definition: cli.h:92
vlib_get_next_frame
#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:395
unformat_integer
static uword unformat_integer(unformat_input_t *input, va_list *va, uword base, uword is_signed, uword data_bytes)
Definition: unformat.c:430
STRUCT_SIZE_OF
#define STRUCT_SIZE_OF(t, f)
Definition: clib.h:75
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_process_signal_event_at_time
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:942
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169