FD.io VPP  v18.07.1-19-g511ce25
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  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  api_main_t *am = &api_main;
131 
133 
134  /* vec_validated in the init routine */
135  _vec_len (vam->inbuf) = 0;
136 
137  vam->input = &_input;
138 
139  while (((c = unformat_get_input (input)) != '\n') &&
140  (c != UNFORMAT_END_OF_INPUT))
141  vec_add1 (vam->inbuf, c);
142 
143  /* Null-terminate the command */
144  vec_add1 (vam->inbuf, 0);
145 
146  /* In case no args given */
147  vec_add1 (vam->inbuf, 0);
148 
149  /* Split input into cmd + args */
150  this_cmd = cmdp = vam->inbuf;
151 
152  /* Skip leading whitespace */
153  while (cmdp < (this_cmd + vec_len (this_cmd)))
154  {
155  if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
156  {
157  cmdp++;
158  }
159  else
160  break;
161  }
162 
163  argsp = cmdp;
164 
165  /* Advance past the command */
166  while (argsp < (this_cmd + vec_len (this_cmd)))
167  {
168  if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n' && *argsp != 0)
169  {
170  argsp++;
171  }
172  else
173  break;
174  }
175  /* NULL terminate the command */
176  *argsp++ = 0;
177 
178  /* No arguments? Ensure that argsp points to a proper (empty) string */
179  if (argsp == (this_cmd + vec_len (this_cmd) - 1))
180  argsp[0] = 0;
181  else
182  while (argsp < (this_cmd + vec_len (this_cmd)))
183  {
184  if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
185  {
186  argsp++;
187  }
188  else
189  break;
190  }
191 
192  /* Blank input line? */
193  if (*cmdp == 0)
194  return 0;
195 
196  p = hash_get_mem (vam->function_by_name, cmdp);
197  if (p == 0)
198  {
199  return clib_error_return (0, "'%s': function not found\n", cmdp);
200  }
201 
202  arg_len = strlen ((char *) argsp);
203 
204  unformat_init_string (vam->input, (char *) argsp, arg_len);
205  fp = (void *) p[0];
206 
207  rv = (*fp) (vam);
208 
209  if (rv < 0)
210  {
211  unformat_free (vam->input);
212  return clib_error_return (0,
213  "%s error: %U\n", cmdp,
214  format_api_error, vam, rv);
215 
216  }
217  if (vam->regenerate_interface_table)
218  {
219  vam->regenerate_interface_table = 0;
220  api_sw_interface_dump (vam);
221  }
222  unformat_free (vam->input);
223  return 0;
224 }
225 
226 /* *INDENT-OFF* */
227 VLIB_CLI_COMMAND (api_command, static) =
228 {
229  .path = "binary-api",
230  .short_help = "binary-api [help] <name> [<args>]",
231  .function = api_command_fn,
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  u32 *result = va_arg (*args, u32 *);
281  vnet_main_t *vnm = vnet_get_main ();
282  u32 sw_if_index = ~0;
283 
284  if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
285  {
286  *result = sw_if_index;
287  return 1;
288  }
289  return 0;
290 }
291 
292 /*
293  * fd.io coding-style-patch-verification: ON
294  *
295  * Local Variables:
296  * eval: (c-set-style "gnu")
297  * End:
298  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:437
Message range (belonging to a plugin)
Definition: api_common.h:110
uword output_function_arg
Definition: node.h:589
u8 * name
Client name.
Definition: api_common.h:51
#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:47
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
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:523
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
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:228
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:448
vat_main_t vat_main
Definition: api_main.c:3
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:156
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:267
static u32 vl_msg_api_handle_from_index_and_epoch(u32 index, u32 epoch)
Definition: memory_api.h:52
#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:254
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:264
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:1023
vl_registration_type_t registration_type
type
Definition: api_common.h:46
#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:59
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
word fformat(FILE *f, char *fmt,...)
Definition: format.c:453
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:201
An API client registration, only in vpp/vlib.
Definition: api_common.h:44
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:144
Shared memory connection.
Definition: api_common.h:36
svmdb_client_t * c
static vlib_process_t * vlib_get_current_process(vlib_main_t *vm)
Definition: node_funcs.h:417
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
vl_api_msg_range_t * msg_ranges
vector of message ranges
Definition: api_common.h:279
#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:154
signed int i32
Definition: types.h:81
#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:112
u16 first_msg_id
first assigned message ID
Definition: api_common.h:113
#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
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:188
u32 vl_api_registration_pool_index
Index in VLIB&#39;s brain (not shared memory).
Definition: api_common.h:49
#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:588
uword * msg_range_by_name
Message range by name hash.
Definition: api_common.h:276
api_main_t api_main
Definition: api_shared.c:35
svm_region_t * vlib_rp
Definition: api_common.h:60
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
pthread_mutex_t mutex
Definition: svm_common.h:37