blob: 139594944d2d30f4803020d7ea3bfa185d534c2d [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:
Jieqiang Wang6f533d72020-01-20 13:43:38 +0800269 case SIGPROF:
Damjan Marion7cd468a2016-12-19 23:05:39 +0100270 continue;
271
272 /* ignore SIGPIPE, SIGCHLD */
273 case SIGPIPE:
274 case SIGCHLD:
ezkexma7d3161a2019-03-26 10:24:38 -0400275 case SIGWINCH:
Damjan Marion7cd468a2016-12-19 23:05:39 +0100276 sa.sa_sigaction = (void *) SIG_IGN;
277 break;
278
279 /* catch and handle all other signals */
280 default:
281 break;
282 }
283
284 if (sigaction (i, &sa, 0) < 0)
285 clib_unix_warning ("sigaction %U", format_signal, i);
286 }
287}
288
Damjan Marion4d2f86a2019-01-18 13:28:22 +0100289static void
290vat_find_plugin_path ()
291{
Damjan Marion4d2f86a2019-01-18 13:28:22 +0100292 char *p, path[PATH_MAX];
293 int rv;
294 u8 *s;
295
296 /* find executable path */
297 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
298 return;
299
300 /* readlink doesn't provide null termination */
301 path[rv] = 0;
302
303 /* strip filename */
304 if ((p = strrchr (path, '/')) == 0)
305 return;
306 *p = 0;
307
308 /* strip bin/ */
309 if ((p = strrchr (path, '/')) == 0)
310 return;
311 *p = 0;
312
313 s = format (0, "%s/lib/" CLIB_TARGET_TRIPLET "/vpp_api_test_plugins:"
314 "%s/lib/vpp_api_test_plugins", path, path);
315 vec_add1 (s, 0);
316 vat_plugin_path = (char *) s;
317}
318
Damjan Marion7cd468a2016-12-19 23:05:39 +0100319int
320main (int argc, char **argv)
321{
322 vat_main_t *vam = &vat_main;
323 unformat_input_t _argv, *a = &_argv;
324 u8 **input_files = 0;
325 u8 *output_file = 0;
326 u8 *chroot_prefix;
327 u8 *this_input_file;
328 u8 interactive = 1;
329 u8 json_output = 0;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100330 int i;
Dave Barachcf5e8482017-10-17 11:48:29 -0400331 f64 timeout;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100332
Dave Barach6a5adc32018-07-04 10:56:23 -0400333 clib_mem_init_thread_safe (0, 128 << 20);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100334
335 clib_macro_init (&vam->macro_main);
336 clib_macro_add_builtin (&vam->macro_main, "current_file",
337 eval_current_file);
338 clib_macro_add_builtin (&vam->macro_main, "current_line",
339 eval_current_line);
340
341 init_error_string_table (vam);
Dave Barach59b25652017-09-10 15:04:27 -0400342 vec_validate (vam->cmd_reply, 0);
343 vec_reset_length (vam->cmd_reply);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100344
Damjan Marion4d2f86a2019-01-18 13:28:22 +0100345 vat_find_plugin_path ();
346
Damjan Marion7cd468a2016-12-19 23:05:39 +0100347 unformat_init_command_line (a, argv);
348
349 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
350 {
351 if (unformat (a, "in %s", &this_input_file))
352 vec_add1 (input_files, this_input_file);
353 else if (unformat (a, "out %s", &output_file))
354 ;
355 else if (unformat (a, "script"))
356 interactive = 0;
357 else if (unformat (a, "json"))
358 json_output = 1;
Dave Barach59b25652017-09-10 15:04:27 -0400359 else if (unformat (a, "socket-name %s", &vam->socket_name))
360 ;
361 else if (unformat (a, "default-socket"))
362 {
363 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
364 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100365 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
366 vec_add1 (vat_plugin_path, 0);
367 else if (unformat (a, "plugin_name_filter %s",
368 (u8 *) & vat_plugin_name_filter))
369 vec_add1 (vat_plugin_name_filter, 0);
370 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
371 {
372 vl_set_memory_root_path ((char *) chroot_prefix);
373 }
374 else
375 {
Dave Barach59b25652017-09-10 15:04:27 -0400376 fformat
377 (stderr,
378 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
379 "[plugin_path <path>][default-socket][socket-name <name>]\n"
380 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
381 argv[0]);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100382 exit (1);
383 }
384 }
385
386 if (output_file)
387 vam->ofp = fopen ((char *) output_file, "w");
388 else
389 vam->ofp = stdout;
390
391 if (vam->ofp == NULL)
392 {
393 fformat (stderr, "Couldn't open output file %s\n",
394 output_file ? (char *) output_file : "stdout");
395 exit (1);
396 }
397
398 clib_time_init (&vam->clib_time);
399
400 vat_api_hookup (vam);
401 vat_plugin_api_reference ();
402
403 setup_signal_handlers ();
404
Dave Barach59b25652017-09-10 15:04:27 -0400405 if (vam->socket_name && vat_socket_connect (vam))
406 fformat (stderr, "WARNING: socket connection failed");
407
Florin Coras90a63982017-12-19 04:50:01 -0800408 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
Dave Barach59b25652017-09-10 15:04:27 -0400409 && connect_to_vpe ("vpp_api_test") < 0)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100410 {
411 svm_region_exit ();
412 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
413 exit (1);
414 }
415
416 vam->json_output = json_output;
417
418 if (!json_output)
Dave Barach59b25652017-09-10 15:04:27 -0400419 api_sw_interface_dump (vam);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100420
421 vec_validate (vam->inbuf, 4096);
422
423 vam->current_file = (u8 *) "plugin-init";
424 vat_plugin_init (vam);
425
426 for (i = 0; i < vec_len (input_files); i++)
427 {
428 vam->ifp = fopen ((char *) input_files[i], "r");
429 if (vam->ifp == NULL)
430 {
431 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
432 continue;
433 }
434 vam->current_file = input_files[i];
435 vam->input_line_number = 0;
436 do_one_file (vam);
437 fclose (vam->ifp);
438 }
439
440 if (output_file)
441 fclose (vam->ofp);
442
443 if (interactive)
444 {
445 vam->ifp = stdin;
446 vam->ofp = stdout;
447 vam->current_file = (u8 *) "interactive";
448 do_one_file (vam);
449 fclose (vam->ifp);
450 }
451
Dave Barachcf5e8482017-10-17 11:48:29 -0400452 /*
453 * Particularly when running a script, don't be in a hurry to leave.
454 * A reply message queued to this process will end up constipating
455 * the allocation rings.
456 */
457 timeout = vat_time_now (vam) + 2.0;
458 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
459 ;
460
461 if (vat_time_now (vam) > timeout)
462 clib_warning ("BUG: message reply spin-wait timeout");
463
Damjan Marion7cd468a2016-12-19 23:05:39 +0100464 vl_client_disconnect_from_vlib ();
465 exit (0);
466}
467
468/*
469 * fd.io coding-style-patch-verification: ON
470 *
471 * Local Variables:
472 * eval: (c-set-style "gnu")
473 * End:
474 */