FD.io VPP  v21.06-3-gbb25fbf28
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 {
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;
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 */
121  regp->vl_input_queue = am->shmem_hdr->vl_input_queue;
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
130  am->shmem_hdr->application_restarts);
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') &&
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* */
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 {
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  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
unformat_sw_if_index
uword unformat_sw_if_index(unformat_input_t *input, va_list *args)
Definition: api_main.c:295
vlib_process_t::output_function
vlib_cli_output_function_t * output_function
Definition: node.h:594
f
vlib_frame_t * f
Definition: interface_output.c:1080
vl_msg_api_handle_from_index_and_epoch
static u32 vl_msg_api_handle_from_index_and_epoch(u32 index, u32 epoch)
Definition: memory_api.h:54
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
vl_api_registration_::registration_type
vl_registration_type_t registration_type
type
Definition: api_common.h:49
u16
unsigned short u16
Definition: types.h:57
am
app_main_t * am
Definition: application.c:489
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vl_api_registration_::vl_input_queue
svm_queue_t * vl_input_queue
shared memory only: pointer to client input queue
Definition: api_common.h:63
hash_create_string
#define hash_create_string(elts, value_bytes)
Definition: hash.h:689
va_format
__clib_export u8 * va_format(u8 *s, const char *fmt, va_list *va)
Definition: format.c:391
unformat_input_t
struct _unformat_input_t unformat_input_t
hash_create
#define hash_create(elts, value_bytes)
Definition: hash.h:695
vat_builtin_main_init
clib_error_t * vat_builtin_main_init(vlib_main_t *vm)
Definition: vlib_api.c:253
error
Definition: cJSON.c:88
vat_suspend
void vat_suspend(vlib_main_t *vm, f64 interval)
Definition: api_main.c:6
i32
signed int i32
Definition: types.h:77
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
hash_set
#define hash_set(h, key, value)
Definition: hash.h:255
vat_main
vat_main_t vat_main
Definition: api_main.c:3
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vat_plugin_hash_create
void vat_plugin_hash_create(void)
Definition: api_main.c:87
unformat_free
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
init_error_string_table
static void init_error_string_table(vat_main_t *vam)
Definition: api_main.c:29
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
api_command
static vlib_cli_command_t api_command
(constructor) VLIB_CLI_COMMAND (api_command)
Definition: api_main.c:244
format_api_error
static u8 * format_api_error(u8 *s, va_list *args)
Definition: api_main.c:12
c
svmdb_client_t * c
Definition: vpp_get_metrics.c:48
vat_plugin_init
int vat_plugin_init(vat_main_t *vam)
Definition: plugin.c:197
uword
u64 uword
Definition: types.h:112
hash_get
#define hash_get(h, key)
Definition: hash.h:249
vlibapi_get_main
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:390
f64
double f64
Definition: types.h:142
pool_get
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:255
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
api_cli_output
void api_cli_output(void *notused, const char *fmt,...)
Definition: api_main.c:254
unformat_get_input
static uword unformat_get_input(unformat_input_t *input)
Definition: format.h:184
interval
u16 interval
Definition: vrrp.api:34
fmt
int cJSON_bool fmt
Definition: cJSON.h:160
api_command_fn
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
api_main_t
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:228
vnet_main_t
Definition: vnet.h:76
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
vl_api_registration_::shmem_hdr
void * shmem_hdr
Definition: api_common.h:65
hash_get_mem
#define hash_get_mem(h, key)
Definition: hash.h:269
unformat_init_string
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1029
unformat_vnet_sw_interface
unformat_function_t unformat_vnet_sw_interface
Definition: interface_funcs.h:459
format
description fragment has unexpected format
Definition: map.api:433
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
vl_api_msg_range_t
Message range (belonging to a plugin)
Definition: api_common.h:114
u32
unsigned int u32
Definition: types.h:88
vl_msg_push_heap
void * vl_msg_push_heap(void)
Definition: cli.c:745
vlib_get_current_process
static vlib_process_t * vlib_get_current_process(vlib_main_t *vm)
Definition: node_funcs.h:448
vl_api_msg_range_t::first_msg_id
u16 first_msg_id
first assigned message ID
Definition: api_common.h:117
vl_api_registration_::vlib_rp
svm_region_t * vlib_rp
Definition: api_common.h:64
fformat
__clib_export word fformat(FILE *f, char *fmt,...)
Definition: format.c:466
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vl_client_get_first_plugin_msg_id
u16 vl_client_get_first_plugin_msg_id(const char *plugin_name)
Definition: api_main.c:279
maybe_register_api_client
static void maybe_register_api_client(vat_main_t *vam)
Definition: api_main.c:97
vlib_process_t
Definition: node.h:533
clib_warning
#define clib_warning(format, args...)
Definition: error.h:59
vlib_process_t::output_function_arg
uword output_function_arg
Definition: node.h:595
rv
int __clib_unused rv
Definition: application.c:491
foreach_vnet_api_error
#define foreach_vnet_api_error
Definition: api_errno.h:22
REGISTRATION_TYPE_SHMEM
@ REGISTRATION_TYPE_SHMEM
Shared memory connection.
Definition: api_common.h:39
vl_api_registration_::name
u8 * name
Client name.
Definition: api_common.h:54
vl_msg_pop_heap
void vl_msg_pop_heap(void *oldheap)
Definition: cli.c:752
vlib_cli_command_t
Definition: cli.h:92
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
clib_mem_alloc
static void * clib_mem_alloc(uword size)
Definition: mem.h:253
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
vl_api_registration_::vl_api_registration_pool_index
u32 vl_api_registration_pool_index
Index in VLIB's brain (not shared memory).
Definition: api_common.h:52