76c4d3f7c2a8eae8b12c0f53eba428d0a2693842
[vpp.git] / vpp-api-test / vat / main.c
1 /*
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"
17 #include <signal.h>
18
19 vat_main_t vat_main;
20
21 int
22 connect_to_vpe (char *name)
23 {
24   vat_main_t *vam = &vat_main;
25   api_main_t *am = &api_main;
26
27   if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
28     return -1;
29
30   vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
31   vam->my_client_index = am->my_client_index;
32
33   return 0;
34 }
35
36 vlib_main_t vlib_global_main;
37 vlib_main_t **vlib_mains;
38 void
39 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
40 {
41   clib_warning ("BUG");
42 }
43
44
45 static u8 *
46 format_api_error (u8 * s, va_list * args)
47 {
48   vat_main_t *vam = va_arg (*args, vat_main_t *);
49   i32 error = va_arg (*args, u32);
50   uword *p;
51
52   p = hash_get (vam->error_string_by_error_number, -error);
53
54   if (p)
55     s = format (s, "%s", p[0]);
56   else
57     s = format (s, "%d", error);
58   return s;
59 }
60
61 void
62 do_one_file (vat_main_t * vam)
63 {
64   int rv;
65   int (*fp) (vat_main_t * vam);
66   int arg_len;
67   unformat_input_t _input;
68   u8 *cmdp, *argsp;
69   uword *p;
70   u8 *this_cmd = 0;
71
72   vam->input = &_input;
73
74   /* Used by the "quit" command handler */
75   if (setjmp (vam->jump_buf) != 0)
76     return;
77
78   vam->jump_buf_set = 1;
79
80   while (1)
81     {
82       if (vam->ifp == stdin)
83         {
84           if (vam->exec_mode == 0)
85             rv = write (1, "vat# ", 5);
86           else
87             rv = write (1, "exec# ", 6);
88         }
89
90       _vec_len (vam->inbuf) = 4096;
91
92       if (vam->do_exit ||
93           fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
94         break;
95
96       vam->input_line_number++;
97
98       vec_free (this_cmd);
99
100       this_cmd =
101         (u8 *) clib_macro_eval (&vam->macro_main, (char *) vam->inbuf,
102                                 1 /* complain */ );
103
104       if (vam->exec_mode == 0)
105         {
106           /* Split input into cmd + args */
107           cmdp = this_cmd;
108
109           while (cmdp < (this_cmd + vec_len (this_cmd)))
110             {
111               if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
112                 {
113                   cmdp++;
114                 }
115               else
116                 break;
117             }
118           argsp = cmdp;
119           while (argsp < (this_cmd + vec_len (this_cmd)))
120             {
121               if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
122                 {
123                   argsp++;
124                 }
125               else
126                 break;
127             }
128           *argsp++ = 0;
129           while (argsp < (this_cmd + vec_len (this_cmd)))
130             {
131               if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
132                 {
133                   argsp++;
134                 }
135               else
136                 break;
137             }
138
139
140           /* Blank input line? */
141           if (*cmdp == 0)
142             continue;
143
144           p = hash_get_mem (vam->function_by_name, cmdp);
145           if (p == 0)
146             {
147               errmsg ("'%s': function not found\n", cmdp);
148               continue;
149             }
150
151           arg_len = strlen ((char *) argsp);
152
153           unformat_init_string (vam->input, (char *) argsp, arg_len);
154           fp = (void *) p[0];
155         }
156       else
157         {
158           unformat_init_string (vam->input, (char *) this_cmd,
159                                 strlen ((char *) this_cmd));
160           cmdp = this_cmd;
161           fp = exec;
162         }
163
164       rv = (*fp) (vam);
165       if (rv < 0)
166         errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
167       unformat_free (vam->input);
168
169       if (vam->regenerate_interface_table)
170         {
171           vam->regenerate_interface_table = 0;
172           api_sw_interface_dump (vam);
173         }
174     }
175 }
176
177 static void
178 init_error_string_table (vat_main_t * vam)
179 {
180
181   vam->error_string_by_error_number = hash_create (0, sizeof (uword));
182
183 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
184   foreach_vnet_api_error;
185 #undef _
186
187   hash_set (vam->error_string_by_error_number, 99, "Misc");
188 }
189
190 static i8 *
191 eval_current_file (macro_main_t * mm, i32 complain)
192 {
193   vat_main_t *vam = &vat_main;
194   return ((i8 *) format (0, "%s%c", vam->current_file, 0));
195 }
196
197 static i8 *
198 eval_current_line (macro_main_t * mm, i32 complain)
199 {
200   vat_main_t *vam = &vat_main;
201   return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
202 }
203
204 static void
205 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
206 {
207   vat_main_t *vam = &vat_main;
208
209   switch (signum)
210     {
211       /* these (caught) signals cause the application to exit */
212     case SIGINT:
213     case SIGTERM:
214       if (vam->jump_buf_set)
215         {
216           vam->do_exit = 1;
217           return;
218         }
219
220       /* FALLTHROUGH on purpose */
221
222     default:
223       break;
224     }
225
226   _exit (1);
227 }
228
229 static void
230 setup_signal_handlers (void)
231 {
232   uword i;
233   struct sigaction sa;
234
235   for (i = 1; i < 32; i++)
236     {
237       memset (&sa, 0, sizeof (sa));
238       sa.sa_sigaction = (void *) signal_handler;
239       sa.sa_flags = SA_SIGINFO;
240
241       switch (i)
242         {
243           /* these signals take the default action */
244         case SIGABRT:
245         case SIGKILL:
246         case SIGSTOP:
247         case SIGUSR1:
248         case SIGUSR2:
249           continue;
250
251           /* ignore SIGPIPE, SIGCHLD */
252         case SIGPIPE:
253         case SIGCHLD:
254           sa.sa_sigaction = (void *) SIG_IGN;
255           break;
256
257           /* catch and handle all other signals */
258         default:
259           break;
260         }
261
262       if (sigaction (i, &sa, 0) < 0)
263         clib_unix_warning ("sigaction %U", format_signal, i);
264     }
265 }
266
267 int
268 main (int argc, char **argv)
269 {
270   vat_main_t *vam = &vat_main;
271   unformat_input_t _argv, *a = &_argv;
272   u8 **input_files = 0;
273   u8 *output_file = 0;
274   u8 *chroot_prefix;
275   u8 *this_input_file;
276   u8 interactive = 1;
277   u8 json_output = 0;
278   u8 *heap;
279   mheap_t *h;
280   int i;
281
282   clib_mem_init (0, 128 << 20);
283
284   heap = clib_mem_get_per_cpu_heap ();
285   h = mheap_header (heap);
286
287   /* make the main heap thread-safe */
288   h->flags |= MHEAP_FLAG_THREAD_SAFE;
289
290   clib_macro_init (&vam->macro_main);
291   clib_macro_add_builtin (&vam->macro_main, "current_file",
292                           eval_current_file);
293   clib_macro_add_builtin (&vam->macro_main, "current_line",
294                           eval_current_line);
295
296   init_error_string_table (vam);
297
298   unformat_init_command_line (a, argv);
299
300   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
301     {
302       if (unformat (a, "in %s", &this_input_file))
303         vec_add1 (input_files, this_input_file);
304       else if (unformat (a, "out %s", &output_file))
305         ;
306       else if (unformat (a, "script"))
307         interactive = 0;
308       else if (unformat (a, "json"))
309         json_output = 1;
310       else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
311         vec_add1 (vat_plugin_path, 0);
312       else if (unformat (a, "plugin_name_filter %s",
313                          (u8 *) & vat_plugin_name_filter))
314         vec_add1 (vat_plugin_name_filter, 0);
315       else if (unformat (a, "chroot prefix %s", &chroot_prefix))
316         {
317           vl_set_memory_root_path ((char *) chroot_prefix);
318         }
319       else
320         {
321           fformat (stderr,
322                    "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n");
323           exit (1);
324         }
325     }
326
327   if (output_file)
328     vam->ofp = fopen ((char *) output_file, "w");
329   else
330     vam->ofp = stdout;
331
332   if (vam->ofp == NULL)
333     {
334       fformat (stderr, "Couldn't open output file %s\n",
335                output_file ? (char *) output_file : "stdout");
336       exit (1);
337     }
338
339   clib_time_init (&vam->clib_time);
340
341   vat_api_hookup (vam);
342   vat_plugin_api_reference ();
343
344   setup_signal_handlers ();
345
346   if (connect_to_vpe ("vpp_api_test") < 0)
347     {
348       svm_region_exit ();
349       fformat (stderr, "Couldn't connect to vpe, exiting...\n");
350       exit (1);
351     }
352
353   vam->json_output = json_output;
354
355   if (!json_output)
356     {
357       api_sw_interface_dump (vam);
358     }
359
360   vec_validate (vam->inbuf, 4096);
361
362   vam->current_file = (u8 *) "plugin-init";
363   vat_plugin_init (vam);
364
365   for (i = 0; i < vec_len (input_files); i++)
366     {
367       vam->ifp = fopen ((char *) input_files[i], "r");
368       if (vam->ifp == NULL)
369         {
370           fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
371           continue;
372         }
373       vam->current_file = input_files[i];
374       vam->input_line_number = 0;
375       do_one_file (vam);
376       fclose (vam->ifp);
377     }
378
379   if (output_file)
380     fclose (vam->ofp);
381
382   if (interactive)
383     {
384       vam->ifp = stdin;
385       vam->ofp = stdout;
386       vam->current_file = (u8 *) "interactive";
387       do_one_file (vam);
388       fclose (vam->ifp);
389     }
390
391   vl_client_disconnect_from_vlib ();
392   exit (0);
393 }
394
395 /*
396  * fd.io coding-style-patch-verification: ON
397  *
398  * Local Variables:
399  * eval: (c-set-style "gnu")
400  * End:
401  */