FD.io VPP  v18.01.1-37-g7ea3975
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 
115 static clib_error_t *
117  unformat_input_t * input, vlib_cli_command_t * cmd)
118 {
119  vat_main_t *vam = &vat_main;
120  unformat_input_t _input;
121  uword c;
122  u8 *cmdp, *argsp, *this_cmd;
123  uword *p;
124  u32 arg_len;
125  int rv;
126  int (*fp) (vat_main_t *);
127  api_main_t *am = &api_main;
128 
130 
131  vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
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 };
232 /* *INDENT-ON* */
233 
234 void
235 api_cli_output (void *notused, const char *fmt, ...)
236 {
237  va_list va;
238  vat_main_t *vam = &vat_main;
239  vlib_main_t *vm = vam->vlib_main;
241  u8 *s;
242 
243  va_start (va, fmt);
244  s = va_format (0, fmt, &va);
245  va_end (va);
246 
247  /* Terminate with \n if not present. */
248  if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
249  vec_add1 (s, '\n');
250 
251  if ((!cp) || (!cp->output_function))
252  fformat (stdout, "%v", s);
253  else
254  cp->output_function (cp->output_function_arg, s, vec_len (s));
255 
256  vec_free (s);
257 }
258 
259 u16
260 vl_client_get_first_plugin_msg_id (const char *plugin_name)
261 {
262  api_main_t *am = &api_main;
263  vl_api_msg_range_t *rp;
264  uword *p;
265 
266  p = hash_get_mem (am->msg_range_by_name, plugin_name);
267  if (p == 0)
268  return ~0;
269 
270  rp = vec_elt_at_index (am->msg_ranges, p[0]);
271 
272  return (rp->first_msg_id);
273 }
274 
275 uword
276 unformat_sw_if_index (unformat_input_t * input, va_list * args)
277 {
278  u32 *result = va_arg (*args, u32 *);
279  vnet_main_t *vnm = vnet_get_main ();
280  u32 sw_if_index = ~0;
281 
282  if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
283  {
284  *result = sw_if_index;
285  return 1;
286  }
287  return 0;
288 }
289 
290 /*
291  * fd.io coding-style-patch-verification: ON
292  *
293  * Local Variables:
294  * eval: (c-set-style "gnu")
295  * End:
296  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:432
Message range (belonging to a plugin)
Definition: api_common.h:109
uword output_function_arg
Definition: node.h:553
u8 * name
Client name.
Definition: api_common.h:51
#define hash_set(h, key, value)
Definition: hash.h:254
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:190
u32 application_restarts
Definition: api_common.h:79
unix_shared_memory_queue_t * vl_input_queue
Definition: api_common.h:68
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:518
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:225
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
static clib_error_t * api_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: api_main.c:116
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:111
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:264
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:99
svm_region_t * vlib_rp
Current binary api segment descriptor.
Definition: api_common.h:251
struct vl_shmem_hdr_ * shmem_hdr
Binary API shared-memory segment header pointer.
Definition: api_common.h:261
#define hash_create_string(elts, value_bytes)
Definition: hash.h:675
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:248
struct _unformat_input_t unformat_input_t
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:198
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:276
api_main_t api_main
Definition: api_shared.c:35
#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:417
vlib_main_t * vm
Definition: buffer.c:283
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
unix_shared_memory_queue_t * vl_input_queue
shared memory only: pointer to client input queue
Definition: api_common.h:59
vl_api_msg_range_t * msg_ranges
vector of message ranges
Definition: api_common.h:276
#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:681
unsigned int u32
Definition: types.h:88
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
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
u16 first_msg_id
first assigned message ID
Definition: api_common.h:112
#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
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:268
void api_cli_output(void *notused, const char *fmt,...)
Definition: api_main.c:235
u16 vl_client_get_first_plugin_msg_id(const char *plugin_name)
Definition: api_main.c:260
vlib_cli_output_function_t * output_function
Definition: node.h:552
uword * msg_range_by_name
Message range by name hash.
Definition: api_common.h:273
Shared memory connection.
Definition: api_common.h:36
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
static u32 vl_msg_api_handle_from_index_and_epoch(u32 index, u32 epoch)
Definition: api.h:43