3 #include <vat/plugin.h>
8 vat_suspend (vlib_main_t * vm, f64 interval)
10 vlib_process_suspend (vm, interval);
14 format_api_error (u8 * s, va_list * args)
16 vat_main_t *vam = va_arg (*args, vat_main_t *);
17 i32 error = va_arg (*args, u32);
20 p = hash_get (vam->error_string_by_error_number, -error);
23 s = format (s, "%s", p[0]);
25 s = format (s, "%d", error);
31 init_error_string_table (vat_main_t * vam)
34 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
36 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
37 foreach_vnet_api_error;
40 hash_set (vam->error_string_by_error_number, 99, "Misc");
43 #if VPP_API_TEST_BUILTIN > 0
47 vat_registered_features_t *f;
48 vat_main_t *vam = &vat_main;
51 f = vam->feature_function_registrations;
55 error = f->function (vam);
58 clib_warning ("INIT FAILED");
65 vat_builtin_main_init (vlib_main_t * vm)
67 vat_main_t *vam = &vat_main;
69 int vat_plugin_init (vat_main_t * vam);
72 vam->my_client_index = (u32) ~ 0;
73 /* Ensure that vam->inbuf is never NULL */
74 vec_validate (vam->inbuf, 0);
75 vec_validate (vam->cmd_reply, 0);
76 vec_reset_length (vam->cmd_reply);
77 init_error_string_table (vam);
78 rv = vat_plugin_init (vam);
80 clib_warning ("vat_plugin_init returned %d", rv);
89 vat_plugin_hash_create (void)
91 vat_main_t *vam = &vat_main;
93 vam->sw_if_index_by_interface_name = hash_create_string (0, sizeof (uword));
94 vam->function_by_name = hash_create_string (0, sizeof (uword));
95 vam->help_by_name = hash_create_string (0, sizeof (uword));
99 vat_register_interface_dump (vat_main_t *vam)
104 vec_foreach (pi, vat_plugin_main.plugin_info)
106 handle = dlsym (pi->handle, "api_sw_interface_dump");
109 vam->api_sw_interface_dump = handle;
114 if (!vam->api_sw_interface_dump)
117 "sw_interface_dump not found in interface_test plugin!\n");
123 maybe_register_api_client (vat_main_t * vam)
125 vl_api_registration_t **regpp;
126 vl_api_registration_t *regp;
128 api_main_t *am = vlibapi_get_main ();
130 if (vam->my_client_index != ~0)
133 pool_get (am->vl_clients, regpp);
135 oldheap = vl_msg_push_heap ();
137 *regpp = clib_mem_alloc (sizeof (vl_api_registration_t));
140 clib_memset (regp, 0, sizeof (*regp));
141 regp->registration_type = REGISTRATION_TYPE_SHMEM;
142 regp->vl_api_registration_pool_index = regpp - am->vl_clients;
143 regp->vlib_rp = am->vlib_rp;
144 regp->shmem_hdr = am->shmem_hdr;
146 /* Loopback connection */
147 regp->vl_input_queue = am->shmem_hdr->vl_input_queue;
149 regp->name = format (0, "%s", "vpp-internal");
150 vec_add1 (regp->name, 0);
152 vl_msg_pop_heap (oldheap);
154 vam->my_client_index = vl_msg_api_handle_from_index_and_epoch
155 (regp->vl_api_registration_pool_index,
156 am->shmem_hdr->application_restarts);
158 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
159 vat_register_interface_dump (vam);
160 vam->api_sw_interface_dump (vam);
163 static clib_error_t *
164 api_command_fn (vlib_main_t * vm,
165 unformat_input_t * input, vlib_cli_command_t * cmd)
167 vat_main_t *vam = &vat_main;
168 unformat_input_t _input;
170 u8 *cmdp, *argsp, *this_cmd;
174 int (*fp) (vat_main_t *);
176 maybe_register_api_client (vam);
178 /* vec_validated in the init routine */
179 _vec_len (vam->inbuf) = 0;
181 vam->input = &_input;
183 while (((c = unformat_get_input (input)) != '\n') &&
184 (c != UNFORMAT_END_OF_INPUT))
185 vec_add1 (vam->inbuf, c);
187 /* Null-terminate the command */
188 vec_add1 (vam->inbuf, 0);
190 /* In case no args given */
191 vec_add1 (vam->inbuf, 0);
193 /* Split input into cmd + args */
194 this_cmd = cmdp = vam->inbuf;
196 /* Skip leading whitespace */
197 while (cmdp < (this_cmd + vec_len (this_cmd)))
199 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
209 /* Advance past the command */
210 while (argsp < (this_cmd + vec_len (this_cmd)))
212 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n' && *argsp != 0)
219 /* NULL terminate the command */
222 /* No arguments? Ensure that argsp points to a proper (empty) string */
223 if (argsp == (this_cmd + vec_len (this_cmd) - 1))
226 while (argsp < (this_cmd + vec_len (this_cmd)))
228 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
236 /* Blank input line? */
240 p = hash_get_mem (vam->function_by_name, cmdp);
243 return clib_error_return (0, "'%s': function not found\n", cmdp);
246 arg_len = strlen ((char *) argsp);
248 unformat_init_string (vam->input, (char *) argsp, arg_len);
255 unformat_free (vam->input);
256 return clib_error_return (0,
257 "%s error: %U\n", cmdp,
258 format_api_error, vam, rv);
261 if (vam->regenerate_interface_table)
263 vam->regenerate_interface_table = 0;
264 vam->api_sw_interface_dump (vam);
266 unformat_free (vam->input);
271 VLIB_CLI_COMMAND (api_command, static) =
273 .path = "binary-api",
274 .short_help = "binary-api [help] <name> [<args>]",
275 .function = api_command_fn,
281 api_cli_output (void *notused, const char *fmt, ...)
284 vat_main_t *vam = &vat_main;
285 vlib_main_t *vm = vam->vlib_main;
286 vlib_process_t *cp = vlib_get_current_process (vm);
290 s = va_format (0, fmt, &va);
293 /* Terminate with \n if not present. */
294 if (vec_len (s) > 0 && s[vec_len (s) - 1] != '\n')
297 if ((!cp) || (!cp->output_function))
298 fformat (stdout, "%v", s);
300 cp->output_function (cp->output_function_arg, s, vec_len (s));
306 vl_client_get_first_plugin_msg_id (const char *plugin_name)
308 api_main_t *am = vlibapi_get_main ();
309 vl_api_msg_range_t *rp;
312 p = hash_get_mem (am->msg_range_by_name, plugin_name);
316 rp = vec_elt_at_index (am->msg_ranges, p[0]);
318 return (rp->first_msg_id);
322 unformat_sw_if_index (unformat_input_t * input, va_list * args)
324 void *vam_unused = va_arg (*args, void *);
326 u32 *result = va_arg (*args, u32 *);
327 vnet_main_t *vnm = vnet_get_main ();
328 u32 sw_if_index = ~0;
330 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
332 *result = sw_if_index;
339 * fd.io coding-style-patch-verification: ON
342 * eval: (c-set-style "gnu")