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 */
272 /* ignore SIGPIPE, SIGCHLD */
276 sa.sa_sigaction = (void *) SIG_IGN;
279 /* catch and handle all other signals */
284 if (sigaction (i, &sa, 0) < 0)
285 clib_unix_warning ("sigaction %U", format_signal, i);
290 vat_find_plugin_path ()
292 char *p, path[PATH_MAX];
296 /* find executable path */
297 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
300 /* readlink doesn't provide null termination */
304 if ((p = strrchr (path, '/')) == 0)
309 if ((p = strrchr (path, '/')) == 0)
313 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
314 "%s/lib/vpp_api_test_plugins", path, path);
316 vat_plugin_path = (char *) s;
320 main (int argc, char **argv)
322 vat_main_t *vam = &vat_main;
323 unformat_input_t _argv, *a = &_argv;
324 u8 **input_files = 0;
333 clib_mem_init_thread_safe (0, 128 << 20);
335 clib_macro_init (&vam->macro_main);
336 clib_macro_add_builtin (&vam->macro_main, "current_file",
338 clib_macro_add_builtin (&vam->macro_main, "current_line",
341 init_error_string_table (vam);
342 vec_validate (vam->cmd_reply, 0);
343 vec_reset_length (vam->cmd_reply);
345 vat_find_plugin_path ();
347 unformat_init_command_line (a, argv);
349 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
351 if (unformat (a, "in %s", &this_input_file))
352 vec_add1 (input_files, this_input_file);
353 else if (unformat (a, "out %s", &output_file))
355 else if (unformat (a, "script"))
357 else if (unformat (a, "json"))
359 else if (unformat (a, "socket-name %s", &vam->socket_name))
361 else if (unformat (a, "default-socket"))
363 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
365 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
366 vec_add1 (vat_plugin_path, 0);
367 else if (unformat (a, "plugin_name_filter %s",
368 (u8 *) & vat_plugin_name_filter))
369 vec_add1 (vat_plugin_name_filter, 0);
370 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
372 vl_set_memory_root_path ((char *) chroot_prefix);
378 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
379 "[plugin_path <path>][default-socket][socket-name <name>]\n"
380 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
387 vam->ofp = fopen ((char *) output_file, "w");
391 if (vam->ofp == NULL)
393 fformat (stderr, "Couldn't open output file %s\n",
394 output_file ? (char *) output_file : "stdout");
398 clib_time_init (&vam->clib_time);
400 vat_api_hookup (vam);
401 vat_plugin_api_reference ();
403 setup_signal_handlers ();
405 if (vam->socket_name && vat_socket_connect (vam))
406 fformat (stderr, "WARNING: socket connection failed");
408 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
409 && connect_to_vpe ("vpp_api_test") < 0)
412 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
416 vam->json_output = json_output;
419 api_sw_interface_dump (vam);
421 vec_validate (vam->inbuf, 4096);
423 vam->current_file = (u8 *) "plugin-init";
424 vat_plugin_init (vam);
426 for (i = 0; i < vec_len (input_files); i++)
428 vam->ifp = fopen ((char *) input_files[i], "r");
429 if (vam->ifp == NULL)
431 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
434 vam->current_file = input_files[i];
435 vam->input_line_number = 0;
447 vam->current_file = (u8 *) "interactive";
453 * Particularly when running a script, don't be in a hurry to leave.
454 * A reply message queued to this process will end up constipating
455 * the allocation rings.
457 timeout = vat_time_now (vam) + 2.0;
458 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
461 if (vat_time_now (vam) > timeout)
462 clib_warning ("BUG: message reply spin-wait timeout");
464 vl_client_disconnect_from_vlib ();
469 * fd.io coding-style-patch-verification: ON
472 * eval: (c-set-style "gnu")