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 connect_to_vpe (char *name)
32 vat_main_t *vam = &vat_main;
33 api_main_t *am = &api_main;
35 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
38 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
39 vam->my_client_index = am->my_client_index;
44 vlib_main_t vlib_global_main;
45 vlib_main_t **vlib_mains;
47 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
54 format_api_error (u8 * s, va_list * args)
56 vat_main_t *vam = va_arg (*args, vat_main_t *);
57 i32 error = va_arg (*args, u32);
60 p = hash_get (vam->error_string_by_error_number, -error);
63 s = format (s, "%s", p[0]);
65 s = format (s, "%d", error);
70 do_one_file (vat_main_t * vam)
73 int (*fp) (vat_main_t * vam);
75 unformat_input_t _input;
82 /* Used by the "quit" command handler */
83 if (setjmp (vam->jump_buf) != 0)
86 vam->jump_buf_set = 1;
90 if (vam->ifp == stdin)
92 if (vam->exec_mode == 0)
93 rv = write (1, "vat# ", 5);
95 rv = write (1, "exec# ", 6);
98 _vec_len (vam->inbuf) = 4096;
101 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
104 vam->input_line_number++;
109 (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf,
112 if (vam->exec_mode == 0)
114 /* Split input into cmd + args */
117 while (cmdp < (this_cmd + vec_len (this_cmd)))
119 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
127 while (argsp < (this_cmd + vec_len (this_cmd)))
129 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
137 while (argsp < (this_cmd + vec_len (this_cmd)))
139 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
148 /* Blank input line? */
152 p = hash_get_mem (vam->function_by_name, cmdp);
155 errmsg ("'%s': function not found\n", cmdp);
159 arg_len = strlen ((char *) argsp);
161 unformat_init_string (vam->input, (char *) argsp, arg_len);
166 unformat_init_string (vam->input, (char *) this_cmd,
167 strlen ((char *) this_cmd));
174 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
175 unformat_free (vam->input);
177 if (vam->regenerate_interface_table)
179 vam->regenerate_interface_table = 0;
180 api_sw_interface_dump (vam);
183 /* Hack to pick up new client index after memfd_segment_create pivot */
184 if (vam->client_index_invalid)
186 vat_main_t *vam = &vat_main;
187 api_main_t *am = &api_main;
189 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
190 vam->my_client_index = am->my_client_index;
191 vam->client_index_invalid = 0;
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;
300 clib_mem_init_thread_safe (0, 128 << 20);
302 clib_macro_init (&vam->macro_main);
303 clib_macro_add_builtin (&vam->macro_main, "current_file",
305 clib_macro_add_builtin (&vam->macro_main, "current_line",
308 init_error_string_table (vam);
309 vec_validate (vam->cmd_reply, 0);
310 vec_reset_length (vam->cmd_reply);
312 unformat_init_command_line (a, argv);
314 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
316 if (unformat (a, "in %s", &this_input_file))
317 vec_add1 (input_files, this_input_file);
318 else if (unformat (a, "out %s", &output_file))
320 else if (unformat (a, "script"))
322 else if (unformat (a, "json"))
324 else if (unformat (a, "socket-name %s", &vam->socket_name))
326 else if (unformat (a, "default-socket"))
328 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
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);
343 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
344 "[plugin_path <path>][default-socket][socket-name <name>]\n"
345 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
352 vam->ofp = fopen ((char *) output_file, "w");
356 if (vam->ofp == NULL)
358 fformat (stderr, "Couldn't open output file %s\n",
359 output_file ? (char *) output_file : "stdout");
363 clib_time_init (&vam->clib_time);
365 vat_api_hookup (vam);
366 vat_plugin_api_reference ();
368 setup_signal_handlers ();
370 if (vam->socket_name && vat_socket_connect (vam))
371 fformat (stderr, "WARNING: socket connection failed");
373 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
374 && connect_to_vpe ("vpp_api_test") < 0)
377 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
381 vam->json_output = json_output;
384 api_sw_interface_dump (vam);
386 vec_validate (vam->inbuf, 4096);
388 vam->current_file = (u8 *) "plugin-init";
389 vat_plugin_init (vam);
391 for (i = 0; i < vec_len (input_files); i++)
393 vam->ifp = fopen ((char *) input_files[i], "r");
394 if (vam->ifp == NULL)
396 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
399 vam->current_file = input_files[i];
400 vam->input_line_number = 0;
412 vam->current_file = (u8 *) "interactive";
418 * Particularly when running a script, don't be in a hurry to leave.
419 * A reply message queued to this process will end up constipating
420 * the allocation rings.
422 timeout = vat_time_now (vam) + 2.0;
423 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
426 if (vat_time_now (vam) > timeout)
427 clib_warning ("BUG: message reply spin-wait timeout");
429 vl_client_disconnect_from_vlib ();
434 * fd.io coding-style-patch-verification: ON
437 * eval: (c-set-style "gnu")