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;
48 vlib_main_t vlib_global_main;
54 } __attribute__ ((packed)) __bootstrap_vlib_main_vector
55 __attribute__ ((aligned (CLIB_CACHE_LINE_BYTES))) =
58 .vm = &vlib_global_main,
62 vlib_main_t **vlib_mains = &__bootstrap_vlib_main_vector.vm;
65 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
71 format_api_error (u8 * s, va_list * args)
73 vat_main_t *vam = va_arg (*args, vat_main_t *);
74 i32 error = va_arg (*args, u32);
77 p = hash_get (vam->error_string_by_error_number, -error);
80 s = format (s, "%s", p[0]);
82 s = format (s, "%d", error);
87 do_one_file (vat_main_t * vam)
90 int (*fp) (vat_main_t * vam);
92 unformat_input_t _input;
99 /* Used by the "quit" command handler */
100 if (setjmp (vam->jump_buf) != 0)
103 vam->jump_buf_set = 1;
107 if (vam->ifp == stdin)
109 if (vam->exec_mode == 0)
110 rv = write (1, "vat# ", 5);
112 rv = write (1, "exec# ", 6);
115 _vec_len (vam->inbuf) = 4096;
118 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
121 vam->input_line_number++;
126 (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf,
131 if (vam->exec_mode == 0)
133 /* Split input into cmd + args */
136 while (cmdp < (this_cmd + vec_len (this_cmd)))
138 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
146 while (argsp < (this_cmd + vec_len (this_cmd)))
148 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
156 while (argsp < (this_cmd + vec_len (this_cmd)))
158 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
167 /* Blank input line? */
171 p = hash_get_mem (vam->function_by_name, cmdp);
174 errmsg ("'%s': function not found\n", cmdp);
178 arg_len = strlen ((char *) argsp);
180 unformat_init_string (vam->input, (char *) argsp, arg_len);
185 unformat_init_string (vam->input, (char *) this_cmd,
186 strlen ((char *) this_cmd));
193 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
194 unformat_free (vam->input);
196 if (vam->regenerate_interface_table)
198 vam->regenerate_interface_table = 0;
199 api_sw_interface_dump (vam);
202 /* Hack to pick up new client index after memfd_segment_create pivot */
203 if (vam->client_index_invalid)
205 vat_main_t *vam = &vat_main;
206 api_main_t *am = vlibapi_get_main ();
208 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
209 vam->my_client_index = am->my_client_index;
210 vam->client_index_invalid = 0;
216 init_error_string_table (vat_main_t * vam)
219 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
221 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
222 foreach_vnet_api_error;
225 hash_set (vam->error_string_by_error_number, 99, "Misc");
229 eval_current_file (clib_macro_main_t * mm, i32 complain)
231 vat_main_t *vam = &vat_main;
232 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
236 eval_current_line (clib_macro_main_t * mm, i32 complain)
238 vat_main_t *vam = &vat_main;
239 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
243 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
245 vat_main_t *vam = &vat_main;
249 /* these (caught) signals cause the application to exit */
252 if (vam->jump_buf_set)
258 /* FALLTHROUGH on purpose */
268 setup_signal_handlers (void)
273 for (i = 1; i < 32; i++)
275 clib_memset (&sa, 0, sizeof (sa));
276 sa.sa_sigaction = (void *) signal_handler;
277 sa.sa_flags = SA_SIGINFO;
281 /* these signals take the default action */
290 /* ignore SIGPIPE, SIGCHLD */
294 sa.sa_sigaction = (void *) SIG_IGN;
297 /* catch and handle all other signals */
302 if (sigaction (i, &sa, 0) < 0)
303 clib_unix_warning ("sigaction %U", format_signal, i);
308 vat_find_plugin_path ()
310 char *p, path[PATH_MAX];
314 /* find executable path */
315 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
318 /* readlink doesn't provide null termination */
322 if ((p = strrchr (path, '/')) == 0)
327 if ((p = strrchr (path, '/')) == 0)
331 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
332 "%s/lib/vpp_api_test_plugins", path, path);
334 vat_plugin_path = (char *) s;
340 vat_registered_features_t *f;
341 vat_main_t *vam = &vat_main;
344 f = vam->feature_function_registrations;
348 error = f->function (vam);
351 clib_warning ("INIT FAILED");
357 static inline clib_error_t *
358 call_init_exit_functions_internal (vlib_main_t * vm,
359 _vlib_init_function_list_elt_t ** headp,
360 int call_once, int do_sort)
362 clib_error_t *error = 0;
363 _vlib_init_function_list_elt_t *i;
366 /* Not worth copying the topological sort code */
367 if (do_sort && (error = vlib_sort_init_exit_functions (headp)))
374 if (call_once && !hash_get (vm->init_functions_called, i->f))
377 hash_set1 (vm->init_functions_called, i->f);
382 i = i->next_init_function;
388 vlib_call_init_exit_functions (vlib_main_t * vm,
389 _vlib_init_function_list_elt_t ** headp,
392 return call_init_exit_functions_internal (vm, headp, call_once,
397 main (int argc, char **argv)
399 vat_main_t *vam = &vat_main;
400 unformat_input_t _argv, *a = &_argv;
401 u8 **input_files = 0;
410 vlib_main_t *vm = &vlib_global_main;
412 clib_mem_init_thread_safe (0, 128 << 20);
414 clib_macro_init (&vam->macro_main);
415 clib_macro_add_builtin (&vam->macro_main, "current_file",
417 clib_macro_add_builtin (&vam->macro_main, "current_line",
420 init_error_string_table (vam);
421 vec_validate (vam->cmd_reply, 0);
422 vec_reset_length (vam->cmd_reply);
424 vat_find_plugin_path ();
426 unformat_init_command_line (a, argv);
428 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
430 if (unformat (a, "in %s", &this_input_file))
431 vec_add1 (input_files, this_input_file);
432 else if (unformat (a, "out %s", &output_file))
434 else if (unformat (a, "script"))
436 else if (unformat (a, "json"))
438 else if (unformat (a, "socket-name %s", &vam->socket_name))
440 else if (unformat (a, "default-socket"))
442 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
444 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
445 vec_add1 (vat_plugin_path, 0);
446 else if (unformat (a, "plugin_name_filter %s",
447 (u8 *) & vat_plugin_name_filter))
448 vec_add1 (vat_plugin_name_filter, 0);
449 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
451 vl_set_memory_root_path ((char *) chroot_prefix);
457 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
458 "[plugin_path <path>][default-socket][socket-name <name>]\n"
459 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
466 vam->ofp = fopen ((char *) output_file, "w");
470 if (vam->ofp == NULL)
472 fformat (stderr, "Couldn't open output file %s\n",
473 output_file ? (char *) output_file : "stdout");
477 clib_time_init (&vam->clib_time);
479 vat_api_hookup (vam);
480 vat_plugin_api_reference ();
482 setup_signal_handlers ();
484 if (vam->socket_name && vat_socket_connect (vam))
485 fformat (stderr, "WARNING: socket connection failed");
487 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
488 && connect_to_vpe ("vpp_api_test") < 0)
491 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
495 vam->json_output = json_output;
498 api_sw_interface_dump (vam);
500 vec_validate (vam->inbuf, 4096);
504 vam->current_file = (u8 *) "plugin-init";
505 vat_plugin_init (vam);
507 /* Set up the init function hash table */
508 vm->init_functions_called = hash_create (0, 0);
510 /* Execute plugin init and api_init functions */
511 error = vlib_call_init_exit_functions
512 (vm, &vm->init_function_registrations, 1 /* call once */ );
515 clib_error_report (error);
517 error = vlib_call_init_exit_functions
518 (vm, &vm->api_init_function_registrations, 1 /* call_once */ );
521 clib_error_report (error);
523 for (i = 0; i < vec_len (input_files); i++)
525 vam->ifp = fopen ((char *) input_files[i], "r");
526 if (vam->ifp == NULL)
528 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
531 vam->current_file = input_files[i];
532 vam->input_line_number = 0;
544 vam->current_file = (u8 *) "interactive";
550 * Particularly when running a script, don't be in a hurry to leave.
551 * A reply message queued to this process will end up constipating
552 * the allocation rings.
554 timeout = vat_time_now (vam) + 2.0;
555 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
558 if (vat_time_now (vam) > timeout)
559 clib_warning ("BUG: message reply spin-wait timeout");
561 vl_client_disconnect_from_vlib ();
566 * fd.io coding-style-patch-verification: ON
569 * eval: (c-set-style "gnu")