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.
21 #include <vlibapi/api_helper_macros.h>
24 vat_suspend (vlib_main_t * vm, f64 interval)
26 /* do nothing in the standalone version, just return */
30 fformat_append_cr (FILE * ofp, const char *fmt, ...)
35 (void) va_fformat (ofp, (char *) fmt, &va);
41 connect_to_vpe (char *name)
43 vat_main_t *vam = &vat_main;
44 api_main_t *am = &api_main;
46 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
49 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
50 vam->my_client_index = am->my_client_index;
55 vlib_main_t vlib_global_main;
56 vlib_main_t **vlib_mains;
58 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
65 format_api_error (u8 * s, va_list * args)
67 vat_main_t *vam = va_arg (*args, vat_main_t *);
68 i32 error = va_arg (*args, u32);
71 p = hash_get (vam->error_string_by_error_number, -error);
74 s = format (s, "%s", p[0]);
76 s = format (s, "%d", error);
81 do_one_file (vat_main_t * vam)
84 int (*fp) (vat_main_t * vam);
86 unformat_input_t _input;
93 /* Used by the "quit" command handler */
94 if (setjmp (vam->jump_buf) != 0)
97 vam->jump_buf_set = 1;
101 if (vam->ifp == stdin)
103 if (vam->exec_mode == 0)
104 rv = write (1, "vat# ", 5);
106 rv = write (1, "exec# ", 6);
109 _vec_len (vam->inbuf) = 4096;
112 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
115 vam->input_line_number++;
120 (u8 *) clib_macro_eval (&vam->macro_main, (char *) vam->inbuf,
123 if (vam->exec_mode == 0)
125 /* Split input into cmd + args */
128 while (cmdp < (this_cmd + vec_len (this_cmd)))
130 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
138 while (argsp < (this_cmd + vec_len (this_cmd)))
140 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
148 while (argsp < (this_cmd + vec_len (this_cmd)))
150 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
159 /* Blank input line? */
163 p = hash_get_mem (vam->function_by_name, cmdp);
166 errmsg ("'%s': function not found\n", cmdp);
170 arg_len = strlen ((char *) argsp);
172 unformat_init_string (vam->input, (char *) argsp, arg_len);
177 unformat_init_string (vam->input, (char *) this_cmd,
178 strlen ((char *) this_cmd));
185 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
186 unformat_free (vam->input);
188 if (vam->regenerate_interface_table)
190 vam->regenerate_interface_table = 0;
191 api_sw_interface_dump (vam);
197 init_error_string_table (vat_main_t * vam)
200 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
202 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
203 foreach_vnet_api_error;
206 hash_set (vam->error_string_by_error_number, 99, "Misc");
210 eval_current_file (macro_main_t * mm, i32 complain)
212 vat_main_t *vam = &vat_main;
213 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
217 eval_current_line (macro_main_t * mm, i32 complain)
219 vat_main_t *vam = &vat_main;
220 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
224 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
226 vat_main_t *vam = &vat_main;
230 /* these (caught) signals cause the application to exit */
233 if (vam->jump_buf_set)
239 /* FALLTHROUGH on purpose */
249 setup_signal_handlers (void)
254 for (i = 1; i < 32; i++)
256 memset (&sa, 0, sizeof (sa));
257 sa.sa_sigaction = (void *) signal_handler;
258 sa.sa_flags = SA_SIGINFO;
262 /* these signals take the default action */
270 /* ignore SIGPIPE, SIGCHLD */
273 sa.sa_sigaction = (void *) SIG_IGN;
276 /* catch and handle all other signals */
281 if (sigaction (i, &sa, 0) < 0)
282 clib_unix_warning ("sigaction %U", format_signal, i);
287 main (int argc, char **argv)
289 vat_main_t *vam = &vat_main;
290 unformat_input_t _argv, *a = &_argv;
291 u8 **input_files = 0;
301 clib_mem_init (0, 128 << 20);
303 heap = clib_mem_get_per_cpu_heap ();
304 h = mheap_header (heap);
306 /* make the main heap thread-safe */
307 h->flags |= MHEAP_FLAG_THREAD_SAFE;
309 clib_macro_init (&vam->macro_main);
310 clib_macro_add_builtin (&vam->macro_main, "current_file",
312 clib_macro_add_builtin (&vam->macro_main, "current_line",
315 init_error_string_table (vam);
317 unformat_init_command_line (a, argv);
319 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
321 if (unformat (a, "in %s", &this_input_file))
322 vec_add1 (input_files, this_input_file);
323 else if (unformat (a, "out %s", &output_file))
325 else if (unformat (a, "script"))
327 else if (unformat (a, "json"))
329 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
330 vec_add1 (vat_plugin_path, 0);
331 else if (unformat (a, "plugin_name_filter %s",
332 (u8 *) & vat_plugin_name_filter))
333 vec_add1 (vat_plugin_name_filter, 0);
334 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
336 vl_set_memory_root_path ((char *) chroot_prefix);
341 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n");
347 vam->ofp = fopen ((char *) output_file, "w");
351 if (vam->ofp == NULL)
353 fformat (stderr, "Couldn't open output file %s\n",
354 output_file ? (char *) output_file : "stdout");
358 clib_time_init (&vam->clib_time);
360 vat_api_hookup (vam);
361 vat_plugin_api_reference ();
363 setup_signal_handlers ();
365 if (connect_to_vpe ("vpp_api_test") < 0)
368 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
372 vam->json_output = json_output;
376 api_sw_interface_dump (vam);
379 vec_validate (vam->inbuf, 4096);
381 vam->current_file = (u8 *) "plugin-init";
382 vat_plugin_init (vam);
384 for (i = 0; i < vec_len (input_files); i++)
386 vam->ifp = fopen ((char *) input_files[i], "r");
387 if (vam->ifp == NULL)
389 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
392 vam->current_file = input_files[i];
393 vam->input_line_number = 0;
405 vam->current_file = (u8 *) "interactive";
410 vl_client_disconnect_from_vlib ();
415 * fd.io coding-style-patch-verification: ON
418 * eval: (c-set-style "gnu")