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,
113 if (vam->exec_mode == 0)
115 /* Split input into cmd + args */
118 while (cmdp < (this_cmd + vec_len (this_cmd)))
120 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
128 while (argsp < (this_cmd + vec_len (this_cmd)))
130 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
138 while (argsp < (this_cmd + vec_len (this_cmd)))
140 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
149 /* Blank input line? */
153 p = hash_get_mem (vam->function_by_name, cmdp);
156 errmsg ("'%s': function not found\n", cmdp);
160 arg_len = strlen ((char *) argsp);
162 unformat_init_string (vam->input, (char *) argsp, arg_len);
167 unformat_init_string (vam->input, (char *) this_cmd,
168 strlen ((char *) this_cmd));
175 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
176 unformat_free (vam->input);
178 if (vam->regenerate_interface_table)
180 vam->regenerate_interface_table = 0;
181 api_sw_interface_dump (vam);
184 /* Hack to pick up new client index after memfd_segment_create pivot */
185 if (vam->client_index_invalid)
187 vat_main_t *vam = &vat_main;
188 api_main_t *am = vlibapi_get_main ();
190 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
191 vam->my_client_index = am->my_client_index;
192 vam->client_index_invalid = 0;
198 init_error_string_table (vat_main_t * vam)
201 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
203 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
204 foreach_vnet_api_error;
207 hash_set (vam->error_string_by_error_number, 99, "Misc");
211 eval_current_file (macro_main_t * mm, i32 complain)
213 vat_main_t *vam = &vat_main;
214 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
218 eval_current_line (macro_main_t * mm, i32 complain)
220 vat_main_t *vam = &vat_main;
221 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
225 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
227 vat_main_t *vam = &vat_main;
231 /* these (caught) signals cause the application to exit */
234 if (vam->jump_buf_set)
240 /* FALLTHROUGH on purpose */
250 setup_signal_handlers (void)
255 for (i = 1; i < 32; i++)
257 clib_memset (&sa, 0, sizeof (sa));
258 sa.sa_sigaction = (void *) signal_handler;
259 sa.sa_flags = SA_SIGINFO;
263 /* these signals take the default action */
271 /* ignore SIGPIPE, SIGCHLD */
275 sa.sa_sigaction = (void *) SIG_IGN;
278 /* catch and handle all other signals */
283 if (sigaction (i, &sa, 0) < 0)
284 clib_unix_warning ("sigaction %U", format_signal, i);
289 vat_find_plugin_path ()
291 char *p, path[PATH_MAX];
295 /* find executable path */
296 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
299 /* readlink doesn't provide null termination */
303 if ((p = strrchr (path, '/')) == 0)
308 if ((p = strrchr (path, '/')) == 0)
312 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
313 "%s/lib/vpp_api_test_plugins", path, path);
315 vat_plugin_path = (char *) s;
319 main (int argc, char **argv)
321 vat_main_t *vam = &vat_main;
322 unformat_input_t _argv, *a = &_argv;
323 u8 **input_files = 0;
332 clib_mem_init_thread_safe (0, 128 << 20);
334 clib_macro_init (&vam->macro_main);
335 clib_macro_add_builtin (&vam->macro_main, "current_file",
337 clib_macro_add_builtin (&vam->macro_main, "current_line",
340 init_error_string_table (vam);
341 vec_validate (vam->cmd_reply, 0);
342 vec_reset_length (vam->cmd_reply);
344 vat_find_plugin_path ();
346 unformat_init_command_line (a, argv);
348 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
350 if (unformat (a, "in %s", &this_input_file))
351 vec_add1 (input_files, this_input_file);
352 else if (unformat (a, "out %s", &output_file))
354 else if (unformat (a, "script"))
356 else if (unformat (a, "json"))
358 else if (unformat (a, "socket-name %s", &vam->socket_name))
360 else if (unformat (a, "default-socket"))
362 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
364 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
365 vec_add1 (vat_plugin_path, 0);
366 else if (unformat (a, "plugin_name_filter %s",
367 (u8 *) & vat_plugin_name_filter))
368 vec_add1 (vat_plugin_name_filter, 0);
369 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
371 vl_set_memory_root_path ((char *) chroot_prefix);
377 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
378 "[plugin_path <path>][default-socket][socket-name <name>]\n"
379 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
386 vam->ofp = fopen ((char *) output_file, "w");
390 if (vam->ofp == NULL)
392 fformat (stderr, "Couldn't open output file %s\n",
393 output_file ? (char *) output_file : "stdout");
397 clib_time_init (&vam->clib_time);
399 vat_api_hookup (vam);
400 vat_plugin_api_reference ();
402 setup_signal_handlers ();
404 if (vam->socket_name && vat_socket_connect (vam))
405 fformat (stderr, "WARNING: socket connection failed");
407 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
408 && connect_to_vpe ("vpp_api_test") < 0)
411 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
415 vam->json_output = json_output;
418 api_sw_interface_dump (vam);
420 vec_validate (vam->inbuf, 4096);
422 vam->current_file = (u8 *) "plugin-init";
423 vat_plugin_init (vam);
425 for (i = 0; i < vec_len (input_files); i++)
427 vam->ifp = fopen ((char *) input_files[i], "r");
428 if (vam->ifp == NULL)
430 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
433 vam->current_file = input_files[i];
434 vam->input_line_number = 0;
446 vam->current_file = (u8 *) "interactive";
452 * Particularly when running a script, don't be in a hurry to leave.
453 * A reply message queued to this process will end up constipating
454 * the allocation rings.
456 timeout = vat_time_now (vam) + 2.0;
457 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
460 if (vat_time_now (vam) > timeout)
461 clib_warning ("BUG: message reply spin-wait timeout");
463 vl_client_disconnect_from_vlib ();
468 * fd.io coding-style-patch-verification: ON
471 * eval: (c-set-style "gnu")