blob: c34ec947252d47a0488ce3e4fd36cef475534ec3 [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>
18
19vat_main_t vat_main;
20
21#include <vlibapi/api_helper_macros.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010022
23void
24vat_suspend (vlib_main_t * vm, f64 interval)
25{
26 /* do nothing in the standalone version, just return */
27}
28
29void
30fformat_append_cr (FILE * ofp, const char *fmt, ...)
31{
32 va_list va;
33
34 va_start (va, fmt);
35 (void) va_fformat (ofp, (char *) fmt, &va);
36 va_end (va);
37 fformat (ofp, "\n");
38}
39
40int
41connect_to_vpe (char *name)
42{
43 vat_main_t *vam = &vat_main;
44 api_main_t *am = &api_main;
45
46 if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0)
47 return -1;
48
49 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
50 vam->my_client_index = am->my_client_index;
51
52 return 0;
53}
54
55vlib_main_t vlib_global_main;
56vlib_main_t **vlib_mains;
57void
58vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...)
59{
60 clib_warning ("BUG");
61}
62
63
64static u8 *
65format_api_error (u8 * s, va_list * args)
66{
67 vat_main_t *vam = va_arg (*args, vat_main_t *);
68 i32 error = va_arg (*args, u32);
69 uword *p;
70
71 p = hash_get (vam->error_string_by_error_number, -error);
72
73 if (p)
74 s = format (s, "%s", p[0]);
75 else
76 s = format (s, "%d", error);
77 return s;
78}
79
80void
81do_one_file (vat_main_t * vam)
82{
83 int rv;
84 int (*fp) (vat_main_t * vam);
85 int arg_len;
86 unformat_input_t _input;
87 u8 *cmdp, *argsp;
88 uword *p;
89 u8 *this_cmd = 0;
90
91 vam->input = &_input;
92
93 /* Used by the "quit" command handler */
94 if (setjmp (vam->jump_buf) != 0)
95 return;
96
97 vam->jump_buf_set = 1;
98
99 while (1)
100 {
101 if (vam->ifp == stdin)
102 {
103 if (vam->exec_mode == 0)
104 rv = write (1, "vat# ", 5);
105 else
106 rv = write (1, "exec# ", 6);
107 }
108
109 _vec_len (vam->inbuf) = 4096;
110
111 if (vam->do_exit ||
112 fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0)
113 break;
114
115 vam->input_line_number++;
116
117 vec_free (this_cmd);
118
119 this_cmd =
Damjan Marionffe9d212018-05-30 09:26:11 +0200120 (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf,
Damjan Marion7cd468a2016-12-19 23:05:39 +0100121 1 /* complain */ );
122
123 if (vam->exec_mode == 0)
124 {
125 /* Split input into cmd + args */
126 cmdp = this_cmd;
127
128 while (cmdp < (this_cmd + vec_len (this_cmd)))
129 {
130 if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n')
131 {
132 cmdp++;
133 }
134 else
135 break;
136 }
137 argsp = cmdp;
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 *argsp++ = 0;
148 while (argsp < (this_cmd + vec_len (this_cmd)))
149 {
150 if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n')
151 {
152 argsp++;
153 }
154 else
155 break;
156 }
157
158
159 /* Blank input line? */
160 if (*cmdp == 0)
161 continue;
162
163 p = hash_get_mem (vam->function_by_name, cmdp);
164 if (p == 0)
165 {
166 errmsg ("'%s': function not found\n", cmdp);
167 continue;
168 }
169
170 arg_len = strlen ((char *) argsp);
171
172 unformat_init_string (vam->input, (char *) argsp, arg_len);
173 fp = (void *) p[0];
174 }
175 else
176 {
177 unformat_init_string (vam->input, (char *) this_cmd,
178 strlen ((char *) this_cmd));
179 cmdp = this_cmd;
180 fp = exec;
181 }
182
183 rv = (*fp) (vam);
184 if (rv < 0)
185 errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv);
186 unformat_free (vam->input);
187
188 if (vam->regenerate_interface_table)
189 {
190 vam->regenerate_interface_table = 0;
191 api_sw_interface_dump (vam);
192 }
Dave Barach59b25652017-09-10 15:04:27 -0400193
194 /* Hack to pick up new client index after memfd_segment_create pivot */
195 if (vam->client_index_invalid)
196 {
197 vat_main_t *vam = &vat_main;
198 api_main_t *am = &api_main;
199
200 vam->vl_input_queue = am->shmem_hdr->vl_input_queue;
201 vam->my_client_index = am->my_client_index;
202 vam->client_index_invalid = 0;
203 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100204 }
205}
206
207static void
208init_error_string_table (vat_main_t * vam)
209{
210
211 vam->error_string_by_error_number = hash_create (0, sizeof (uword));
212
213#define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s);
214 foreach_vnet_api_error;
215#undef _
216
217 hash_set (vam->error_string_by_error_number, 99, "Misc");
218}
219
220static i8 *
221eval_current_file (macro_main_t * mm, i32 complain)
222{
223 vat_main_t *vam = &vat_main;
224 return ((i8 *) format (0, "%s%c", vam->current_file, 0));
225}
226
227static i8 *
228eval_current_line (macro_main_t * mm, i32 complain)
229{
230 vat_main_t *vam = &vat_main;
231 return ((i8 *) format (0, "%d%c", vam->input_line_number, 0));
232}
233
234static void
235signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
236{
237 vat_main_t *vam = &vat_main;
238
239 switch (signum)
240 {
241 /* these (caught) signals cause the application to exit */
242 case SIGINT:
243 case SIGTERM:
244 if (vam->jump_buf_set)
245 {
246 vam->do_exit = 1;
247 return;
248 }
249
250 /* FALLTHROUGH on purpose */
251
252 default:
253 break;
254 }
255
256 _exit (1);
257}
258
259static void
260setup_signal_handlers (void)
261{
262 uword i;
263 struct sigaction sa;
264
265 for (i = 1; i < 32; i++)
266 {
267 memset (&sa, 0, sizeof (sa));
268 sa.sa_sigaction = (void *) signal_handler;
269 sa.sa_flags = SA_SIGINFO;
270
271 switch (i)
272 {
273 /* these signals take the default action */
274 case SIGABRT:
275 case SIGKILL:
276 case SIGSTOP:
277 case SIGUSR1:
278 case SIGUSR2:
279 continue;
280
281 /* ignore SIGPIPE, SIGCHLD */
282 case SIGPIPE:
283 case SIGCHLD:
284 sa.sa_sigaction = (void *) SIG_IGN;
285 break;
286
287 /* catch and handle all other signals */
288 default:
289 break;
290 }
291
292 if (sigaction (i, &sa, 0) < 0)
293 clib_unix_warning ("sigaction %U", format_signal, i);
294 }
295}
296
297int
298main (int argc, char **argv)
299{
300 vat_main_t *vam = &vat_main;
301 unformat_input_t _argv, *a = &_argv;
302 u8 **input_files = 0;
303 u8 *output_file = 0;
304 u8 *chroot_prefix;
305 u8 *this_input_file;
306 u8 interactive = 1;
307 u8 json_output = 0;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100308 int i;
Dave Barachcf5e8482017-10-17 11:48:29 -0400309 f64 timeout;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100310
Dave Barach6a5adc32018-07-04 10:56:23 -0400311 clib_mem_init_thread_safe (0, 128 << 20);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100312
313 clib_macro_init (&vam->macro_main);
314 clib_macro_add_builtin (&vam->macro_main, "current_file",
315 eval_current_file);
316 clib_macro_add_builtin (&vam->macro_main, "current_line",
317 eval_current_line);
318
319 init_error_string_table (vam);
Dave Barach59b25652017-09-10 15:04:27 -0400320 vec_validate (vam->cmd_reply, 0);
321 vec_reset_length (vam->cmd_reply);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100322
323 unformat_init_command_line (a, argv);
324
325 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
326 {
327 if (unformat (a, "in %s", &this_input_file))
328 vec_add1 (input_files, this_input_file);
329 else if (unformat (a, "out %s", &output_file))
330 ;
331 else if (unformat (a, "script"))
332 interactive = 0;
333 else if (unformat (a, "json"))
334 json_output = 1;
Dave Barach59b25652017-09-10 15:04:27 -0400335 else if (unformat (a, "socket-name %s", &vam->socket_name))
336 ;
337 else if (unformat (a, "default-socket"))
338 {
339 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
340 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100341 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
342 vec_add1 (vat_plugin_path, 0);
343 else if (unformat (a, "plugin_name_filter %s",
344 (u8 *) & vat_plugin_name_filter))
345 vec_add1 (vat_plugin_name_filter, 0);
346 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
347 {
348 vl_set_memory_root_path ((char *) chroot_prefix);
349 }
350 else
351 {
Dave Barach59b25652017-09-10 15:04:27 -0400352 fformat
353 (stderr,
354 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
355 "[plugin_path <path>][default-socket][socket-name <name>]\n"
356 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
357 argv[0]);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100358 exit (1);
359 }
360 }
361
362 if (output_file)
363 vam->ofp = fopen ((char *) output_file, "w");
364 else
365 vam->ofp = stdout;
366
367 if (vam->ofp == NULL)
368 {
369 fformat (stderr, "Couldn't open output file %s\n",
370 output_file ? (char *) output_file : "stdout");
371 exit (1);
372 }
373
374 clib_time_init (&vam->clib_time);
375
376 vat_api_hookup (vam);
377 vat_plugin_api_reference ();
378
379 setup_signal_handlers ();
380
Dave Barach59b25652017-09-10 15:04:27 -0400381 if (vam->socket_name && vat_socket_connect (vam))
382 fformat (stderr, "WARNING: socket connection failed");
383
Florin Coras90a63982017-12-19 04:50:01 -0800384 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
Dave Barach59b25652017-09-10 15:04:27 -0400385 && connect_to_vpe ("vpp_api_test") < 0)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100386 {
387 svm_region_exit ();
388 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
389 exit (1);
390 }
391
392 vam->json_output = json_output;
393
394 if (!json_output)
Dave Barach59b25652017-09-10 15:04:27 -0400395 api_sw_interface_dump (vam);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100396
397 vec_validate (vam->inbuf, 4096);
398
399 vam->current_file = (u8 *) "plugin-init";
400 vat_plugin_init (vam);
401
402 for (i = 0; i < vec_len (input_files); i++)
403 {
404 vam->ifp = fopen ((char *) input_files[i], "r");
405 if (vam->ifp == NULL)
406 {
407 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
408 continue;
409 }
410 vam->current_file = input_files[i];
411 vam->input_line_number = 0;
412 do_one_file (vam);
413 fclose (vam->ifp);
414 }
415
416 if (output_file)
417 fclose (vam->ofp);
418
419 if (interactive)
420 {
421 vam->ifp = stdin;
422 vam->ofp = stdout;
423 vam->current_file = (u8 *) "interactive";
424 do_one_file (vam);
425 fclose (vam->ifp);
426 }
427
Dave Barachcf5e8482017-10-17 11:48:29 -0400428 /*
429 * Particularly when running a script, don't be in a hurry to leave.
430 * A reply message queued to this process will end up constipating
431 * the allocation rings.
432 */
433 timeout = vat_time_now (vam) + 2.0;
434 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
435 ;
436
437 if (vat_time_now (vam) > timeout)
438 clib_warning ("BUG: message reply spin-wait timeout");
439
Damjan Marion7cd468a2016-12-19 23:05:39 +0100440 vl_client_disconnect_from_vlib ();
441 exit (0);
442}
443
444/*
445 * fd.io coding-style-patch-verification: ON
446 *
447 * Local Variables:
448 * eval: (c-set-style "gnu")
449 * End:
450 */