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>
22 vpe_api_main_t vpe_api_main;
25 vat_suspend (vlib_main_t * vm, f64 interval)
27 /* do nothing in the standalone version, just return */
31 fformat_append_cr (FILE * ofp, const char *fmt, ...)
36 (void) va_fformat (ofp, (char *) fmt, &va);
42 connect_to_vpe (char *name)
44 vat_main_t *vam = &vat_main;
45 api_main_t *am = &api_main;
47 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
50 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
51 vam->my_client_index = am->my_client_index;
56 vlib_main_t vlib_global_main;
57 vlib_main_t **vlib_mains;
59 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
66 format_api_error (u8 * s, va_list * args)
68 vat_main_t *vam = va_arg (*args, vat_main_t *);
69 i32 error = va_arg (*args, u32);
72 p = hash_get (vam->error_string_by_error_number, -error);
75 s = format (s, "%s", p[0]);
77 s = format (s, "%d", error);
82 do_one_file (vat_main_t * vam)
85 int (*fp) (vat_main_t * vam);
87 unformat_input_t _input;
94 /* Used by the "quit" command handler */
95 if (setjmp (vam->jump_buf) != 0)
98 vam->jump_buf_set = 1;
102 if (vam->ifp == stdin)
104 if (vam->exec_mode == 0)
105 rv = write (1, "vat# ", 5);
107 rv = write (1, "exec# ", 6);
110 _vec_len (vam->inbuf) = 4096;
113 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
116 vam->input_line_number++;
121 (u8 *) clib_macro_eval (&vam->macro_main, (char *) vam->inbuf,
124 if (vam->exec_mode == 0)
126 /* Split input into cmd + args */
129 while (cmdp < (this_cmd + vec_len (this_cmd)))
131 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
139 while (argsp < (this_cmd + vec_len (this_cmd)))
141 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
149 while (argsp < (this_cmd + vec_len (this_cmd)))
151 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
160 /* Blank input line? */
164 p = hash_get_mem (vam->function_by_name, cmdp);
167 errmsg ("'%s': function not found\n", cmdp);
171 arg_len = strlen ((char *) argsp);
173 unformat_init_string (vam->input, (char *) argsp, arg_len);
178 unformat_init_string (vam->input, (char *) this_cmd,
179 strlen ((char *) this_cmd));
186 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
187 unformat_free (vam->input);
189 if (vam->regenerate_interface_table)
191 vam->regenerate_interface_table = 0;
192 api_sw_interface_dump (vam);
198 init_error_string_table (vat_main_t * vam)
201 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
203 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
204 foreach_vnet_api_error;
207 hash_set (vam->error_string_by_error_number, 99, "Misc");
211 eval_current_file (macro_main_t * mm, i32 complain)
213 vat_main_t *vam = &vat_main;
214 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
218 eval_current_line (macro_main_t * mm, i32 complain)
220 vat_main_t *vam = &vat_main;
221 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
225 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
227 vat_main_t *vam = &vat_main;
231 /* these (caught) signals cause the application to exit */
234 if (vam->jump_buf_set)
240 /* FALLTHROUGH on purpose */
250 setup_signal_handlers (void)
255 for (i = 1; i < 32; i++)
257 memset (&sa, 0, sizeof (sa));
258 sa.sa_sigaction = (void *) signal_handler;
259 sa.sa_flags = SA_SIGINFO;
263 /* these signals take the default action */
271 /* ignore SIGPIPE, SIGCHLD */
274 sa.sa_sigaction = (void *) SIG_IGN;
277 /* catch and handle all other signals */
282 if (sigaction (i, &sa, 0) < 0)
283 clib_unix_warning ("sigaction %U", format_signal, i);
288 main (int argc, char **argv)
290 vat_main_t *vam = &vat_main;
291 unformat_input_t _argv, *a = &_argv;
292 u8 **input_files = 0;
302 clib_mem_init (0, 128 << 20);
304 heap = clib_mem_get_per_cpu_heap ();
305 h = mheap_header (heap);
307 /* make the main heap thread-safe */
308 h->flags |= MHEAP_FLAG_THREAD_SAFE;
310 clib_macro_init (&vam->macro_main);
311 clib_macro_add_builtin (&vam->macro_main, "current_file",
313 clib_macro_add_builtin (&vam->macro_main, "current_line",
316 init_error_string_table (vam);
318 unformat_init_command_line (a, argv);
320 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
322 if (unformat (a, "in %s", &this_input_file))
323 vec_add1 (input_files, this_input_file);
324 else if (unformat (a, "out %s", &output_file))
326 else if (unformat (a, "script"))
328 else if (unformat (a, "json"))
330 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
331 vec_add1 (vat_plugin_path, 0);
332 else if (unformat (a, "plugin_name_filter %s",
333 (u8 *) & vat_plugin_name_filter))
334 vec_add1 (vat_plugin_name_filter, 0);
335 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
337 vl_set_memory_root_path ((char *) chroot_prefix);
342 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n");
348 vam->ofp = fopen ((char *) output_file, "w");
352 if (vam->ofp == NULL)
354 fformat (stderr, "Couldn't open output file %s\n",
355 output_file ? (char *) output_file : "stdout");
359 clib_time_init (&vam->clib_time);
361 vat_api_hookup (vam);
362 vat_plugin_api_reference ();
364 setup_signal_handlers ();
366 if (connect_to_vpe ("vpp_api_test") < 0)
369 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
373 vam->json_output = json_output;
377 api_sw_interface_dump (vam);
380 vec_validate (vam->inbuf, 4096);
382 vam->current_file = (u8 *) "plugin-init";
383 vat_plugin_init (vam);
385 for (i = 0; i < vec_len (input_files); i++)
387 vam->ifp = fopen ((char *) input_files[i], "r");
388 if (vam->ifp == NULL)
390 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
393 vam->current_file = input_files[i];
394 vam->input_line_number = 0;
406 vam->current_file = (u8 *) "interactive";
411 vl_client_disconnect_from_vlib ();
416 * fd.io coding-style-patch-verification: ON
419 * eval: (c-set-style "gnu")