FD.io VPP  v19.08.3-2-gbabecb413
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);
52  vec_validate (vam->cmd_reply, 0);
53  vec_reset_length (vam->cmd_reply);
55  rv = vat_plugin_init (vam);
56  if (rv)
57  clib_warning ("vat_plugin_init returned %d", rv);
58 
59  return 0;
60 }
61 
63 
64 void
66 {
67  vat_main_t *vam = &vat_main;
68 
69  vam->sw_if_index_by_interface_name = hash_create_string (0, sizeof (uword));
70  vam->function_by_name = hash_create_string (0, sizeof (uword));
71  vam->help_by_name = hash_create_string (0, sizeof (uword));
72 }
73 
74 static void
75 maybe_register_api_client (vat_main_t * vam)
76 {
77  vl_api_registration_t **regpp;
79  svm_region_t *svm;
80  void *oldheap;
81  api_main_t *am = &api_main;
82 
83  if (vam->my_client_index != ~0)
84  return;
85 
86  pool_get (am->vl_clients, regpp);
87 
88  svm = am->vlib_rp;
89 
90  pthread_mutex_lock (&svm->mutex);
91  oldheap = svm_push_data_heap (svm);
92  *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
93 
94  regp = *regpp;
95  clib_memset (regp, 0, sizeof (*regp));
97  regp->vl_api_registration_pool_index = regpp - am->vl_clients;
98  regp->vlib_rp = svm;
99  regp->shmem_hdr = am->shmem_hdr;
100 
101  /* Loopback connection */
103 
104  regp->name = format (0, "%s", "vpp-internal");
105  vec_add1 (regp->name, 0);
106 
107  pthread_mutex_unlock (&svm->mutex);
108  svm_pop_heap (oldheap);
109 
110  vam->my_client_index = vl_msg_api_handle_from_index_and_epoch
113 
114  vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
115  api_sw_interface_dump (vam);
116 }
117 
118 static clib_error_t *
120  unformat_input_t * input, vlib_cli_command_t * cmd)
121 {
122  vat_main_t *vam = &vat_main;
123  unformat_input_t _input;
124  uword c;
125  u8 *cmdp, *argsp, *this_cmd;
126  uword *p;
127  u32 arg_len;
128  int rv;
129  int (*fp) (vat_main_t *);
130 
132 
133  /* vec_validated in the init routine */
134  _vec_len (vam->inbuf) = 0;
135 
136  vam->input = &_input;
137 
138  while (((c = unformat_get_input (input)) != '\n') &&
139  (c != UNFORMAT_END_OF_INPUT))
140  vec_add1 (vam->inbuf, c);
141 
142  /* Null-terminate the command */
143  vec_add1 (vam->inbuf, 0);
144 
145  /* In case no args given */
146  vec_add1 (vam->inbuf, 0);
147 
148  /* Split input into cmd + args */
149  this_cmd = cmdp = vam->inbuf;
150 
151  /* Skip leading whitespace */
152  while (cmdp < (this_cmd + vec_len (this_cmd)))
153  {
154  if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
155  {
156  cmdp++;
157  }
158  else
159  break;
160  }
161 
162  argsp = cmdp;
163 
164  /* Advance past the command */
165  while (argsp < (this_cmd + vec_len (this_cmd)))
166  {
167  if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n' && *argsp != 0)
168  {
169  argsp++;
170  }
171  else
172  break;
173  }
174  /* NULL terminate the command */
175  *argsp++ = 0;
176 
177  /* No arguments? Ensure that argsp points to a proper (empty) string */
178  if (argsp == (this_cmd + vec_len (this_cmd) - 1))
179  argsp[0] = 0;
180  else
181  while (argsp < (this_cmd + vec_len (this_cmd)))
182  {
183  if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
184  {
185  argsp++;
186  }
187  else
188  break;
189  }
190 
191  /* Blank input line? */
192  if (*cmdp == 0)
193  return 0;
194 
195  p = hash_get_mem (vam->function_by_name, cmdp);
196  if (p == 0)
197  {
198  return clib_error_return (0, "'%s': function not found\n", cmdp);
199  }
200 
201  arg_len = strlen ((char *) argsp);
202 
203  unformat_init_string (vam->input, (char *) argsp, arg_len);
204  fp = (void *) p[0];
205 
206  rv = (*fp) (vam);
207 
208  if (rv < 0)
209  {
210  unformat_free (vam->input);
211  return clib_error_return (0,
212  "%s error: %U\n", cmdp,
213  format_api_error, vam, rv);
214 
215  }
216  if (vam->regenerate_interface_table)
217  {
218  vam->regenerate_interface_table = 0;
219  api_sw_interface_dump (vam);
220  }
221  unformat_free (vam->input);
222  return 0;
223 }
224 
225 /* *INDENT-OFF* */
226 VLIB_CLI_COMMAND (api_command, static) =
227 {
228  .path = "binary-api",
229  .short_help = "binary-api [help] <name> [<args>]",
230  .function = api_command_fn,
231  .is_mp_safe = 1,
232 };
233 /* *INDENT-ON* */
234 
235 void
236 api_cli_output (void *notused, const char *fmt, ...)
237 {
238  va_list va;
239  vat_main_t *vam = &vat_main;
240  vlib_main_t *vm = vam->vlib_main;
242  u8 *s;
243 
244  va_start (va, fmt);
245  s = va_format (0, fmt, &va);
246  va_end (va);
247 
248  /* Terminate with \n if not present. */
249  if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
250  vec_add1 (s, '\n');
251 
252  if ((!cp) || (!cp->output_function))
253  fformat (stdout, "%v", s);
254  else
255  cp->output_function (cp->output_function_arg, s, vec_len (s));
256 
257  vec_free (s);
258 }
259 
260 u16
261 vl_client_get_first_plugin_msg_id (const char *plugin_name)
262 {
263  api_main_t *am = &api_main;
264  vl_api_msg_range_t *rp;
265  uword *p;
266 
267  p = hash_get_mem (am->msg_range_by_name, plugin_name);
268  if (p == 0)
269  return ~0;
270 
271  rp = vec_elt_at_index (am->msg_ranges, p[0]);
272 
273  return (rp->first_msg_id);
274 }
275 
276 uword
277 unformat_sw_if_index (unformat_input_t * input, va_list * args)
278 {
279  void *vam_unused = va_arg (*args, void *);
280  (void) (vam_unused);
281  u32 *result = va_arg (*args, u32 *);
282  vnet_main_t *vnm = vnet_get_main ();
283  u32 sw_if_index = ~0;
284 
285  if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
286  {
287  *result = sw_if_index;
288  return 1;
289  }
290  return 0;
291 }
292 
293 /*
294  * fd.io coding-style-patch-verification: ON
295  *
296  * Local Variables:
297  * eval: (c-set-style "gnu")
298  * End:
299  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
Message range (belonging to a plugin)
Definition: api_common.h:112
uword output_function_arg
Definition: node.h:612
u8 * name
Client name.
Definition: api_common.h:53
#define hash_set(h, key, value)
Definition: hash.h:255
static void svm_pop_heap(void *oldheap)
Definition: svm.h:94
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define VLIB_MAIN_LOOP_ENTER_FUNCTION(x)
Definition: init.h:176
static uword unformat_get_input(unformat_input_t *input)
Definition: format.h:192
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:75
#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:424
u8 * va_format(u8 *s, const char *fmt, va_list *va)
Definition: format.c:387
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:236
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:119
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:422
vat_main_t vat_main
Definition: api_main.c:3
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
static void * svm_push_data_heap(svm_region_t *rp)
Definition: svm.h:86
vl_api_registration_t ** vl_clients
vlib/vpp only: vector of client registrations
Definition: api_common.h:269
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:256
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:266
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:48
#define hash_get(h, key)
Definition: hash.h:249
svm_queue_t * vl_input_queue
shared memory only: pointer to client input queue
Definition: api_common.h:61
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
word fformat(FILE *f, char *fmt,...)
Definition: format.c:462
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:203
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
uword unformat_sw_if_index(unformat_input_t *input, va_list *args)
Definition: api_main.c:277
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
Shared memory connection.
Definition: api_common.h:38
svmdb_client_t * c
static vlib_process_t * vlib_get_current_process(vlib_main_t *vm)
Definition: node_funcs.h:391
vlib_main_t * vm
Definition: buffer.c:323
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
vl_api_msg_range_t * msg_ranges
vector of message ranges
Definition: api_common.h:281
#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:161
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:65
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:153
u16 first_msg_id
first assigned message ID
Definition: api_common.h:115
#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:163
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:185
u32 vl_api_registration_pool_index
Index in VLIB&#39;s brain (not shared memory).
Definition: api_common.h:51
#define hash_get_mem(h, key)
Definition: hash.h:269
void api_cli_output(void *notused, const char *fmt,...)
Definition: api_main.c:236
u16 vl_client_get_first_plugin_msg_id(const char *plugin_name)
Definition: api_main.c:261
vlib_cli_output_function_t * output_function
Definition: node.h:611
uword * msg_range_by_name
Message range by name hash.
Definition: api_common.h:278
api_main_t api_main
Definition: api_shared.c:35
svm_region_t * vlib_rp
Definition: api_common.h:62
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
pthread_mutex_t mutex
Definition: svm_common.h:37