vlib: debug CLI macro expander, part deux
[vpp.git] / src / 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 #include <limits.h>
19
20 vat_main_t vat_main;
21
22 #include <vlibapi/api_helper_macros.h>
23
24 void
25 vat_suspend (vlib_main_t * vm, f64 interval)
26 {
27   /* do nothing in the standalone version, just return */
28 }
29
30 int
31 connect_to_vpe (char *name)
32 {
33   vat_main_t *vam = &vat_main;
34   api_main_t *am = vlibapi_get_main ();
35
36   if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
37     return -1;
38
39   vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
40   vam->my_client_index = am->my_client_index;
41
42   return 0;
43 }
44
45 vlib_main_t vlib_global_main;
46 vlib_main_t **vlib_mains;
47 void
48 vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
49 {
50   clib_warning ("BUG");
51 }
52
53
54 static u8 *
55 format_api_error (u8 * s, va_list * args)
56 {
57   vat_main_t *vam = va_arg (*args, vat_main_t *);
58   i32 error = va_arg (*args, u32);
59   uword *p;
60
61   p = hash_get (vam->error_string_by_error_number, -error);
62
63   if (p)
64     s = format (s, "%s", p[0]);
65   else
66     s = format (s, "%d", error);
67   return s;
68 }
69
70 void
71 do_one_file (vat_main_t * vam)
72 {
73   int rv;
74   int (*fp) (vat_main_t * vam);
75   int arg_len;
76   unformat_input_t _input;
77   u8 *cmdp, *argsp;
78   uword *p;
79   u8 *this_cmd = 0;
80
81   vam->input = &_input;
82
83   /* Used by the "quit" command handler */
84   if (setjmp (vam->jump_buf) != 0)
85     return;
86
87   vam->jump_buf_set = 1;
88
89   while (1)
90     {
91       if (vam->ifp == stdin)
92         {
93           if (vam->exec_mode == 0)
94             rv = write (1, "vat# ", 5);
95           else
96             rv = write (1, "exec# ", 6);
97         }
98
99       _vec_len (vam->inbuf) = 4096;
100
101       if (vam->do_exit ||
102           fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
103         break;
104
105       vam->input_line_number++;
106
107       vec_free (this_cmd);
108
109       this_cmd =
110         (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf,
111                                 1 /* complain */ ,
112                                 0 /* level */ ,
113                                 8 /* max_level */ );
114
115       if (vam->exec_mode == 0)
116         {
117           /* Split input into cmd + args */
118           cmdp = this_cmd;
119
120           while (cmdp < (this_cmd + vec_len (this_cmd)))
121             {
122               if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
123                 {
124                   cmdp++;
125                 }
126               else
127                 break;
128             }
129           argsp = cmdp;
130           while (argsp < (this_cmd + vec_len (this_cmd)))
131             {
132               if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
133                 {
134                   argsp++;
135                 }
136               else
137                 break;
138             }
139           *argsp++ = 0;
140           while (argsp < (this_cmd + vec_len (this_cmd)))
141             {
142               if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
143                 {
144                   argsp++;
145                 }
146               else
147                 break;
148             }
149
150
151           /* Blank input line? */
152           if (*cmdp == 0)
153             continue;
154
155           p = hash_get_mem (vam->function_by_name, cmdp);
156           if (p == 0)
157             {
158               errmsg ("'%s': function not found\n", cmdp);
159               continue;
160             }
161
162           arg_len = strlen ((char *) argsp);
163
164           unformat_init_string (vam->input, (char *) argsp, arg_len);
165           fp = (void *) p[0];
166         }
167       else
168         {
169           unformat_init_string (vam->input, (char *) this_cmd,
170                                 strlen ((char *) this_cmd));
171           cmdp = this_cmd;
172           fp = exec;
173         }
174
175       rv = (*fp) (vam);
176       if (rv < 0)
177         errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
178       unformat_free (vam->input);
179
180       if (vam->regenerate_interface_table)
181         {
182           vam->regenerate_interface_table = 0;
183           api_sw_interface_dump (vam);
184         }
185
186       /* Hack to pick up new client index after memfd_segment_create pivot */
187       if (vam->client_index_invalid)
188         {
189           vat_main_t *vam = &vat_main;
190           api_main_t *am = vlibapi_get_main ();
191
192           vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
193           vam->my_client_index = am->my_client_index;
194           vam->client_index_invalid = 0;
195         }
196     }
197 }
198
199 static void
200 init_error_string_table (vat_main_t * vam)
201 {
202
203   vam->error_string_by_error_number = hash_create (0, sizeof (uword));
204
205 #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
206   foreach_vnet_api_error;
207 #undef _
208
209   hash_set (vam->error_string_by_error_number, 99, "Misc");
210 }
211
212 static i8 *
213 eval_current_file (clib_macro_main_t * mm, i32 complain)
214 {
215   vat_main_t *vam = &vat_main;
216   return ((i8 *) format (0, "%s%c", vam->current_file, 0));
217 }
218
219 static i8 *
220 eval_current_line (clib_macro_main_t * mm, i32 complain)
221 {
222   vat_main_t *vam = &vat_main;
223   return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
224 }
225
226 static void
227 signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
228 {
229   vat_main_t *vam = &vat_main;
230
231   switch (signum)
232     {
233       /* these (caught) signals cause the application to exit */
234     case SIGINT:
235     case SIGTERM:
236       if (vam->jump_buf_set)
237         {
238           vam->do_exit = 1;
239           return;
240         }
241
242       /* FALLTHROUGH on purpose */
243
244     default:
245       break;
246     }
247
248   _exit (1);
249 }
250
251 static void
252 setup_signal_handlers (void)
253 {
254   uword i;
255   struct sigaction sa;
256
257   for (i = 1; i < 32; i++)
258     {
259       clib_memset (&sa, 0, sizeof (sa));
260       sa.sa_sigaction = (void *) signal_handler;
261       sa.sa_flags = SA_SIGINFO;
262
263       switch (i)
264         {
265           /* these signals take the default action */
266         case SIGABRT:
267         case SIGKILL:
268         case SIGSTOP:
269         case SIGUSR1:
270         case SIGUSR2:
271         case SIGPROF:
272           continue;
273
274           /* ignore SIGPIPE, SIGCHLD */
275         case SIGPIPE:
276         case SIGCHLD:
277         case SIGWINCH:
278           sa.sa_sigaction = (void *) SIG_IGN;
279           break;
280
281           /* catch and handle all other signals */
282         default:
283           break;
284         }
285
286       if (sigaction (i, &sa, 0) < 0)
287         clib_unix_warning ("sigaction %U", format_signal, i);
288     }
289 }
290
291 static void
292 vat_find_plugin_path ()
293 {
294   char *p, path[PATH_MAX];
295   int rv;
296   u8 *s;
297
298   /* find executable path */
299   if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
300     return;
301
302   /* readlink doesn't provide null termination */
303   path[rv] = 0;
304
305   /* strip filename */
306   if ((p = strrchr (path, '/')) == 0)
307     return;
308   *p = 0;
309
310   /* strip bin/ */
311   if ((p = strrchr (path, '/')) == 0)
312     return;
313   *p = 0;
314
315   s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
316               "%s/lib/vpp_api_test_plugins", path, path);
317   vec_add1 (s, 0);
318   vat_plugin_path = (char *) s;
319 }
320
321 int
322 main (int argc, char **argv)
323 {
324   vat_main_t *vam = &vat_main;
325   unformat_input_t _argv, *a = &_argv;
326   u8 **input_files = 0;
327   u8 *output_file = 0;
328   u8 *chroot_prefix;
329   u8 *this_input_file;
330   u8 interactive = 1;
331   u8 json_output = 0;
332   int i;
333   f64 timeout;
334
335   clib_mem_init_thread_safe (0, 128 << 20);
336
337   clib_macro_init (&vam->macro_main);
338   clib_macro_add_builtin (&vam->macro_main, "current_file",
339                           eval_current_file);
340   clib_macro_add_builtin (&vam->macro_main, "current_line",
341                           eval_current_line);
342
343   init_error_string_table (vam);
344   vec_validate (vam->cmd_reply, 0);
345   vec_reset_length (vam->cmd_reply);
346
347   vat_find_plugin_path ();
348
349   unformat_init_command_line (a, argv);
350
351   while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
352     {
353       if (unformat (a, "in %s", &this_input_file))
354         vec_add1 (input_files, this_input_file);
355       else if (unformat (a, "out %s", &output_file))
356         ;
357       else if (unformat (a, "script"))
358         interactive = 0;
359       else if (unformat (a, "json"))
360         json_output = 1;
361       else if (unformat (a, "socket-name %s", &vam->socket_name))
362         ;
363       else if (unformat (a, "default-socket"))
364         {
365           vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
366         }
367       else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
368         vec_add1 (vat_plugin_path, 0);
369       else if (unformat (a, "plugin_name_filter %s",
370                          (u8 *) & vat_plugin_name_filter))
371         vec_add1 (vat_plugin_name_filter, 0);
372       else if (unformat (a, "chroot prefix %s", &chroot_prefix))
373         {
374           vl_set_memory_root_path ((char *) chroot_prefix);
375         }
376       else
377         {
378           fformat
379             (stderr,
380              "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
381              "[plugin_path <path>][default-socket][socket-name <name>]\n"
382              "[plugin_name_filter <filter>][chroot prefix <path>]\n",
383              argv[0]);
384           exit (1);
385         }
386     }
387
388   if (output_file)
389     vam->ofp = fopen ((char *) output_file, "w");
390   else
391     vam->ofp = stdout;
392
393   if (vam->ofp == NULL)
394     {
395       fformat (stderr, "Couldn't open output file %s\n",
396                output_file ? (char *) output_file : "stdout");
397       exit (1);
398     }
399
400   clib_time_init (&vam->clib_time);
401
402   vat_api_hookup (vam);
403   vat_plugin_api_reference ();
404
405   setup_signal_handlers ();
406
407   if (vam->socket_name && vat_socket_connect (vam))
408     fformat (stderr, "WARNING: socket connection failed");
409
410   if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
411       && connect_to_vpe ("vpp_api_test") < 0)
412     {
413       svm_region_exit ();
414       fformat (stderr, "Couldn't connect to vpe, exiting...\n");
415       exit (1);
416     }
417
418   vam->json_output = json_output;
419
420   if (!json_output)
421     api_sw_interface_dump (vam);
422
423   vec_validate (vam->inbuf, 4096);
424
425   vam->current_file = (u8 *) "plugin-init";
426   vat_plugin_init (vam);
427
428   for (i = 0; i < vec_len (input_files); i++)
429     {
430       vam->ifp = fopen ((char *) input_files[i], "r");
431       if (vam->ifp == NULL)
432         {
433           fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
434           continue;
435         }
436       vam->current_file = input_files[i];
437       vam->input_line_number = 0;
438       do_one_file (vam);
439       fclose (vam->ifp);
440     }
441
442   if (output_file)
443     fclose (vam->ofp);
444
445   if (interactive)
446     {
447       vam->ifp = stdin;
448       vam->ofp = stdout;
449       vam->current_file = (u8 *) "interactive";
450       do_one_file (vam);
451       fclose (vam->ifp);
452     }
453
454   /*
455    * Particularly when running a script, don't be in a hurry to leave.
456    * A reply message queued to this process will end up constipating
457    * the allocation rings.
458    */
459   timeout = vat_time_now (vam) + 2.0;
460   while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
461     ;
462
463   if (vat_time_now (vam) > timeout)
464     clib_warning ("BUG: message reply spin-wait timeout");
465
466   vl_client_disconnect_from_vlib ();
467   exit (0);
468 }
469
470 /*
471  * fd.io coding-style-patch-verification: ON
472  *
473  * Local Variables:
474  * eval: (c-set-style "gnu")
475  * End:
476  */