blob: 8ba60e2b36d98828386028b3bd16e775245dbb6e [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15#include "vat.h"
16#include "plugin.h"
Dave Barach477570e2016-10-09 17:43:22 -040017#include <signal.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070018
19vat_main_t vat_main;
20
Dave Barach72d72232016-08-04 10:15:08 -040021int
22connect_to_vpe (char *name)
Ed Warnickecb9cada2015-12-08 15:45:58 -070023{
Dave Barach72d72232016-08-04 10:15:08 -040024 vat_main_t *vam = &vat_main;
25 api_main_t *am = &api_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
Dave Barach72d72232016-08-04 10:15:08 -040027 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
28 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Dave Barach72d72232016-08-04 10:15:08 -040030 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
31 vam->my_client_index = am->my_client_index;
32
33 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070034}
35
Dave Barach72d72232016-08-04 10:15:08 -040036void
37vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -070038{
Dave Barach72d72232016-08-04 10:15:08 -040039 clib_warning ("BUG");
Ed Warnickecb9cada2015-12-08 15:45:58 -070040}
41
Dave Barach72d72232016-08-04 10:15:08 -040042
43static u8 *
44format_api_error (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070045{
Dave Barach72d72232016-08-04 10:15:08 -040046 vat_main_t *vam = va_arg (*args, vat_main_t *);
47 i32 error = va_arg (*args, u32);
48 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070049
Dave Barach72d72232016-08-04 10:15:08 -040050 p = hash_get (vam->error_string_by_error_number, -error);
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
Dave Barach72d72232016-08-04 10:15:08 -040052 if (p)
53 s = format (s, "%s", p[0]);
54 else
55 s = format (s, "%d", error);
56 return s;
57}
Ed Warnickecb9cada2015-12-08 15:45:58 -070058
Dave Barach72d72232016-08-04 10:15:08 -040059void
60do_one_file (vat_main_t * vam)
61{
62 int rv;
63 int (*fp) (vat_main_t * vam);
64 int arg_len;
65 unformat_input_t _input;
66 u8 *cmdp, *argsp;
67 uword *p;
68 u8 *this_cmd = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
Dave Barach72d72232016-08-04 10:15:08 -040070 vam->input = &_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
Dave Barach72d72232016-08-04 10:15:08 -040072 /* Used by the "quit" command handler */
73 if (setjmp (vam->jump_buf) != 0)
74 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
Dave Barach477570e2016-10-09 17:43:22 -040076 vam->jump_buf_set = 1;
77
Dave Barach72d72232016-08-04 10:15:08 -040078 while (1)
79 {
80 if (vam->ifp == stdin)
81 {
82 if (vam->exec_mode == 0)
83 rv = write (1, "vat# ", 5);
84 else
85 rv = write (1, "exec# ", 6);
86 }
87
88 _vec_len (vam->inbuf) = 4096;
89
Dave Barach477570e2016-10-09 17:43:22 -040090 if (vam->do_exit ||
91 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
Dave Barach72d72232016-08-04 10:15:08 -040092 break;
93
94 vam->input_line_number++;
95
96 vec_free (this_cmd);
97
98 this_cmd =
99 (u8 *) clib_macro_eval (&vam->macro_main, (char *) vam->inbuf,
100 1 /* complain */ );
101
102 if (vam->exec_mode == 0)
103 {
104 /* Split input into cmd + args */
105 cmdp = this_cmd;
106
107 while (cmdp < (this_cmd + vec_len (this_cmd)))
108 {
109 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
110 {
111 cmdp++;
112 }
113 else
114 break;
115 }
116 argsp = cmdp;
117 while (argsp < (this_cmd + vec_len (this_cmd)))
118 {
119 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
120 {
121 argsp++;
122 }
123 else
124 break;
125 }
126 *argsp++ = 0;
127 while (argsp < (this_cmd + vec_len (this_cmd)))
128 {
129 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
130 {
131 argsp++;
132 }
133 else
134 break;
135 }
136
137
138 /* Blank input line? */
139 if (*cmdp == 0)
140 continue;
141
142 p = hash_get_mem (vam->function_by_name, cmdp);
143 if (p == 0)
144 {
145 errmsg ("'%s': function not found\n", cmdp);
146 continue;
147 }
148
149 arg_len = strlen ((char *) argsp);
150
151 unformat_init_string (vam->input, (char *) argsp, arg_len);
152 fp = (void *) p[0];
153 }
154 else
155 {
156 unformat_init_string (vam->input, (char *) this_cmd,
157 strlen ((char *) this_cmd));
158 cmdp = this_cmd;
159 fp = exec;
160 }
161
162 rv = (*fp) (vam);
163 if (rv < 0)
164 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
165 unformat_free (vam->input);
166
167 if (vam->regenerate_interface_table)
168 {
169 vam->regenerate_interface_table = 0;
170 api_sw_interface_dump (vam);
171 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 }
173}
174
Dave Barach72d72232016-08-04 10:15:08 -0400175static void
176init_error_string_table (vat_main_t * vam)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177{
178
Dave Barach72d72232016-08-04 10:15:08 -0400179 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
181#define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
Dave Barach72d72232016-08-04 10:15:08 -0400182 foreach_vnet_api_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183#undef _
184
Dave Barach72d72232016-08-04 10:15:08 -0400185 hash_set (vam->error_string_by_error_number, 99, "Misc");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186}
187
Dave Barach72d72232016-08-04 10:15:08 -0400188static i8 *
189eval_current_file (macro_main_t * mm, i32 complain)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190{
Dave Barach72d72232016-08-04 10:15:08 -0400191 vat_main_t *vam = &vat_main;
192 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193}
194
Dave Barach72d72232016-08-04 10:15:08 -0400195static i8 *
196eval_current_line (macro_main_t * mm, i32 complain)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197{
Dave Barach72d72232016-08-04 10:15:08 -0400198 vat_main_t *vam = &vat_main;
199 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200}
201
Dave Barach477570e2016-10-09 17:43:22 -0400202static void
203signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
204{
205 vat_main_t *vam = &vat_main;
206
207 switch (signum)
208 {
209 /* these (caught) signals cause the application to exit */
210 case SIGINT:
211 case SIGTERM:
212 if (vam->jump_buf_set)
213 {
214 vam->do_exit = 1;
215 return;
216 }
217
218 /* FALLTHROUGH on purpose */
219
220 default:
221 break;
222 }
223
224 _exit (1);
225}
226
227static void
228setup_signal_handlers (void)
229{
230 uword i;
231 struct sigaction sa;
232
233 for (i = 1; i < 32; i++)
234 {
235 memset (&sa, 0, sizeof (sa));
236 sa.sa_sigaction = (void *) signal_handler;
237 sa.sa_flags = SA_SIGINFO;
238
239 switch (i)
240 {
241 /* these signals take the default action */
242 case SIGABRT:
243 case SIGKILL:
244 case SIGSTOP:
245 case SIGUSR1:
246 case SIGUSR2:
247 continue;
248
249 /* ignore SIGPIPE, SIGCHLD */
250 case SIGPIPE:
251 case SIGCHLD:
252 sa.sa_sigaction = (void *) SIG_IGN;
253 break;
254
255 /* catch and handle all other signals */
256 default:
257 break;
258 }
259
260 if (sigaction (i, &sa, 0) < 0)
261 clib_unix_warning ("sigaction %U", format_signal, i);
262 }
263}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
Dave Barach72d72232016-08-04 10:15:08 -0400265int
266main (int argc, char **argv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267{
Dave Barach72d72232016-08-04 10:15:08 -0400268 vat_main_t *vam = &vat_main;
269 unformat_input_t _argv, *a = &_argv;
270 u8 **input_files = 0;
271 u8 *output_file = 0;
272 u8 *chroot_prefix;
273 u8 *this_input_file;
274 u8 interactive = 1;
275 u8 json_output = 0;
276 u8 *heap;
277 mheap_t *h;
278 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
Dave Barach72d72232016-08-04 10:15:08 -0400280 clib_mem_init (0, 128 << 20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
Dave Barach72d72232016-08-04 10:15:08 -0400282 heap = clib_mem_get_per_cpu_heap ();
283 h = mheap_header (heap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
Dave Barach72d72232016-08-04 10:15:08 -0400285 /* make the main heap thread-safe */
286 h->flags |= MHEAP_FLAG_THREAD_SAFE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287
Dave Barach72d72232016-08-04 10:15:08 -0400288 clib_macro_init (&vam->macro_main);
289 clib_macro_add_builtin (&vam->macro_main, "current_file",
290 eval_current_file);
291 clib_macro_add_builtin (&vam->macro_main, "current_line",
292 eval_current_line);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
Dave Barach72d72232016-08-04 10:15:08 -0400294 init_error_string_table (vam);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295
Dave Barach72d72232016-08-04 10:15:08 -0400296 unformat_init_command_line (a, argv);
297
298 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
299 {
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))
303 ;
304 else if (unformat (a, "script"))
305 interactive = 0;
306 else if (unformat (a, "json"))
307 json_output = 1;
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))
314 {
315 vl_set_memory_root_path ((char *) chroot_prefix);
316 }
317 else
318 {
319 fformat (stderr,
320 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n");
321 exit (1);
322 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323 }
324
Dave Barach72d72232016-08-04 10:15:08 -0400325 if (output_file)
326 vam->ofp = fopen ((char *) output_file, "w");
327 else
328 vam->ofp = stdout;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Dave Barach72d72232016-08-04 10:15:08 -0400330 if (vam->ofp == NULL)
331 {
332 fformat (stderr, "Couldn't open output file %s\n",
333 output_file ? (char *) output_file : "stdout");
334 exit (1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335 }
336
Dave Barach72d72232016-08-04 10:15:08 -0400337 clib_time_init (&vam->clib_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
Dave Barach72d72232016-08-04 10:15:08 -0400339 vat_api_hookup (vam);
340 vat_plugin_api_reference ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341
Dave Barach477570e2016-10-09 17:43:22 -0400342 setup_signal_handlers ();
343
Dave Barach72d72232016-08-04 10:15:08 -0400344 if (connect_to_vpe ("vpp_api_test") < 0)
345 {
346 svm_region_exit ();
347 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
348 exit (1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349 }
350
Dave Barach72d72232016-08-04 10:15:08 -0400351 vam->json_output = json_output;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Dave Barach72d72232016-08-04 10:15:08 -0400353 if (!json_output)
354 {
355 api_sw_interface_dump (vam);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 }
357
Dave Barach72d72232016-08-04 10:15:08 -0400358 vec_validate (vam->inbuf, 4096);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359
Dave Barach72d72232016-08-04 10:15:08 -0400360 vam->current_file = (u8 *) "plugin-init";
361 vat_plugin_init (vam);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362
Dave Barach72d72232016-08-04 10:15:08 -0400363 for (i = 0; i < vec_len (input_files); i++)
364 {
365 vam->ifp = fopen ((char *) input_files[i], "r");
366 if (vam->ifp == NULL)
367 {
368 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
369 continue;
370 }
371 vam->current_file = input_files[i];
372 vam->input_line_number = 0;
373 do_one_file (vam);
374 fclose (vam->ifp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 }
376
Dave Barach72d72232016-08-04 10:15:08 -0400377 if (output_file)
378 fclose (vam->ofp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379
Dave Barach72d72232016-08-04 10:15:08 -0400380 if (interactive)
381 {
382 vam->ifp = stdin;
383 vam->ofp = stdout;
384 vam->current_file = (u8 *) "interactive";
385 do_one_file (vam);
386 fclose (vam->ifp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387 }
388
Dave Barach72d72232016-08-04 10:15:08 -0400389 vl_client_disconnect_from_vlib ();
390 exit (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391}
Dave Barach72d72232016-08-04 10:15:08 -0400392
393/*
394 * fd.io coding-style-patch-verification: ON
395 *
396 * Local Variables:
397 * eval: (c-set-style "gnu")
398 * End:
399 */