blob: d052493495da8c8a1ce4da3603e3afaf7130e007 [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"
Filip Tehlarf0e67d72021-07-23 22:03:05 +000016#include <dlfcn.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010017#include "plugin.h"
18#include <signal.h>
Damjan Marion4d2f86a2019-01-18 13:28:22 +010019#include <limits.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010020
21vat_main_t vat_main;
22
23#include <vlibapi/api_helper_macros.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010024
25void
26vat_suspend (vlib_main_t * vm, f64 interval)
27{
28 /* do nothing in the standalone version, just return */
29}
30
Damjan Marion7cd468a2016-12-19 23:05:39 +010031int
32connect_to_vpe (char *name)
33{
34 vat_main_t *vam = &vat_main;
Dave Barach39d69112019-11-27 11:42:13 -050035 api_main_t *am = vlibapi_get_main ();
Damjan Marion7cd468a2016-12-19 23:05:39 +010036
37 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
38 return -1;
39
40 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
41 vam->my_client_index = am->my_client_index;
42
43 return 0;
44}
45
Jon Loeligerc0b19542020-05-11 08:43:51 -050046/* *INDENT-OFF* */
47
Damjan Marionfd8deb42021-03-06 12:26:28 +010048vlib_global_main_t vlib_global_main;
Jon Loeligerc0b19542020-05-11 08:43:51 -050049
Damjan Marion7cd468a2016-12-19 23:05:39 +010050void
51vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
52{
53 clib_warning ("BUG");
54}
55
Damjan Marion7cd468a2016-12-19 23:05:39 +010056static u8 *
57format_api_error (u8 * s, va_list * args)
58{
59 vat_main_t *vam = va_arg (*args, vat_main_t *);
60 i32 error = va_arg (*args, u32);
61 uword *p;
62
63 p = hash_get (vam->error_string_by_error_number, -error);
64
65 if (p)
66 s = format (s, "%s", p[0]);
67 else
68 s = format (s, "%d", error);
69 return s;
70}
71
72void
73do_one_file (vat_main_t * vam)
74{
75 int rv;
76 int (*fp) (vat_main_t * vam);
77 int arg_len;
78 unformat_input_t _input;
79 u8 *cmdp, *argsp;
80 uword *p;
81 u8 *this_cmd = 0;
82
83 vam->input = &_input;
84
85 /* Used by the "quit" command handler */
86 if (setjmp (vam->jump_buf) != 0)
87 return;
88
89 vam->jump_buf_set = 1;
90
91 while (1)
92 {
93 if (vam->ifp == stdin)
94 {
95 if (vam->exec_mode == 0)
96 rv = write (1, "vat# ", 5);
97 else
98 rv = write (1, "exec# ", 6);
99 }
100
101 _vec_len (vam->inbuf) = 4096;
102
103 if (vam->do_exit ||
104 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
105 break;
106
107 vam->input_line_number++;
108
109 vec_free (this_cmd);
110
111 this_cmd =
Damjan Marionffe9d212018-05-30 09:26:11 +0200112 (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf,
Dave Barachb30b9542020-06-22 10:02:25 -0400113 1 /* complain */ ,
114 0 /* level */ ,
115 8 /* max_level */ );
Damjan Marion7cd468a2016-12-19 23:05:39 +0100116
117 if (vam->exec_mode == 0)
118 {
119 /* Split input into cmd + args */
120 cmdp = this_cmd;
121
122 while (cmdp < (this_cmd + vec_len (this_cmd)))
123 {
124 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
125 {
126 cmdp++;
127 }
128 else
129 break;
130 }
131 argsp = cmdp;
132 while (argsp < (this_cmd + vec_len (this_cmd)))
133 {
134 if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n')
135 {
136 argsp++;
137 }
138 else
139 break;
140 }
141 *argsp++ = 0;
142 while (argsp < (this_cmd + vec_len (this_cmd)))
143 {
144 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
145 {
146 argsp++;
147 }
148 else
149 break;
150 }
151
152
153 /* Blank input line? */
154 if (*cmdp == 0)
155 continue;
156
157 p = hash_get_mem (vam->function_by_name, cmdp);
158 if (p == 0)
159 {
160 errmsg ("'%s': function not found\n", cmdp);
161 continue;
162 }
163
164 arg_len = strlen ((char *) argsp);
165
166 unformat_init_string (vam->input, (char *) argsp, arg_len);
167 fp = (void *) p[0];
168 }
169 else
170 {
171 unformat_init_string (vam->input, (char *) this_cmd,
172 strlen ((char *) this_cmd));
173 cmdp = this_cmd;
174 fp = exec;
175 }
176
177 rv = (*fp) (vam);
178 if (rv < 0)
179 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
180 unformat_free (vam->input);
181
182 if (vam->regenerate_interface_table)
183 {
184 vam->regenerate_interface_table = 0;
Filip Tehlarf0e67d72021-07-23 22:03:05 +0000185 vam->api_sw_interface_dump (vam);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100186 }
Dave Barach59b25652017-09-10 15:04:27 -0400187
188 /* Hack to pick up new client index after memfd_segment_create pivot */
189 if (vam->client_index_invalid)
190 {
191 vat_main_t *vam = &vat_main;
Dave Barach39d69112019-11-27 11:42:13 -0500192 api_main_t *am = vlibapi_get_main ();
Dave Barach59b25652017-09-10 15:04:27 -0400193
194 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
195 vam->my_client_index = am->my_client_index;
196 vam->client_index_invalid = 0;
197 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100198 }
199}
200
201static void
202init_error_string_table (vat_main_t * vam)
203{
204
205 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
206
207#define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
208 foreach_vnet_api_error;
209#undef _
210
211 hash_set (vam->error_string_by_error_number, 99, "Misc");
212}
213
214static i8 *
Dave Barach961e3c82020-06-18 17:04:18 -0400215eval_current_file (clib_macro_main_t * mm, i32 complain)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100216{
217 vat_main_t *vam = &vat_main;
218 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
219}
220
221static i8 *
Dave Barach961e3c82020-06-18 17:04:18 -0400222eval_current_line (clib_macro_main_t * mm, i32 complain)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100223{
224 vat_main_t *vam = &vat_main;
225 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
226}
227
228static void
229signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
230{
231 vat_main_t *vam = &vat_main;
232
233 switch (signum)
234 {
235 /* these (caught) signals cause the application to exit */
236 case SIGINT:
237 case SIGTERM:
238 if (vam->jump_buf_set)
239 {
240 vam->do_exit = 1;
241 return;
242 }
243
244 /* FALLTHROUGH on purpose */
245
246 default:
247 break;
248 }
249
250 _exit (1);
251}
252
253static void
254setup_signal_handlers (void)
255{
256 uword i;
257 struct sigaction sa;
258
259 for (i = 1; i < 32; i++)
260 {
Dave Barachb7b92992018-10-17 10:38:51 -0400261 clib_memset (&sa, 0, sizeof (sa));
Damjan Marion7cd468a2016-12-19 23:05:39 +0100262 sa.sa_sigaction = (void *) signal_handler;
263 sa.sa_flags = SA_SIGINFO;
264
265 switch (i)
266 {
267 /* these signals take the default action */
268 case SIGABRT:
269 case SIGKILL:
270 case SIGSTOP:
271 case SIGUSR1:
272 case SIGUSR2:
Jieqiang Wang6f533d72020-01-20 13:43:38 +0800273 case SIGPROF:
Damjan Marion7cd468a2016-12-19 23:05:39 +0100274 continue;
275
276 /* ignore SIGPIPE, SIGCHLD */
277 case SIGPIPE:
278 case SIGCHLD:
ezkexma7d3161a2019-03-26 10:24:38 -0400279 case SIGWINCH:
Damjan Marion7cd468a2016-12-19 23:05:39 +0100280 sa.sa_sigaction = (void *) SIG_IGN;
281 break;
282
283 /* catch and handle all other signals */
284 default:
285 break;
286 }
287
288 if (sigaction (i, &sa, 0) < 0)
289 clib_unix_warning ("sigaction %U", format_signal, i);
290 }
291}
292
Damjan Marion4d2f86a2019-01-18 13:28:22 +0100293static void
294vat_find_plugin_path ()
295{
Damjan Marion4d2f86a2019-01-18 13:28:22 +0100296 char *p, path[PATH_MAX];
297 int rv;
298 u8 *s;
299
300 /* find executable path */
301 if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
302 return;
303
304 /* readlink doesn't provide null termination */
305 path[rv] = 0;
306
307 /* strip filename */
308 if ((p = strrchr (path, '/')) == 0)
309 return;
310 *p = 0;
311
312 /* strip bin/ */
313 if ((p = strrchr (path, '/')) == 0)
314 return;
315 *p = 0;
316
Damjan Marion5546e432021-09-30 20:04:14 +0200317 s = format (0, "%s/" CLIB_LIB_DIR "/vpp_api_test_plugins", path, path);
Damjan Marion4d2f86a2019-01-18 13:28:22 +0100318 vec_add1 (s, 0);
319 vat_plugin_path = (char *) s;
320}
321
Ole Troan3d812672020-09-15 10:53:34 +0200322static void
323load_features (void)
324{
325 vat_registered_features_t *f;
326 vat_main_t *vam = &vat_main;
327 clib_error_t *error;
328
329 f = vam->feature_function_registrations;
330
331 while (f)
332 {
333 error = f->function (vam);
334 if (error)
335 {
336 clib_warning ("INIT FAILED");
337 }
338 f = f->next;
339 }
340}
341
Jon Loeligerc0b19542020-05-11 08:43:51 -0500342static inline clib_error_t *
Damjan Marionfd8deb42021-03-06 12:26:28 +0100343call_init_exit_functions_internal (vlib_main_t *vm,
344 _vlib_init_function_list_elt_t **headp,
345 int call_once, int do_sort, int is_global)
Jon Loeligerc0b19542020-05-11 08:43:51 -0500346{
Damjan Marionfd8deb42021-03-06 12:26:28 +0100347 vlib_global_main_t *vgm = vlib_get_global_main ();
Jon Loeligerc0b19542020-05-11 08:43:51 -0500348 clib_error_t *error = 0;
349 _vlib_init_function_list_elt_t *i;
350
Damjan Marionfd8deb42021-03-06 12:26:28 +0100351 ASSERT (is_global == 1);
352
Jon Loeligerc0b19542020-05-11 08:43:51 -0500353#if 0
354 /* Not worth copying the topological sort code */
355 if (do_sort && (error = vlib_sort_init_exit_functions (headp)))
356 return (error);
357#endif
358
359 i = *headp;
360 while (i)
361 {
Damjan Marionfd8deb42021-03-06 12:26:28 +0100362 if (call_once && !hash_get (vgm->init_functions_called, i->f))
Jon Loeligerc0b19542020-05-11 08:43:51 -0500363 {
364 if (call_once)
Damjan Marionfd8deb42021-03-06 12:26:28 +0100365 hash_set1 (vgm->init_functions_called, i->f);
Jon Loeligerc0b19542020-05-11 08:43:51 -0500366 error = i->f (vm);
367 if (error)
368 return error;
369 }
370 i = i->next_init_function;
371 }
372 return error;
373}
374
375clib_error_t *
Damjan Marionfd8deb42021-03-06 12:26:28 +0100376vlib_call_init_exit_functions (vlib_main_t *vm,
377 _vlib_init_function_list_elt_t **headp,
378 int call_once, int is_global)
Jon Loeligerc0b19542020-05-11 08:43:51 -0500379{
380 return call_init_exit_functions_internal (vm, headp, call_once,
Damjan Marionfd8deb42021-03-06 12:26:28 +0100381 1 /* do_sort */, is_global);
Jon Loeligerc0b19542020-05-11 08:43:51 -0500382}
383
Filip Tehlarf0e67d72021-07-23 22:03:05 +0000384static void
385vat_register_interface_dump (vat_main_t *vam)
386{
387 void *handle;
388 plugin_info_t *pi;
389
390 vec_foreach (pi, vat_plugin_main.plugin_info)
391 {
392 handle = dlsym (pi->handle, "api_sw_interface_dump");
393 if (handle)
394 {
395 vam->api_sw_interface_dump = handle;
396 break;
397 }
398 }
399
400 if (!vam->api_sw_interface_dump)
401 {
402 fformat (stderr,
403 "sw_interface_dump not found in interface_test plugin!\n");
404 exit (1);
405 }
406}
407
Damjan Marion7cd468a2016-12-19 23:05:39 +0100408int
409main (int argc, char **argv)
410{
Damjan Marionfd8deb42021-03-06 12:26:28 +0100411 vlib_global_main_t *vgm = vlib_get_global_main ();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100412 vat_main_t *vam = &vat_main;
413 unformat_input_t _argv, *a = &_argv;
414 u8 **input_files = 0;
415 u8 *output_file = 0;
416 u8 *chroot_prefix;
417 u8 *this_input_file;
418 u8 interactive = 1;
419 u8 json_output = 0;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100420 int i;
Dave Barachcf5e8482017-10-17 11:48:29 -0400421 f64 timeout;
Jon Loeligerc0b19542020-05-11 08:43:51 -0500422 clib_error_t *error;
Damjan Marionfd8deb42021-03-06 12:26:28 +0100423 vlib_main_t *vm;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100424
Dave Barach6a5adc32018-07-04 10:56:23 -0400425 clib_mem_init_thread_safe (0, 128 << 20);
Damjan Marionfd8deb42021-03-06 12:26:28 +0100426 vlib_main_init ();
427 vm = vlib_get_first_main ();
Damjan Marion7cd468a2016-12-19 23:05:39 +0100428
429 clib_macro_init (&vam->macro_main);
430 clib_macro_add_builtin (&vam->macro_main, "current_file",
431 eval_current_file);
432 clib_macro_add_builtin (&vam->macro_main, "current_line",
433 eval_current_line);
434
435 init_error_string_table (vam);
Dave Barach59b25652017-09-10 15:04:27 -0400436 vec_validate (vam->cmd_reply, 0);
437 vec_reset_length (vam->cmd_reply);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100438
Damjan Marion4d2f86a2019-01-18 13:28:22 +0100439 vat_find_plugin_path ();
440
Damjan Marion7cd468a2016-12-19 23:05:39 +0100441 unformat_init_command_line (a, argv);
442
443 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
444 {
445 if (unformat (a, "in %s", &this_input_file))
446 vec_add1 (input_files, this_input_file);
447 else if (unformat (a, "out %s", &output_file))
448 ;
449 else if (unformat (a, "script"))
450 interactive = 0;
451 else if (unformat (a, "json"))
452 json_output = 1;
Dave Barach59b25652017-09-10 15:04:27 -0400453 else if (unformat (a, "socket-name %s", &vam->socket_name))
454 ;
455 else if (unformat (a, "default-socket"))
456 {
457 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
458 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100459 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
460 vec_add1 (vat_plugin_path, 0);
461 else if (unformat (a, "plugin_name_filter %s",
462 (u8 *) & vat_plugin_name_filter))
463 vec_add1 (vat_plugin_name_filter, 0);
464 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
465 {
466 vl_set_memory_root_path ((char *) chroot_prefix);
467 }
468 else
469 {
Dave Barach59b25652017-09-10 15:04:27 -0400470 fformat
471 (stderr,
472 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
473 "[plugin_path <path>][default-socket][socket-name <name>]\n"
474 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
475 argv[0]);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100476 exit (1);
477 }
478 }
479
480 if (output_file)
481 vam->ofp = fopen ((char *) output_file, "w");
482 else
483 vam->ofp = stdout;
484
485 if (vam->ofp == NULL)
486 {
487 fformat (stderr, "Couldn't open output file %s\n",
488 output_file ? (char *) output_file : "stdout");
489 exit (1);
490 }
491
492 clib_time_init (&vam->clib_time);
493
494 vat_api_hookup (vam);
495 vat_plugin_api_reference ();
496
497 setup_signal_handlers ();
498
Dave Barach59b25652017-09-10 15:04:27 -0400499 if (vam->socket_name && vat_socket_connect (vam))
500 fformat (stderr, "WARNING: socket connection failed");
501
Florin Coras90a63982017-12-19 04:50:01 -0800502 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
Dave Barach59b25652017-09-10 15:04:27 -0400503 && connect_to_vpe ("vpp_api_test") < 0)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100504 {
505 svm_region_exit ();
506 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
507 exit (1);
508 }
509
510 vam->json_output = json_output;
511
Damjan Marion7cd468a2016-12-19 23:05:39 +0100512 vec_validate (vam->inbuf, 4096);
513
Ole Troan3d812672020-09-15 10:53:34 +0200514 load_features ();
515
Damjan Marion7cd468a2016-12-19 23:05:39 +0100516 vam->current_file = (u8 *) "plugin-init";
517 vat_plugin_init (vam);
518
Filip Tehlarf0e67d72021-07-23 22:03:05 +0000519 vat_register_interface_dump (vam);
520
521 if (!json_output)
522 vam->api_sw_interface_dump (vam);
523
Jon Loeligerc0b19542020-05-11 08:43:51 -0500524 /* Set up the init function hash table */
Damjan Marionfd8deb42021-03-06 12:26:28 +0100525 vgm->init_functions_called = hash_create (0, 0);
Jon Loeligerc0b19542020-05-11 08:43:51 -0500526
527 /* Execute plugin init and api_init functions */
Damjan Marionfd8deb42021-03-06 12:26:28 +0100528 error = vlib_call_init_exit_functions (vm, &vgm->init_function_registrations,
529 1 /* call once */, 1 /* is_global*/);
Jon Loeligerc0b19542020-05-11 08:43:51 -0500530
531 if (error)
532 clib_error_report (error);
533
Damjan Marionfd8deb42021-03-06 12:26:28 +0100534 error =
535 vlib_call_init_exit_functions (vm, &vgm->api_init_function_registrations,
536 1 /* call_once */, 1 /* is_global */);
Jon Loeligerc0b19542020-05-11 08:43:51 -0500537
538 if (error)
539 clib_error_report (error);
540
Damjan Marion7cd468a2016-12-19 23:05:39 +0100541 for (i = 0; i < vec_len (input_files); i++)
542 {
543 vam->ifp = fopen ((char *) input_files[i], "r");
544 if (vam->ifp == NULL)
545 {
546 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
547 continue;
548 }
549 vam->current_file = input_files[i];
550 vam->input_line_number = 0;
551 do_one_file (vam);
552 fclose (vam->ifp);
553 }
554
555 if (output_file)
556 fclose (vam->ofp);
557
558 if (interactive)
559 {
560 vam->ifp = stdin;
561 vam->ofp = stdout;
562 vam->current_file = (u8 *) "interactive";
563 do_one_file (vam);
564 fclose (vam->ifp);
565 }
566
Dave Barachcf5e8482017-10-17 11:48:29 -0400567 /*
568 * Particularly when running a script, don't be in a hurry to leave.
569 * A reply message queued to this process will end up constipating
570 * the allocation rings.
571 */
572 timeout = vat_time_now (vam) + 2.0;
573 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
574 ;
575
576 if (vat_time_now (vam) > timeout)
577 clib_warning ("BUG: message reply spin-wait timeout");
578
Damjan Marion7cd468a2016-12-19 23:05:39 +0100579 vl_client_disconnect_from_vlib ();
580 exit (0);
581}
582
583/*
584 * fd.io coding-style-patch-verification: ON
585 *
586 * Local Variables:
587 * eval: (c-set-style "gnu")
588 * End:
589 */