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.
23 #include <vlibapi/api_helper_macros.h>
26 vat_suspend (vlib_main_t * vm, f64 interval)
28 /* do nothing in the standalone version, just return */
32 connect_to_vpe (char *name)
34 vat_main_t *vam = &vat_main;
35 api_main_t *am = vlibapi_get_main ();
37 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
40 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
41 vam->my_client_index = am->my_client_index;
48 vlib_global_main_t vlib_global_main;
51 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
57 format_api_error (u8 * s, va_list * args)
59 vat_main_t *vam = va_arg (*args, vat_main_t *);
60 i32 error = va_arg (*args, u32);
63 p = hash_get (vam->error_string_by_error_number, -error);
66 s = format (s, "%s", p[0]);
68 s = format (s, "%d", error);
73 do_one_file (vat_main_t * vam)
76 int (*fp) (vat_main_t * vam);
78 unformat_input_t _input;
85 /* Used by the "quit" command handler */
86 if (setjmp (vam->jump_buf) != 0)
89 vam->jump_buf_set = 1;
93 if (vam->ifp == stdin)
95 if (vam->exec_mode == 0)
96 rv = write (1, "vat# ", 5);
98 rv = write (1, "exec# ", 6);
101 _vec_len (vam->inbuf) = 4096;
104 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
107 vam->input_line_number++;
112 (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf,
117 if (vam->exec_mode == 0)
119 /* Split input into cmd + args */
122 while (cmdp < (this_cmd + vec_len (this_cmd)))
124 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
132 while (argsp < (this_cmd + vec_len (this_cmd)))
134 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
142 while (argsp < (this_cmd + vec_len (this_cmd)))
144 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
153 /* Blank input line? */
157 p = hash_get_mem (vam->function_by_name, cmdp);
160 errmsg ("'%s': function not found\n", cmdp);
164 arg_len = strlen ((char *) argsp);
166 unformat_init_string (vam->input, (char *) argsp, arg_len);
171 unformat_init_string (vam->input, (char *) this_cmd,
172 strlen ((char *) this_cmd));
179 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
180 unformat_free (vam->input);
182 if (vam->regenerate_interface_table)
184 vam->regenerate_interface_table = 0;
185 vam->api_sw_interface_dump (vam);
188 /* Hack to pick up new client index after memfd_segment_create pivot */
189 if (vam->client_index_invalid)
191 vat_main_t *vam = &vat_main;
192 api_main_t *am = vlibapi_get_main ();
194 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
195 vam->my_client_index = am->my_client_index;
196 vam->client_index_invalid = 0;
202 init_error_string_table (vat_main_t * vam)
205 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
207 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
208 foreach_vnet_api_error;
211 hash_set (vam->error_string_by_error_number, 99, "Misc");
215 eval_current_file (clib_macro_main_t * mm, i32 complain)
217 vat_main_t *vam = &vat_main;
218 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
222 eval_current_line (clib_macro_main_t * mm, i32 complain)
224 vat_main_t *vam = &vat_main;
225 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
229 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
231 vat_main_t *vam = &vat_main;
235 /* these (caught) signals cause the application to exit */
238 if (vam->jump_buf_set)
244 /* FALLTHROUGH on purpose */
254 setup_signal_handlers (void)
259 for (i = 1; i < 32; i++)
261 clib_memset (&sa, 0, sizeof (sa));
262 sa.sa_sigaction = (void *) signal_handler;
263 sa.sa_flags = SA_SIGINFO;
267 /* these signals take the default action */
277 /* ignore SIGPIPE, SIGCHLD */
281 sa.sa_sigaction = (void *) SIG_IGN;
284 /* catch and handle all other signals */
289 if (sigaction (i, &sa, 0) < 0)
290 clib_unix_warning ("sigaction %U", format_signal, i);
295 vat_find_plugin_path ()
297 char *p, path[PATH_MAX];
301 /* find executable path */
302 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
305 /* readlink doesn't provide null termination */
309 if ((p = strrchr (path, '/')) == 0)
314 if ((p = strrchr (path, '/')) == 0)
318 s = format (0, "%s/" CLIB_LIB_DIR "/vpp_api_test_plugins", path, path);
320 vat_plugin_path = (char *) s;
326 vat_registered_features_t *f;
327 vat_main_t *vam = &vat_main;
330 f = vam->feature_function_registrations;
334 error = f->function (vam);
337 clib_warning ("INIT FAILED");
343 static inline clib_error_t *
344 call_init_exit_functions_internal (vlib_main_t *vm,
345 _vlib_init_function_list_elt_t **headp,
346 int call_once, int do_sort, int is_global)
348 vlib_global_main_t *vgm = vlib_get_global_main ();
349 clib_error_t *error = 0;
350 _vlib_init_function_list_elt_t *i;
352 ASSERT (is_global == 1);
355 /* Not worth copying the topological sort code */
356 if (do_sort && (error = vlib_sort_init_exit_functions (headp)))
363 if (call_once && !hash_get (vgm->init_functions_called, i->f))
366 hash_set1 (vgm->init_functions_called, i->f);
371 i = i->next_init_function;
377 vlib_call_init_exit_functions (vlib_main_t *vm,
378 _vlib_init_function_list_elt_t **headp,
379 int call_once, int is_global)
381 return call_init_exit_functions_internal (vm, headp, call_once,
382 1 /* do_sort */, is_global);
386 vat_register_interface_dump (vat_main_t *vam)
391 vec_foreach (pi, vat_plugin_main.plugin_info)
393 handle = dlsym (pi->handle, "api_sw_interface_dump");
396 vam->api_sw_interface_dump = handle;
401 if (!vam->api_sw_interface_dump)
404 "sw_interface_dump not found in interface_test plugin!\n");
410 main (int argc, char **argv)
412 vlib_global_main_t *vgm = vlib_get_global_main ();
413 vat_main_t *vam = &vat_main;
414 unformat_input_t _argv, *a = &_argv;
415 u8 **input_files = 0;
426 clib_mem_init_thread_safe (0, 128 << 20);
428 vm = vlib_get_first_main ();
430 clib_macro_init (&vam->macro_main);
431 clib_macro_add_builtin (&vam->macro_main, "current_file",
433 clib_macro_add_builtin (&vam->macro_main, "current_line",
436 init_error_string_table (vam);
437 vec_validate (vam->cmd_reply, 0);
438 vec_reset_length (vam->cmd_reply);
440 vat_find_plugin_path ();
442 unformat_init_command_line (a, argv);
444 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
446 if (unformat (a, "in %s", &this_input_file))
447 vec_add1 (input_files, this_input_file);
448 else if (unformat (a, "out %s", &output_file))
450 else if (unformat (a, "script"))
452 else if (unformat (a, "json"))
454 else if (unformat (a, "socket-name %s", &vam->socket_name))
456 else if (unformat (a, "default-socket"))
458 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
460 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
461 vec_add1 (vat_plugin_path, 0);
462 else if (unformat (a, "plugin_name_filter %s",
463 (u8 *) & vat_plugin_name_filter))
464 vec_add1 (vat_plugin_name_filter, 0);
465 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
467 vl_set_memory_root_path ((char *) chroot_prefix);
473 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
474 "[plugin_path <path>][default-socket][socket-name <name>]\n"
475 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
482 vam->ofp = fopen ((char *) output_file, "w");
486 if (vam->ofp == NULL)
488 fformat (stderr, "Couldn't open output file %s\n",
489 output_file ? (char *) output_file : "stdout");
493 clib_time_init (&vam->clib_time);
495 vat_api_hookup (vam);
496 vat_plugin_api_reference ();
498 setup_signal_handlers ();
500 if (vam->socket_name && vat_socket_connect (vam))
501 fformat (stderr, "WARNING: socket connection failed");
503 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
504 && connect_to_vpe ("vpp_api_test") < 0)
507 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
511 vam->json_output = json_output;
513 vec_validate (vam->inbuf, 4096);
517 vam->current_file = (u8 *) "plugin-init";
518 vat_plugin_init (vam);
520 vat_register_interface_dump (vam);
523 vam->api_sw_interface_dump (vam);
525 /* Set up the init function hash table */
526 vgm->init_functions_called = hash_create (0, 0);
528 /* Execute plugin init and api_init functions */
529 error = vlib_call_init_exit_functions (vm, &vgm->init_function_registrations,
530 1 /* call once */, 1 /* is_global*/);
533 clib_error_report (error);
536 vlib_call_init_exit_functions (vm, &vgm->api_init_function_registrations,
537 1 /* call_once */, 1 /* is_global */);
540 clib_error_report (error);
542 for (i = 0; i < vec_len (input_files); i++)
544 vam->ifp = fopen ((char *) input_files[i], "r");
545 if (vam->ifp == NULL)
547 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
550 vam->current_file = input_files[i];
551 vam->input_line_number = 0;
563 vam->current_file = (u8 *) "interactive";
569 * Particularly when running a script, don't be in a hurry to leave.
570 * A reply message queued to this process will end up constipating
571 * the allocation rings.
573 timeout = vat_time_now (vam) + 2.0;
574 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
577 if (vat_time_now (vam) > timeout)
578 clib_warning ("BUG: message reply spin-wait timeout");
580 vl_client_disconnect_from_vlib ();
585 * fd.io coding-style-patch-verification: ON
588 * eval: (c-set-style "gnu")