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 connect_to_vpe (char *name)
24 vat_main_t *vam = &vat_main;
25 api_main_t *am = &api_main;
27 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
30 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
31 vam->my_client_index = am->my_client_index;
37 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
44 format_api_error (u8 * s, va_list * args)
46 vat_main_t *vam = va_arg (*args, vat_main_t *);
47 i32 error = va_arg (*args, u32);
50 p = hash_get (vam->error_string_by_error_number, -error);
53 s = format (s, "%s", p[0]);
55 s = format (s, "%d", error);
60 do_one_file (vat_main_t * vam)
63 int (*fp) (vat_main_t * vam);
65 unformat_input_t _input;
72 /* Used by the "quit" command handler */
73 if (setjmp (vam->jump_buf) != 0)
76 vam->jump_buf_set = 1;
80 if (vam->ifp == stdin)
82 if (vam->exec_mode == 0)
83 rv = write (1, "vat# ", 5);
85 rv = write (1, "exec# ", 6);
88 _vec_len (vam->inbuf) = 4096;
91 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
94 vam->input_line_number++;
99 (u8 *) clib_macro_eval (&vam->macro_main, (char *) vam->inbuf,
102 if (vam->exec_mode == 0)
104 /* Split input into cmd + args */
107 while (cmdp < (this_cmd + vec_len (this_cmd)))
109 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
117 while (argsp < (this_cmd + vec_len (this_cmd)))
119 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
127 while (argsp < (this_cmd + vec_len (this_cmd)))
129 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
138 /* Blank input line? */
142 p = hash_get_mem (vam->function_by_name, cmdp);
145 errmsg ("'%s': function not found\n", cmdp);
149 arg_len = strlen ((char *) argsp);
151 unformat_init_string (vam->input, (char *) argsp, arg_len);
156 unformat_init_string (vam->input, (char *) this_cmd,
157 strlen ((char *) this_cmd));
164 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
165 unformat_free (vam->input);
167 if (vam->regenerate_interface_table)
169 vam->regenerate_interface_table = 0;
170 api_sw_interface_dump (vam);
176 init_error_string_table (vat_main_t * vam)
179 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
181 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
182 foreach_vnet_api_error;
185 hash_set (vam->error_string_by_error_number, 99, "Misc");
189 eval_current_file (macro_main_t * mm, i32 complain)
191 vat_main_t *vam = &vat_main;
192 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
196 eval_current_line (macro_main_t * mm, i32 complain)
198 vat_main_t *vam = &vat_main;
199 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
203 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
205 vat_main_t *vam = &vat_main;
209 /* these (caught) signals cause the application to exit */
212 if (vam->jump_buf_set)
218 /* FALLTHROUGH on purpose */
228 setup_signal_handlers (void)
233 for (i = 1; i < 32; i++)
235 memset (&sa, 0, sizeof (sa));
236 sa.sa_sigaction = (void *) signal_handler;
237 sa.sa_flags = SA_SIGINFO;
241 /* these signals take the default action */
249 /* ignore SIGPIPE, SIGCHLD */
252 sa.sa_sigaction = (void *) SIG_IGN;
255 /* catch and handle all other signals */
260 if (sigaction (i, &sa, 0) < 0)
261 clib_unix_warning ("sigaction %U", format_signal, i);
266 main (int argc, char **argv)
268 vat_main_t *vam = &vat_main;
269 unformat_input_t _argv, *a = &_argv;
270 u8 **input_files = 0;
280 clib_mem_init (0, 128 << 20);
282 heap = clib_mem_get_per_cpu_heap ();
283 h = mheap_header (heap);
285 /* make the main heap thread-safe */
286 h->flags |= MHEAP_FLAG_THREAD_SAFE;
288 clib_macro_init (&vam->macro_main);
289 clib_macro_add_builtin (&vam->macro_main, "current_file",
291 clib_macro_add_builtin (&vam->macro_main, "current_line",
294 init_error_string_table (vam);
296 unformat_init_command_line (a, argv);
298 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
300 if (unformat (a, "in %s", &this_input_file))
301 vec_add1 (input_files, this_input_file);
302 else if (unformat (a, "out %s", &output_file))
304 else if (unformat (a, "script"))
306 else if (unformat (a, "json"))
308 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
309 vec_add1 (vat_plugin_path, 0);
310 else if (unformat (a, "plugin_name_filter %s",
311 (u8 *) & vat_plugin_name_filter))
312 vec_add1 (vat_plugin_name_filter, 0);
313 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
315 vl_set_memory_root_path ((char *) chroot_prefix);
320 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n");
326 vam->ofp = fopen ((char *) output_file, "w");
330 if (vam->ofp == NULL)
332 fformat (stderr, "Couldn't open output file %s\n",
333 output_file ? (char *) output_file : "stdout");
337 clib_time_init (&vam->clib_time);
339 vat_api_hookup (vam);
340 vat_plugin_api_reference ();
342 setup_signal_handlers ();
344 if (connect_to_vpe ("vpp_api_test") < 0)
347 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
351 vam->json_output = json_output;
355 api_sw_interface_dump (vam);
358 vec_validate (vam->inbuf, 4096);
360 vam->current_file = (u8 *) "plugin-init";
361 vat_plugin_init (vam);
363 for (i = 0; i < vec_len (input_files); i++)
365 vam->ifp = fopen ((char *) input_files[i], "r");
366 if (vam->ifp == NULL)
368 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
371 vam->current_file = input_files[i];
372 vam->input_line_number = 0;
384 vam->current_file = (u8 *) "interactive";
389 vl_client_disconnect_from_vlib ();
394 * fd.io coding-style-patch-verification: ON
397 * eval: (c-set-style "gnu")