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, (i8 *) 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);
194 /* Hack to pick up new client index after memfd_segment_create pivot */
195 if (vam->client_index_invalid)
197 vat_main_t *vam = &vat_main;
198 api_main_t *am = &api_main;
200 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
201 vam->my_client_index = am->my_client_index;
202 vam->client_index_invalid = 0;
208 init_error_string_table (vat_main_t * vam)
211 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
213 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
214 foreach_vnet_api_error;
217 hash_set (vam->error_string_by_error_number, 99, "Misc");
221 eval_current_file (macro_main_t * mm, i32 complain)
223 vat_main_t *vam = &vat_main;
224 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
228 eval_current_line (macro_main_t * mm, i32 complain)
230 vat_main_t *vam = &vat_main;
231 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
235 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
237 vat_main_t *vam = &vat_main;
241 /* these (caught) signals cause the application to exit */
244 if (vam->jump_buf_set)
250 /* FALLTHROUGH on purpose */
260 setup_signal_handlers (void)
265 for (i = 1; i < 32; i++)
267 memset (&sa, 0, sizeof (sa));
268 sa.sa_sigaction = (void *) signal_handler;
269 sa.sa_flags = SA_SIGINFO;
273 /* these signals take the default action */
281 /* ignore SIGPIPE, SIGCHLD */
284 sa.sa_sigaction = (void *) SIG_IGN;
287 /* catch and handle all other signals */
292 if (sigaction (i, &sa, 0) < 0)
293 clib_unix_warning ("sigaction %U", format_signal, i);
298 main (int argc, char **argv)
300 vat_main_t *vam = &vat_main;
301 unformat_input_t _argv, *a = &_argv;
302 u8 **input_files = 0;
313 clib_mem_init (0, 128 << 20);
315 heap = clib_mem_get_per_cpu_heap ();
316 h = mheap_header (heap);
318 /* make the main heap thread-safe */
319 h->flags |= MHEAP_FLAG_THREAD_SAFE;
321 clib_macro_init (&vam->macro_main);
322 clib_macro_add_builtin (&vam->macro_main, "current_file",
324 clib_macro_add_builtin (&vam->macro_main, "current_line",
327 init_error_string_table (vam);
328 vec_validate (vam->cmd_reply, 0);
329 vec_reset_length (vam->cmd_reply);
331 unformat_init_command_line (a, argv);
333 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
335 if (unformat (a, "in %s", &this_input_file))
336 vec_add1 (input_files, this_input_file);
337 else if (unformat (a, "out %s", &output_file))
339 else if (unformat (a, "script"))
341 else if (unformat (a, "json"))
343 else if (unformat (a, "socket-name %s", &vam->socket_name))
345 else if (unformat (a, "default-socket"))
347 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
349 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
350 vec_add1 (vat_plugin_path, 0);
351 else if (unformat (a, "plugin_name_filter %s",
352 (u8 *) & vat_plugin_name_filter))
353 vec_add1 (vat_plugin_name_filter, 0);
354 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
356 vl_set_memory_root_path ((char *) chroot_prefix);
362 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
363 "[plugin_path <path>][default-socket][socket-name <name>]\n"
364 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
371 vam->ofp = fopen ((char *) output_file, "w");
375 if (vam->ofp == NULL)
377 fformat (stderr, "Couldn't open output file %s\n",
378 output_file ? (char *) output_file : "stdout");
382 clib_time_init (&vam->clib_time);
384 vat_api_hookup (vam);
385 vat_plugin_api_reference ();
387 setup_signal_handlers ();
389 if (vam->socket_name && vat_socket_connect (vam))
390 fformat (stderr, "WARNING: socket connection failed");
392 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
393 && connect_to_vpe ("vpp_api_test") < 0)
396 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
400 vam->json_output = json_output;
403 api_sw_interface_dump (vam);
405 vec_validate (vam->inbuf, 4096);
407 vam->current_file = (u8 *) "plugin-init";
408 vat_plugin_init (vam);
410 for (i = 0; i < vec_len (input_files); i++)
412 vam->ifp = fopen ((char *) input_files[i], "r");
413 if (vam->ifp == NULL)
415 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
418 vam->current_file = input_files[i];
419 vam->input_line_number = 0;
431 vam->current_file = (u8 *) "interactive";
437 * Particularly when running a script, don't be in a hurry to leave.
438 * A reply message queued to this process will end up constipating
439 * the allocation rings.
441 timeout = vat_time_now (vam) + 2.0;
442 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
445 if (vat_time_now (vam) > timeout)
446 clib_warning ("BUG: message reply spin-wait timeout");
448 vl_client_disconnect_from_vlib ();
453 * fd.io coding-style-patch-verification: ON
456 * eval: (c-set-style "gnu")