FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
api_main.c
Go to the documentation of this file.
1 #include "vat.h"
2 
3 vat_main_t vat_main;
4 
5 void
7 {
8  vlib_process_suspend (vm, interval);
9 }
10 
11 static u8 *
12 format_api_error (u8 * s, va_list * args)
13 {
14  vat_main_t *vam = va_arg (*args, vat_main_t *);
15  i32 error = va_arg (*args, u32);
16  uword *p;
17 
18  p = hash_get (vam->error_string_by_error_number, -error);
19 
20  if (p)
21  s = format (s, "%s", p[0]);
22  else
23  s = format (s, "%d", error);
24  return s;
25 }
26 
27 
28 static void
29 init_error_string_table (vat_main_t * vam)
30 {
31 
32  vam->error_string_by_error_number = hash_create (0, sizeof (uword));
33 
34 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
36 #undef _
37 
38  hash_set (vam->error_string_by_error_number, 99, "Misc");
39 }
40 
41 static clib_error_t *
43 {
44  vat_main_t *vam = &vat_main;
45  int rv;
46  int vat_plugin_init (vat_main_t * vam);
47 
48  vam->vlib_main = vm;
49  vam->my_client_index = (u32) ~ 0;
50  /* Ensure that vam->inbuf is never NULL */
51  vec_validate (vam->inbuf, 0);
53  rv = vat_plugin_init (vam);
54  if (rv)
55  clib_warning ("vat_plugin_init returned %d", rv);
56  return 0;
57 }
58 
60 
61 void
63 {
64  vat_main_t *vam = &vat_main;
65 
66  vam->sw_if_index_by_interface_name = hash_create_string (0, sizeof (uword));
67  vam->function_by_name = hash_create_string (0, sizeof (uword));
68  vam->help_by_name = hash_create_string (0, sizeof (uword));
69 }
70 
71 static clib_error_t *
73  unformat_input_t * input, vlib_cli_command_t * cmd)
74 {
75  vat_main_t *vam = &vat_main;
76  unformat_input_t _input;
77  uword c;
78  u8 *cmdp, *argsp, *this_cmd;
79  uword *p;
80  u32 arg_len;
81  int rv;
82  int (*fp) (vat_main_t *);
83  api_main_t *am = &api_main;
84 
86 
87  /* vec_validated in the init routine */
88  _vec_len (vam->inbuf) = 0;
89 
90  vam->input = &_input;
91 
92  while (((c = unformat_get_input (input)) != '\n') &&
93  (c != UNFORMAT_END_OF_INPUT))
94  vec_add1 (vam->inbuf, c);
95 
96  /* Null-terminate the command */
97  vec_add1 (vam->inbuf, 0);
98 
99  /* In case no args given */
100  vec_add1 (vam->inbuf, 0);
101 
102  /* Split input into cmd + args */
103  this_cmd = cmdp = vam->inbuf;
104 
105  /* Skip leading whitespace */
106  while (cmdp < (this_cmd + vec_len (this_cmd)))
107  {
108  if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
109  {
110  cmdp++;
111  }
112  else
113  break;
114  }
115 
116  argsp = cmdp;
117 
118  /* Advance past the command */
119  while (argsp < (this_cmd + vec_len (this_cmd)))
120  {
121  if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n' && *argsp != 0)
122  {
123  argsp++;
124  }
125  else
126  break;
127  }
128  /* NULL terminate the command */
129  *argsp++ = 0;
130 
131  /* No arguments? Ensure that argsp points to a proper (empty) string */
132  if (argsp == (this_cmd + vec_len (this_cmd) - 1))
133  argsp[0] = 0;
134  else
135  while (argsp < (this_cmd + vec_len (this_cmd)))
136  {
137  if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
138  {
139  argsp++;
140  }
141  else
142  break;
143  }
144 
145  /* Blank input line? */
146  if (*cmdp == 0)
147  return 0;
148 
149  p = hash_get_mem (vam->function_by_name, cmdp);
150  if (p == 0)
151  {
152  return clib_error_return (0, "'%s': function not found\n", cmdp);
153  }
154 
155  arg_len = strlen ((char *) argsp);
156 
157  unformat_init_string (vam->input, (char *) argsp, arg_len);
158  fp = (void *) p[0];
159 
160  rv = (*fp) (vam);
161 
162  if (rv < 0)
163  {
164  unformat_free (vam->input);
165  return clib_error_return (0,
166  "%s error: %U\n", cmdp,
167  format_api_error, vam, rv);
168 
169  }
170  if (vam->regenerate_interface_table)
171  {
172  vam->regenerate_interface_table = 0;
173  api_sw_interface_dump (vam);
174  }
175  unformat_free (vam->input);
176  return 0;
177 }
178 
179 /* *INDENT-OFF* */
180 VLIB_CLI_COMMAND (api_command, static) =
181 {
182  .path = "binary-api",
183  .short_help = "binary-api [help] <name> [<args>]",
184  .function = api_command_fn,
185 };
186 /* *INDENT-ON* */
187 
188 void
189 api_cli_output (void *notused, const char *fmt, ...)
190 {
191  va_list va;
192  vat_main_t *vam = &vat_main;
193  vlib_main_t *vm = vam->vlib_main;
195  u8 *s;
196 
197  va_start (va, fmt);
198  s = va_format (0, fmt, &va);
199  va_end (va);
200 
201  /* Terminate with \n if not present. */
202  if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
203  vec_add1 (s, '\n');
204 
205  if ((!cp) || (!cp->output_function))
206  fformat (stdout, "%v", s);
207  else
208  cp->output_function (cp->output_function_arg, s, vec_len (s));
209 
210  vec_free (s);
211 }
212 
213 u16
215 {
216  api_main_t *am = &api_main;
217  vl_api_msg_range_t *rp;
218  uword *p;
219 
220  p = hash_get_mem (am->msg_range_by_name, plugin_name);
221  if (p == 0)
222  return ~0;
223 
224  rp = vec_elt_at_index (am->msg_ranges, p[0]);
225 
226  return (rp->first_msg_id);
227 }
228 
229 uword
230 unformat_sw_if_index (unformat_input_t * input, va_list * args)
231 {
232  u32 *result = va_arg (*args, u32 *);
233  vnet_main_t *vnm = vnet_get_main ();
234  u32 sw_if_index = ~0;
235  u8 *if_name;
236  uword *p;
237 
238  if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
239  {
240  *result = sw_if_index;
241  return 1;
242  }
243  return 0;
244 }
245 
246 /*
247  * fd.io coding-style-patch-verification: ON
248  *
249  * Local Variables:
250  * eval: (c-set-style "gnu")
251  * End:
252  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:436
uword output_function_arg
Definition: node.h:546
#define hash_set(h, key, value)
Definition: hash.h:254
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static uword unformat_get_input(unformat_input_t *input)
Definition: format.h:190
unix_shared_memory_queue_t * vl_input_queue
Definition: api.h:73
static u8 * format_api_error(u8 *s, va_list *args)
Definition: api_main.c:12
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
u8 * va_format(u8 *s, const char *fmt, va_list *va)
Definition: format.c:386
unformat_function_t unformat_vnet_sw_interface
unix_shared_memory_queue_t * vl_input_queue
Definition: api.h:179
static clib_error_t * api_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: api_main.c:72
api_main_t api_main
Definition: api_shared.c:35
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:432
vat_main_t vat_main
Definition: api_main.c:3
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
int i32
Definition: types.h:81
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:111
struct vl_shmem_hdr_ * shmem_hdr
Definition: api.h:144
#define hash_create_string(elts, value_bytes)
Definition: hash.h:652
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1022
u16 vl_client_get_first_plugin_msg_id(char *plugin_name)
Definition: api_main.c:214
#define hash_get(h, key)
Definition: hash.h:248
struct _unformat_input_t unformat_input_t
word fformat(FILE *f, char *fmt,...)
Definition: format.c:452
uword unformat_sw_if_index(unformat_input_t *input, va_list *args)
Definition: api_main.c:230
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
svmdb_client_t * c
static vlib_process_t * vlib_get_current_process(vlib_main_t *vm)
Definition: node_funcs.h:397
vlib_main_t * vm
Definition: buffer.c:276
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
vl_api_msg_range_t * msg_ranges
Definition: api.h:156
#define clib_warning(format, args...)
Definition: error.h:59
void vat_suspend(vlib_main_t *vm, f64 interval)
Definition: api_main.c:6
#define foreach_vnet_api_error
Definition: api_errno.h:18
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define hash_create(elts, value_bytes)
Definition: hash.h:658
unsigned int u32
Definition: types.h:88
void vat_plugin_hash_create(void)
Definition: api_main.c:62
static void init_error_string_table(vat_main_t *vam)
Definition: api_main.c:29
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
u16 first_msg_id
Definition: api.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
static clib_error_t * api_main_init(vlib_main_t *vm)
Definition: api_main.c:42
int vat_plugin_init(vat_main_t *vam)
Definition: plugin.c:180
#define hash_get_mem(h, key)
Definition: hash.h:268
void api_cli_output(void *notused, const char *fmt,...)
Definition: api_main.c:189
vlib_cli_output_function_t * output_function
Definition: node.h:545
uword * msg_range_by_name
Definition: api.h:153
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971