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 connect_to_vpe (char *name)
23 vat_main_t *vam = &vat_main;
24 api_main_t *am = &api_main;
26 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
29 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
30 vam->my_client_index = am->my_client_index;
36 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
43 format_api_error (u8 * s, va_list * args)
45 vat_main_t *vam = va_arg (*args, vat_main_t *);
46 i32 error = va_arg (*args, u32);
49 p = hash_get (vam->error_string_by_error_number, -error);
52 s = format (s, "%s", p[0]);
54 s = format (s, "%d", error);
59 do_one_file (vat_main_t * vam)
62 int (*fp) (vat_main_t * vam);
64 unformat_input_t _input;
71 /* Used by the "quit" command handler */
72 if (setjmp (vam->jump_buf) != 0)
77 if (vam->ifp == stdin)
79 if (vam->exec_mode == 0)
80 rv = write (1, "vat# ", 5);
82 rv = write (1, "exec# ", 6);
85 _vec_len (vam->inbuf) = 4096;
87 if (fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
90 vam->input_line_number++;
95 (u8 *) clib_macro_eval (&vam->macro_main, (char *) vam->inbuf,
98 if (vam->exec_mode == 0)
100 /* Split input into cmd + args */
103 while (cmdp < (this_cmd + vec_len (this_cmd)))
105 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
113 while (argsp < (this_cmd + vec_len (this_cmd)))
115 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
123 while (argsp < (this_cmd + vec_len (this_cmd)))
125 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
134 /* Blank input line? */
138 p = hash_get_mem (vam->function_by_name, cmdp);
141 errmsg ("'%s': function not found\n", cmdp);
145 arg_len = strlen ((char *) argsp);
147 unformat_init_string (vam->input, (char *) argsp, arg_len);
152 unformat_init_string (vam->input, (char *) this_cmd,
153 strlen ((char *) this_cmd));
160 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
161 unformat_free (vam->input);
163 if (vam->regenerate_interface_table)
165 vam->regenerate_interface_table = 0;
166 api_sw_interface_dump (vam);
172 init_error_string_table (vat_main_t * vam)
175 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
177 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
178 foreach_vnet_api_error;
181 hash_set (vam->error_string_by_error_number, 99, "Misc");
185 eval_current_file (macro_main_t * mm, i32 complain)
187 vat_main_t *vam = &vat_main;
188 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
192 eval_current_line (macro_main_t * mm, i32 complain)
194 vat_main_t *vam = &vat_main;
195 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
200 main (int argc, char **argv)
202 vat_main_t *vam = &vat_main;
203 unformat_input_t _argv, *a = &_argv;
204 u8 **input_files = 0;
214 clib_mem_init (0, 128 << 20);
216 heap = clib_mem_get_per_cpu_heap ();
217 h = mheap_header (heap);
219 /* make the main heap thread-safe */
220 h->flags |= MHEAP_FLAG_THREAD_SAFE;
222 clib_macro_init (&vam->macro_main);
223 clib_macro_add_builtin (&vam->macro_main, "current_file",
225 clib_macro_add_builtin (&vam->macro_main, "current_line",
228 init_error_string_table (vam);
230 unformat_init_command_line (a, argv);
232 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
234 if (unformat (a, "in %s", &this_input_file))
235 vec_add1 (input_files, this_input_file);
236 else if (unformat (a, "out %s", &output_file))
238 else if (unformat (a, "script"))
240 else if (unformat (a, "json"))
242 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
243 vec_add1 (vat_plugin_path, 0);
244 else if (unformat (a, "plugin_name_filter %s",
245 (u8 *) & vat_plugin_name_filter))
246 vec_add1 (vat_plugin_name_filter, 0);
247 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
249 vl_set_memory_root_path ((char *) chroot_prefix);
254 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n");
260 vam->ofp = fopen ((char *) output_file, "w");
264 if (vam->ofp == NULL)
266 fformat (stderr, "Couldn't open output file %s\n",
267 output_file ? (char *) output_file : "stdout");
271 clib_time_init (&vam->clib_time);
273 vat_api_hookup (vam);
274 vat_plugin_api_reference ();
276 if (connect_to_vpe ("vpp_api_test") < 0)
279 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
283 vam->json_output = json_output;
287 api_sw_interface_dump (vam);
290 vec_validate (vam->inbuf, 4096);
292 vam->current_file = (u8 *) "plugin-init";
293 vat_plugin_init (vam);
295 for (i = 0; i < vec_len (input_files); i++)
297 vam->ifp = fopen ((char *) input_files[i], "r");
298 if (vam->ifp == NULL)
300 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
303 vam->current_file = input_files[i];
304 vam->input_line_number = 0;
316 vam->current_file = (u8 *) "interactive";
321 vl_client_disconnect_from_vlib ();
326 * fd.io coding-style-patch-verification: ON
329 * eval: (c-set-style "gnu")