blob: 996fb1ed6a8928c7470cf520fc0aeb5a235457dd [file] [log] [blame]
Damjan Marion7cd468a2016-12-19 23:05:39 +01001/*
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>
Damjan Marion4d2f86a2019-01-18 13:28:22 +010018#include <limits.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010019
20vat_main_t vat_main;
21
22#include <vlibapi/api_helper_macros.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010023
24void
25vat_suspend (vlib_main_t * vm, f64 interval)
26{
27 /* do nothing in the standalone version, just return */
28}
29
Damjan Marion7cd468a2016-12-19 23:05:39 +010030int
31connect_to_vpe (char *name)
32{
33 vat_main_t *vam = &vat_main;
Dave Barach39d69112019-11-27 11:42:13 -050034 api_main_t *am = vlibapi_get_main ();
Damjan Marion7cd468a2016-12-19 23:05:39 +010035
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
45vlib_main_t vlib_global_main;
46vlib_main_t **vlib_mains;
47void
48vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
49{
50 clib_warning ("BUG");
51}
52
53
54static u8 *
55format_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
70void
71do_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 =
Damjan Marionffe9d212018-05-30 09:26:11 +0200110 (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf,
Damjan Marion7cd468a2016-12-19 23:05:39 +0100111 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 }
Dave Barach59b25652017-09-10 15:04:27 -0400183
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;
Dave Barach39d69112019-11-27 11:42:13 -0500188 api_main_t *am = vlibapi_get_main ();
Dave Barach59b25652017-09-10 15:04:27 -0400189
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 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100194 }
195}
196
197static void
198init_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
210static i8 *
211eval_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
217static i8 *
218eval_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
224static void
225signal_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
249static void
250setup_signal_handlers (void)
251{
252 uword i;
253 struct sigaction sa;
254
255 for (i = 1; i < 32; i++)
256 {
Dave Barachb7b92992018-10-17 10:38:51 -0400257 clib_memset (&sa, 0, sizeof (sa));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100258 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:
ezkexma7d3161a2019-03-26 10:24:38 -0400274 case SIGWINCH:
Damjan Marion7cd468a2016-12-19 23:05:39 +0100275 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
Damjan Marion4d2f86a2019-01-18 13:28:22 +0100288static void
289vat_find_plugin_path ()
290{
Damjan Marion4d2f86a2019-01-18 13:28:22 +0100291 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
Damjan Marion7cd468a2016-12-19 23:05:39 +0100318int
319main (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;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100329 int i;
Dave Barachcf5e8482017-10-17 11:48:29 -0400330 f64 timeout;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100331
Dave Barach6a5adc32018-07-04 10:56:23 -0400332 clib_mem_init_thread_safe (0, 128 << 20);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100333
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);
Dave Barach59b25652017-09-10 15:04:27 -0400341 vec_validate (vam->cmd_reply, 0);
342 vec_reset_length (vam->cmd_reply);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100343
Damjan Marion4d2f86a2019-01-18 13:28:22 +0100344 vat_find_plugin_path ();
345
Damjan Marion7cd468a2016-12-19 23:05:39 +0100346 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;
Dave Barach59b25652017-09-10 15:04:27 -0400358 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 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100364 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 {
Dave Barach59b25652017-09-10 15:04:27 -0400375 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]);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100381 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
Dave Barach59b25652017-09-10 15:04:27 -0400404 if (vam->socket_name && vat_socket_connect (vam))
405 fformat (stderr, "WARNING: socket connection failed");
406
Florin Coras90a63982017-12-19 04:50:01 -0800407 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
Dave Barach59b25652017-09-10 15:04:27 -0400408 && connect_to_vpe ("vpp_api_test") < 0)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100409 {
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)
Dave Barach59b25652017-09-10 15:04:27 -0400418 api_sw_interface_dump (vam);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100419
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
Dave Barachcf5e8482017-10-17 11:48:29 -0400451 /*
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
Damjan Marion7cd468a2016-12-19 23:05:39 +0100463 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 */