6 vat_suspend (vlib_main_t * vm, f64 interval)
8 vlib_process_suspend (vm, interval);
12 format_api_error (u8 * s, va_list * args)
14 vat_main_t *vam = va_arg (*args, vat_main_t *);
15 i32 error = va_arg (*args, u32);
18 p = hash_get (vam->error_string_by_error_number, -error);
21 s = format (s, "%s", p[0]);
23 s = format (s, "%d", error);
29 init_error_string_table (vat_main_t * vam)
32 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
34 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
35 foreach_vnet_api_error;
38 hash_set (vam->error_string_by_error_number, 99, "Misc");
42 api_main_init (vlib_main_t * vm)
44 vat_main_t *vam = &vat_main;
46 int vat_plugin_init (vat_main_t * vam);
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);
54 init_error_string_table (vam);
55 rv = vat_plugin_init (vam);
57 clib_warning ("vat_plugin_init returned %d", rv);
62 VLIB_INIT_FUNCTION (api_main_init);
65 vat_plugin_hash_create (void)
67 vat_main_t *vam = &vat_main;
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));
75 maybe_register_api_client (vat_main_t * vam)
77 vl_api_registration_t **regpp;
78 vl_api_registration_t *regp;
81 api_main_t *am = &api_main;
83 if (vam->my_client_index != ~0)
86 pool_get (am->vl_clients, regpp);
90 pthread_mutex_lock (&svm->mutex);
91 oldheap = svm_push_data_heap (svm);
92 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
95 memset (regp, 0, sizeof (*regp));
96 regp->registration_type = REGISTRATION_TYPE_SHMEM;
97 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
99 regp->shmem_hdr = am->shmem_hdr;
101 /* Loopback connection */
102 regp->vl_input_queue = am->shmem_hdr->vl_input_queue;
104 regp->name = format (0, "%s", "vpp-internal");
105 vec_add1 (regp->name, 0);
107 pthread_mutex_unlock (&svm->mutex);
108 svm_pop_heap (oldheap);
110 vam->my_client_index = vl_msg_api_handle_from_index_and_epoch
111 (regp->vl_api_registration_pool_index,
112 am->shmem_hdr->application_restarts);
114 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
115 api_sw_interface_dump (vam);
118 static clib_error_t *
119 api_command_fn (vlib_main_t * vm,
120 unformat_input_t * input, vlib_cli_command_t * cmd)
122 vat_main_t *vam = &vat_main;
123 unformat_input_t _input;
125 u8 *cmdp, *argsp, *this_cmd;
129 int (*fp) (vat_main_t *);
130 api_main_t *am = &api_main;
132 maybe_register_api_client (vam);
134 /* vec_validated in the init routine */
135 _vec_len (vam->inbuf) = 0;
137 vam->input = &_input;
139 while (((c = unformat_get_input (input)) != '\n') &&
140 (c != UNFORMAT_END_OF_INPUT))
141 vec_add1 (vam->inbuf, c);
143 /* Null-terminate the command */
144 vec_add1 (vam->inbuf, 0);
146 /* In case no args given */
147 vec_add1 (vam->inbuf, 0);
149 /* Split input into cmd + args */
150 this_cmd = cmdp = vam->inbuf;
152 /* Skip leading whitespace */
153 while (cmdp < (this_cmd + vec_len (this_cmd)))
155 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
165 /* Advance past the command */
166 while (argsp < (this_cmd + vec_len (this_cmd)))
168 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n' && *argsp != 0)
175 /* NULL terminate the command */
178 /* No arguments? Ensure that argsp points to a proper (empty) string */
179 if (argsp == (this_cmd + vec_len (this_cmd) - 1))
182 while (argsp < (this_cmd + vec_len (this_cmd)))
184 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
192 /* Blank input line? */
196 p = hash_get_mem (vam->function_by_name, cmdp);
199 return clib_error_return (0, "'%s': function not found\n", cmdp);
202 arg_len = strlen ((char *) argsp);
204 unformat_init_string (vam->input, (char *) argsp, arg_len);
211 unformat_free (vam->input);
212 return clib_error_return (0,
213 "%s error: %U\n", cmdp,
214 format_api_error, vam, rv);
217 if (vam->regenerate_interface_table)
219 vam->regenerate_interface_table = 0;
220 api_sw_interface_dump (vam);
222 unformat_free (vam->input);
227 VLIB_CLI_COMMAND (api_command, static) =
229 .path = "binary-api",
230 .short_help = "binary-api [help] <name> [<args>]",
231 .function = api_command_fn,
237 api_cli_output (void *notused, const char *fmt, ...)
240 vat_main_t *vam = &vat_main;
241 vlib_main_t *vm = vam->vlib_main;
242 vlib_process_t *cp = vlib_get_current_process (vm);
246 s = va_format (0, fmt, &va);
249 /* Terminate with \n if not present. */
250 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
253 if ((!cp) || (!cp->output_function))
254 fformat (stdout, "%v", s);
256 cp->output_function (cp->output_function_arg, s, vec_len (s));
262 vl_client_get_first_plugin_msg_id (const char *plugin_name)
264 api_main_t *am = &api_main;
265 vl_api_msg_range_t *rp;
268 p = hash_get_mem (am->msg_range_by_name, plugin_name);
272 rp = vec_elt_at_index (am->msg_ranges, p[0]);
274 return (rp->first_msg_id);
278 unformat_sw_if_index (unformat_input_t * input, va_list * args)
280 void *vam_unused = va_arg (*args, void *);
281 u32 *result = va_arg (*args, u32 *);
282 vnet_main_t *vnm = vnet_get_main ();
283 u32 sw_if_index = ~0;
285 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
287 *result = sw_if_index;
294 * fd.io coding-style-patch-verification: ON
297 * eval: (c-set-style "gnu")