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 vat_suspend (vlib_main_t * vm, f64 interval)
24 /* do nothing in the standalone version, just return */
28 fformat_append_cr (FILE * ofp, const char *fmt, ...)
33 (void) va_fformat (ofp, (char *) fmt, &va);
39 connect_to_vpe (char *name)
41 vat_main_t *vam = &vat_main;
42 api_main_t *am = &api_main;
44 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
47 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
48 vam->my_client_index = am->my_client_index;
53 vlib_main_t vlib_global_main;
54 vlib_main_t **vlib_mains;
56 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
63 format_api_error (u8 * s, va_list * args)
65 vat_main_t *vam = va_arg (*args, vat_main_t *);
66 i32 error = va_arg (*args, u32);
69 p = hash_get (vam->error_string_by_error_number, -error);
72 s = format (s, "%s", p[0]);
74 s = format (s, "%d", error);
79 do_one_file (vat_main_t * vam)
82 int (*fp) (vat_main_t * vam);
84 unformat_input_t _input;
91 /* Used by the "quit" command handler */
92 if (setjmp (vam->jump_buf) != 0)
95 vam->jump_buf_set = 1;
99 if (vam->ifp == stdin)
101 if (vam->exec_mode == 0)
102 rv = write (1, "vat# ", 5);
104 rv = write (1, "exec# ", 6);
107 _vec_len (vam->inbuf) = 4096;
110 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
113 vam->input_line_number++;
118 (u8 *) clib_macro_eval (&vam->macro_main, (char *) vam->inbuf,
121 if (vam->exec_mode == 0)
123 /* Split input into cmd + args */
126 while (cmdp < (this_cmd + vec_len (this_cmd)))
128 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
136 while (argsp < (this_cmd + vec_len (this_cmd)))
138 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
146 while (argsp < (this_cmd + vec_len (this_cmd)))
148 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
157 /* Blank input line? */
161 p = hash_get_mem (vam->function_by_name, cmdp);
164 errmsg ("'%s': function not found\n", cmdp);
168 arg_len = strlen ((char *) argsp);
170 unformat_init_string (vam->input, (char *) argsp, arg_len);
175 unformat_init_string (vam->input, (char *) this_cmd,
176 strlen ((char *) this_cmd));
183 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
184 unformat_free (vam->input);
186 if (vam->regenerate_interface_table)
188 vam->regenerate_interface_table = 0;
189 api_sw_interface_dump (vam);
195 init_error_string_table (vat_main_t * vam)
198 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
200 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
201 foreach_vnet_api_error;
204 hash_set (vam->error_string_by_error_number, 99, "Misc");
208 eval_current_file (macro_main_t * mm, i32 complain)
210 vat_main_t *vam = &vat_main;
211 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
215 eval_current_line (macro_main_t * mm, i32 complain)
217 vat_main_t *vam = &vat_main;
218 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
222 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
224 vat_main_t *vam = &vat_main;
228 /* these (caught) signals cause the application to exit */
231 if (vam->jump_buf_set)
237 /* FALLTHROUGH on purpose */
247 setup_signal_handlers (void)
252 for (i = 1; i < 32; i++)
254 memset (&sa, 0, sizeof (sa));
255 sa.sa_sigaction = (void *) signal_handler;
256 sa.sa_flags = SA_SIGINFO;
260 /* these signals take the default action */
268 /* ignore SIGPIPE, SIGCHLD */
271 sa.sa_sigaction = (void *) SIG_IGN;
274 /* catch and handle all other signals */
279 if (sigaction (i, &sa, 0) < 0)
280 clib_unix_warning ("sigaction %U", format_signal, i);
285 main (int argc, char **argv)
287 vat_main_t *vam = &vat_main;
288 unformat_input_t _argv, *a = &_argv;
289 u8 **input_files = 0;
299 clib_mem_init (0, 128 << 20);
301 heap = clib_mem_get_per_cpu_heap ();
302 h = mheap_header (heap);
304 /* make the main heap thread-safe */
305 h->flags |= MHEAP_FLAG_THREAD_SAFE;
307 clib_macro_init (&vam->macro_main);
308 clib_macro_add_builtin (&vam->macro_main, "current_file",
310 clib_macro_add_builtin (&vam->macro_main, "current_line",
313 init_error_string_table (vam);
315 unformat_init_command_line (a, argv);
317 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
319 if (unformat (a, "in %s", &this_input_file))
320 vec_add1 (input_files, this_input_file);
321 else if (unformat (a, "out %s", &output_file))
323 else if (unformat (a, "script"))
325 else if (unformat (a, "json"))
327 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
328 vec_add1 (vat_plugin_path, 0);
329 else if (unformat (a, "plugin_name_filter %s",
330 (u8 *) & vat_plugin_name_filter))
331 vec_add1 (vat_plugin_name_filter, 0);
332 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
334 vl_set_memory_root_path ((char *) chroot_prefix);
339 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n");
345 vam->ofp = fopen ((char *) output_file, "w");
349 if (vam->ofp == NULL)
351 fformat (stderr, "Couldn't open output file %s\n",
352 output_file ? (char *) output_file : "stdout");
356 clib_time_init (&vam->clib_time);
358 vat_api_hookup (vam);
359 vat_plugin_api_reference ();
361 setup_signal_handlers ();
363 if (connect_to_vpe ("vpp_api_test") < 0)
366 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
370 vam->json_output = json_output;
374 api_sw_interface_dump (vam);
377 vec_validate (vam->inbuf, 4096);
379 vam->current_file = (u8 *) "plugin-init";
380 vat_plugin_init (vam);
382 for (i = 0; i < vec_len (input_files); i++)
384 vam->ifp = fopen ((char *) input_files[i], "r");
385 if (vam->ifp == NULL)
387 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
390 vam->current_file = input_files[i];
391 vam->input_line_number = 0;
403 vam->current_file = (u8 *) "interactive";
408 vl_client_disconnect_from_vlib ();
413 * fd.io coding-style-patch-verification: ON
416 * eval: (c-set-style "gnu")