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