2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
22 #include <vlibapi/api_helper_macros.h>
25 vat_suspend (vlib_main_t * vm, f64 interval)
27 /* do nothing in the standalone version, just return */
31 connect_to_vpe (char *name)
33 vat_main_t *vam = &vat_main;
34 api_main_t *am = vlibapi_get_main ();
36 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
39 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
40 vam->my_client_index = am->my_client_index;
45 vlib_main_t vlib_global_main;
46 vlib_main_t **vlib_mains;
48 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
55 format_api_error (u8 * s, va_list * args)
57 vat_main_t *vam = va_arg (*args, vat_main_t *);
58 i32 error = va_arg (*args, u32);
61 p = hash_get (vam->error_string_by_error_number, -error);
64 s = format (s, "%s", p[0]);
66 s = format (s, "%d", error);
71 do_one_file (vat_main_t * vam)
74 int (*fp) (vat_main_t * vam);
76 unformat_input_t _input;
83 /* Used by the "quit" command handler */
84 if (setjmp (vam->jump_buf) != 0)
87 vam->jump_buf_set = 1;
91 if (vam->ifp == stdin)
93 if (vam->exec_mode == 0)
94 rv = write (1, "vat# ", 5);
96 rv = write (1, "exec# ", 6);
99 _vec_len (vam->inbuf) = 4096;
102 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
105 vam->input_line_number++;
110 (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf,
115 if (vam->exec_mode == 0)
117 /* Split input into cmd + args */
120 while (cmdp < (this_cmd + vec_len (this_cmd)))
122 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
130 while (argsp < (this_cmd + vec_len (this_cmd)))
132 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
140 while (argsp < (this_cmd + vec_len (this_cmd)))
142 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
151 /* Blank input line? */
155 p = hash_get_mem (vam->function_by_name, cmdp);
158 errmsg ("'%s': function not found\n", cmdp);
162 arg_len = strlen ((char *) argsp);
164 unformat_init_string (vam->input, (char *) argsp, arg_len);
169 unformat_init_string (vam->input, (char *) this_cmd,
170 strlen ((char *) this_cmd));
177 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
178 unformat_free (vam->input);
180 if (vam->regenerate_interface_table)
182 vam->regenerate_interface_table = 0;
183 api_sw_interface_dump (vam);
186 /* Hack to pick up new client index after memfd_segment_create pivot */
187 if (vam->client_index_invalid)
189 vat_main_t *vam = &vat_main;
190 api_main_t *am = vlibapi_get_main ();
192 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
193 vam->my_client_index = am->my_client_index;
194 vam->client_index_invalid = 0;
200 init_error_string_table (vat_main_t * vam)
203 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
205 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
206 foreach_vnet_api_error;
209 hash_set (vam->error_string_by_error_number, 99, "Misc");
213 eval_current_file (clib_macro_main_t * mm, i32 complain)
215 vat_main_t *vam = &vat_main;
216 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
220 eval_current_line (clib_macro_main_t * mm, i32 complain)
222 vat_main_t *vam = &vat_main;
223 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
227 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
229 vat_main_t *vam = &vat_main;
233 /* these (caught) signals cause the application to exit */
236 if (vam->jump_buf_set)
242 /* FALLTHROUGH on purpose */
252 setup_signal_handlers (void)
257 for (i = 1; i < 32; i++)
259 clib_memset (&sa, 0, sizeof (sa));
260 sa.sa_sigaction = (void *) signal_handler;
261 sa.sa_flags = SA_SIGINFO;
265 /* these signals take the default action */
274 /* ignore SIGPIPE, SIGCHLD */
278 sa.sa_sigaction = (void *) SIG_IGN;
281 /* catch and handle all other signals */
286 if (sigaction (i, &sa, 0) < 0)
287 clib_unix_warning ("sigaction %U", format_signal, i);
292 vat_find_plugin_path ()
294 char *p, path[PATH_MAX];
298 /* find executable path */
299 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
302 /* readlink doesn't provide null termination */
306 if ((p = strrchr (path, '/')) == 0)
311 if ((p = strrchr (path, '/')) == 0)
315 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
316 "%s/lib/vpp_api_test_plugins", path, path);
318 vat_plugin_path = (char *) s;
322 main (int argc, char **argv)
324 vat_main_t *vam = &vat_main;
325 unformat_input_t _argv, *a = &_argv;
326 u8 **input_files = 0;
335 clib_mem_init_thread_safe (0, 128 << 20);
337 clib_macro_init (&vam->macro_main);
338 clib_macro_add_builtin (&vam->macro_main, "current_file",
340 clib_macro_add_builtin (&vam->macro_main, "current_line",
343 init_error_string_table (vam);
344 vec_validate (vam->cmd_reply, 0);
345 vec_reset_length (vam->cmd_reply);
347 vat_find_plugin_path ();
349 unformat_init_command_line (a, argv);
351 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
353 if (unformat (a, "in %s", &this_input_file))
354 vec_add1 (input_files, this_input_file);
355 else if (unformat (a, "out %s", &output_file))
357 else if (unformat (a, "script"))
359 else if (unformat (a, "json"))
361 else if (unformat (a, "socket-name %s", &vam->socket_name))
363 else if (unformat (a, "default-socket"))
365 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
367 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
368 vec_add1 (vat_plugin_path, 0);
369 else if (unformat (a, "plugin_name_filter %s",
370 (u8 *) & vat_plugin_name_filter))
371 vec_add1 (vat_plugin_name_filter, 0);
372 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
374 vl_set_memory_root_path ((char *) chroot_prefix);
380 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
381 "[plugin_path <path>][default-socket][socket-name <name>]\n"
382 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
389 vam->ofp = fopen ((char *) output_file, "w");
393 if (vam->ofp == NULL)
395 fformat (stderr, "Couldn't open output file %s\n",
396 output_file ? (char *) output_file : "stdout");
400 clib_time_init (&vam->clib_time);
402 vat_api_hookup (vam);
403 vat_plugin_api_reference ();
405 setup_signal_handlers ();
407 if (vam->socket_name && vat_socket_connect (vam))
408 fformat (stderr, "WARNING: socket connection failed");
410 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
411 && connect_to_vpe ("vpp_api_test") < 0)
414 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
418 vam->json_output = json_output;
421 api_sw_interface_dump (vam);
423 vec_validate (vam->inbuf, 4096);
425 vam->current_file = (u8 *) "plugin-init";
426 vat_plugin_init (vam);
428 for (i = 0; i < vec_len (input_files); i++)
430 vam->ifp = fopen ((char *) input_files[i], "r");
431 if (vam->ifp == NULL)
433 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
436 vam->current_file = input_files[i];
437 vam->input_line_number = 0;
449 vam->current_file = (u8 *) "interactive";
455 * Particularly when running a script, don't be in a hurry to leave.
456 * A reply message queued to this process will end up constipating
457 * the allocation rings.
459 timeout = vat_time_now (vam) + 2.0;
460 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
463 if (vat_time_now (vam) > timeout)
464 clib_warning ("BUG: message reply spin-wait timeout");
466 vl_client_disconnect_from_vlib ();
471 * fd.io coding-style-patch-verification: ON
474 * eval: (c-set-style "gnu")