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