FD.io VPP  v21.01.1
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 #if VPP_API_TEST_BUILTIN > 0
42 static void
43 load_features (void)
44 {
45  vat_registered_features_t *f;
46  vat_main_t *vam = &vat_main;
48 
49  f = vam->feature_function_registrations;
50 
51  while (f)
52  {
53  error = f->function (vam);
54  if (error)
55  {
56  clib_warning ("INIT FAILED");
57  }
58  f = f->next;
59  }
60 }
61 
64 {
65  vat_main_t *vam = &vat_main;
66  int rv;
67  int vat_plugin_init (vat_main_t * vam);
68 
69  vam->vlib_main = vm;
70  vam->my_client_index = (u32) ~ 0;
71  /* Ensure that vam->inbuf is never NULL */
72  vec_validate (vam->inbuf, 0);
73  vec_validate (vam->cmd_reply, 0);
74  vec_reset_length (vam->cmd_reply);
76  rv = vat_plugin_init (vam);
77  if (rv)
78  clib_warning ("vat_plugin_init returned %d", rv);
79 
80  load_features ();
81 
82  return 0;
83 }
84 #endif
85 
86 void
88 {
89  vat_main_t *vam = &vat_main;
90 
91  vam->sw_if_index_by_interface_name = hash_create_string (0, sizeof (uword));
92  vam->function_by_name = hash_create_string (0, sizeof (uword));
93  vam->help_by_name = hash_create_string (0, sizeof (uword));
94 }
95 
96 static void
97 maybe_register_api_client (vat_main_t * vam)
98 {
99  vl_api_registration_t **regpp;
100  vl_api_registration_t *regp;
101  void *oldheap;
102  api_main_t *am = vlibapi_get_main ();
103 
104  if (vam->my_client_index != ~0)
105  return;
106 
107  pool_get (am->vl_clients, regpp);
108 
109  oldheap = vl_msg_push_heap ();
110 
111  *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
112 
113  regp = *regpp;
114  clib_memset (regp, 0, sizeof (*regp));
116  regp->vl_api_registration_pool_index = regpp - am->vl_clients;
117  regp->vlib_rp = am->vlib_rp;
118  regp->shmem_hdr = am->shmem_hdr;
119 
120  /* Loopback connection */
122 
123  regp->name = format (0, "%s", "vpp-internal");
124  vec_add1 (regp->name, 0);
125 
126  vl_msg_pop_heap (oldheap);
127 
128  vam->my_client_index = vl_msg_api_handle_from_index_and_epoch
131 
132  vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
133  api_sw_interface_dump (vam);
134 }
135 
136 static clib_error_t *
138  unformat_input_t * input, vlib_cli_command_t * cmd)
139 {
140  vat_main_t *vam = &vat_main;
141  unformat_input_t _input;
142  uword c;
143  u8 *cmdp, *argsp, *this_cmd;
144  uword *p;
145  u32 arg_len;
146  int rv;
147  int (*fp) (vat_main_t *);
148 
150 
151  /* vec_validated in the init routine */
152  _vec_len (vam->inbuf) = 0;
153 
154  vam->input = &_input;
155 
156  while (((c = unformat_get_input (input)) != '\n') &&
157  (c != UNFORMAT_END_OF_INPUT))
158  vec_add1 (vam->inbuf, c);
159 
160  /* Null-terminate the command */
161  vec_add1 (vam->inbuf, 0);
162 
163  /* In case no args given */
164  vec_add1 (vam->inbuf, 0);
165 
166  /* Split input into cmd + args */
167  this_cmd = cmdp = vam->inbuf;
168 
169  /* Skip leading whitespace */
170  while (cmdp < (this_cmd + vec_len (this_cmd)))
171  {
172  if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
173  {
174  cmdp++;
175  }
176  else
177  break;
178  }
179 
180  argsp = cmdp;
181 
182  /* Advance past the command */
183  while (argsp < (this_cmd + vec_len (this_cmd)))
184  {
185  if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n' && *argsp != 0)
186  {
187  argsp++;
188  }
189  else
190  break;
191  }
192  /* NULL terminate the command */
193  *argsp++ = 0;
194 
195  /* No arguments? Ensure that argsp points to a proper (empty) string */
196  if (argsp == (this_cmd + vec_len (this_cmd) - 1))
197  argsp[0] = 0;
198  else
199  while (argsp < (this_cmd + vec_len (this_cmd)))
200  {
201  if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
202  {
203  argsp++;
204  }
205  else
206  break;
207  }
208 
209  /* Blank input line? */
210  if (*cmdp == 0)
211  return 0;
212 
213  p = hash_get_mem (vam->function_by_name, cmdp);
214  if (p == 0)
215  {
216  return clib_error_return (0, "'%s': function not found\n", cmdp);
217  }
218 
219  arg_len = strlen ((char *) argsp);
220 
221  unformat_init_string (vam->input, (char *) argsp, arg_len);
222  fp = (void *) p[0];
223 
224  rv = (*fp) (vam);
225 
226  if (rv < 0)
227  {
228  unformat_free (vam->input);
229  return clib_error_return (0,
230  "%s error: %U\n", cmdp,
231  format_api_error, vam, rv);
232 
233  }
234  if (vam->regenerate_interface_table)
235  {
236  vam->regenerate_interface_table = 0;
237  api_sw_interface_dump (vam);
238  }
239  unformat_free (vam->input);
240  return 0;
241 }
242 
243 /* *INDENT-OFF* */
244 VLIB_CLI_COMMAND (api_command, static) =
245 {
246  .path = "binary-api",
247  .short_help = "binary-api [help] <name> [<args>]",
248  .function = api_command_fn,
249  .is_mp_safe = 1,
250 };
251 /* *INDENT-ON* */
252 
253 void
254 api_cli_output (void *notused, const char *fmt, ...)
255 {
256  va_list va;
257  vat_main_t *vam = &vat_main;
258  vlib_main_t *vm = vam->vlib_main;
260  u8 *s;
261 
262  va_start (va, fmt);
263  s = va_format (0, fmt, &va);
264  va_end (va);
265 
266  /* Terminate with \n if not present. */
267  if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
268  vec_add1 (s, '\n');
269 
270  if ((!cp) || (!cp->output_function))
271  fformat (stdout, "%v", s);
272  else
273  cp->output_function (cp->output_function_arg, s, vec_len (s));
274 
275  vec_free (s);
276 }
277 
278 u16
279 vl_client_get_first_plugin_msg_id (const char *plugin_name)
280 {
281  api_main_t *am = vlibapi_get_main ();
282  vl_api_msg_range_t *rp;
283  uword *p;
284 
285  p = hash_get_mem (am->msg_range_by_name, plugin_name);
286  if (p == 0)
287  return ~0;
288 
289  rp = vec_elt_at_index (am->msg_ranges, p[0]);
290 
291  return (rp->first_msg_id);
292 }
293 
294 uword
295 unformat_sw_if_index (unformat_input_t * input, va_list * args)
296 {
297  void *vam_unused = va_arg (*args, void *);
298  (void) (vam_unused);
299  u32 *result = va_arg (*args, u32 *);
300  vnet_main_t *vnm = vnet_get_main ();
301  u32 sw_if_index = ~0;
302 
303  if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
304  {
305  *result = sw_if_index;
306  return 1;
307  }
308  return 0;
309 }
310 
311 /*
312  * fd.io coding-style-patch-verification: ON
313  *
314  * Local Variables:
315  * eval: (c-set-style "gnu")
316  * End:
317  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
Message range (belonging to a plugin)
Definition: api_common.h:113
__clib_export u8 * va_format(u8 *s, const char *fmt, va_list *va)
Definition: format.c:387
uword output_function_arg
Definition: node.h:609
u8 * name
Client name.
Definition: api_common.h:54
#define hash_set(h, key, value)
Definition: hash.h:255
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static uword unformat_get_input(unformat_input_t *input)
Definition: format.h:191
u32 application_restarts
Definition: memory_shared.h:95
static u8 * format_api_error(u8 *s, va_list *args)
Definition: api_main.c:12
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void maybe_register_api_client(vat_main_t *vam)
Definition: api_main.c:97
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
vlib_main_t * vm
Definition: in2out_ed.c:1580
unformat_function_t unformat_vnet_sw_interface
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:251
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
static clib_error_t * api_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: api_main.c:137
__clib_export word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
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:482
vat_main_t vat_main
Definition: api_main.c:3
vl_api_registration_t ** vl_clients
vlib/vpp only: vector of client registrations
Definition: api_common.h:296
description fragment has unexpected format
Definition: map.api:433
static u32 vl_msg_api_handle_from_index_and_epoch(u32 index, u32 epoch)
Definition: memory_api.h:54
#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:99
svm_region_t * vlib_rp
Current binary api segment descriptor.
Definition: api_common.h:283
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:293
unsigned int u32
Definition: types.h:88
#define hash_create_string(elts, value_bytes)
Definition: hash.h:690
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1029
vl_registration_type_t registration_type
type
Definition: api_common.h:49
Definition: cJSON.c:84
#define hash_get(h, key)
Definition: hash.h:249
void vl_msg_pop_heap(void *oldheap)
Definition: cli.c:732
svm_queue_t * vl_input_queue
shared memory only: pointer to client input queue
Definition: api_common.h:62
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
clib_error_t * vat_builtin_main_init(vlib_main_t *vm)
Definition: vlib_api.c:253
int cJSON_bool fmt
Definition: cJSON.h:160
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:227
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
uword unformat_sw_if_index(unformat_input_t *input, va_list *args)
Definition: api_main.c:295
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
Shared memory connection.
Definition: api_common.h:39
svmdb_client_t * c
static vlib_process_t * vlib_get_current_process(vlib_main_t *vm)
Definition: node_funcs.h:444
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
vl_api_msg_range_t * msg_ranges
vector of message ranges
Definition: api_common.h:308
#define clib_warning(format, args...)
Definition: error.h:59
void vat_suspend(vlib_main_t *vm, f64 interval)
Definition: api_main.c:6
svm_queue_t * vl_input_queue
Definition: memory_shared.h:84
#define foreach_vnet_api_error
Definition: api_errno.h:22
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
signed int i32
Definition: types.h:77
#define hash_create(elts, value_bytes)
Definition: hash.h:696
void vat_plugin_hash_create(void)
Definition: api_main.c:87
u16 interval
Definition: vrrp.api:34
static void init_error_string_table(vat_main_t *vam)
Definition: api_main.c:29
static void * clib_mem_alloc(uword size)
Definition: mem.h:253
u16 first_msg_id
first assigned message ID
Definition: api_common.h:116
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
int vat_plugin_init(vat_main_t *vam)
Definition: plugin.c:197
u32 vl_api_registration_pool_index
Index in VLIB&#39;s brain (not shared memory).
Definition: api_common.h:52
#define hash_get_mem(h, key)
Definition: hash.h:269
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:389
void api_cli_output(void *notused, const char *fmt,...)
Definition: api_main.c:254
u16 vl_client_get_first_plugin_msg_id(const char *plugin_name)
Definition: api_main.c:279
void * vl_msg_push_heap(void)
Definition: cli.c:725
vlib_cli_output_function_t * output_function
Definition: node.h:608
uword * msg_range_by_name
Message range by name hash.
Definition: api_common.h:305
svm_region_t * vlib_rp
Definition: api_common.h:63
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978