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