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