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