blob: 723c5e8cc3ad6226d2eac076277970ea4c295ae7 [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 =
120 (u8 *) clib_macro_eval (&vam->macro_main, (char *) vam->inbuf,
121 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;
308 u8 *heap;
309 mheap_t *h;
310 int i;
Dave Barachcf5e8482017-10-17 11:48:29 -0400311 f64 timeout;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100312
313 clib_mem_init (0, 128 << 20);
314
315 heap = clib_mem_get_per_cpu_heap ();
316 h = mheap_header (heap);
317
318 /* make the main heap thread-safe */
319 h->flags |= MHEAP_FLAG_THREAD_SAFE;
320
321 clib_macro_init (&vam->macro_main);
322 clib_macro_add_builtin (&vam->macro_main, "current_file",
323 eval_current_file);
324 clib_macro_add_builtin (&vam->macro_main, "current_line",
325 eval_current_line);
326
327 init_error_string_table (vam);
Dave Barach59b25652017-09-10 15:04:27 -0400328 vec_validate (vam->cmd_reply, 0);
329 vec_reset_length (vam->cmd_reply);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100330
331 unformat_init_command_line (a, argv);
332
333 while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT)
334 {
335 if (unformat (a, "in %s", &this_input_file))
336 vec_add1 (input_files, this_input_file);
337 else if (unformat (a, "out %s", &output_file))
338 ;
339 else if (unformat (a, "script"))
340 interactive = 0;
341 else if (unformat (a, "json"))
342 json_output = 1;
Dave Barach59b25652017-09-10 15:04:27 -0400343 else if (unformat (a, "socket-name %s", &vam->socket_name))
344 ;
345 else if (unformat (a, "default-socket"))
346 {
347 vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
348 }
Damjan Marion7cd468a2016-12-19 23:05:39 +0100349 else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path))
350 vec_add1 (vat_plugin_path, 0);
351 else if (unformat (a, "plugin_name_filter %s",
352 (u8 *) & vat_plugin_name_filter))
353 vec_add1 (vat_plugin_name_filter, 0);
354 else if (unformat (a, "chroot prefix %s", &chroot_prefix))
355 {
356 vl_set_memory_root_path ((char *) chroot_prefix);
357 }
358 else
359 {
Dave Barach59b25652017-09-10 15:04:27 -0400360 fformat
361 (stderr,
362 "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"
363 "[plugin_path <path>][default-socket][socket-name <name>]\n"
364 "[plugin_name_filter <filter>][chroot prefix <path>]\n",
365 argv[0]);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100366 exit (1);
367 }
368 }
369
370 if (output_file)
371 vam->ofp = fopen ((char *) output_file, "w");
372 else
373 vam->ofp = stdout;
374
375 if (vam->ofp == NULL)
376 {
377 fformat (stderr, "Couldn't open output file %s\n",
378 output_file ? (char *) output_file : "stdout");
379 exit (1);
380 }
381
382 clib_time_init (&vam->clib_time);
383
384 vat_api_hookup (vam);
385 vat_plugin_api_reference ();
386
387 setup_signal_handlers ();
388
Dave Barach59b25652017-09-10 15:04:27 -0400389 if (vam->socket_name && vat_socket_connect (vam))
390 fformat (stderr, "WARNING: socket connection failed");
391
Florin Coras90a63982017-12-19 04:50:01 -0800392 if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0)
Dave Barach59b25652017-09-10 15:04:27 -0400393 && connect_to_vpe ("vpp_api_test") < 0)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100394 {
395 svm_region_exit ();
396 fformat (stderr, "Couldn't connect to vpe, exiting...\n");
397 exit (1);
398 }
399
400 vam->json_output = json_output;
401
402 if (!json_output)
Dave Barach59b25652017-09-10 15:04:27 -0400403 api_sw_interface_dump (vam);
Damjan Marion7cd468a2016-12-19 23:05:39 +0100404
405 vec_validate (vam->inbuf, 4096);
406
407 vam->current_file = (u8 *) "plugin-init";
408 vat_plugin_init (vam);
409
410 for (i = 0; i < vec_len (input_files); i++)
411 {
412 vam->ifp = fopen ((char *) input_files[i], "r");
413 if (vam->ifp == NULL)
414 {
415 fformat (stderr, "Couldn't open input file %s\n", input_files[i]);
416 continue;
417 }
418 vam->current_file = input_files[i];
419 vam->input_line_number = 0;
420 do_one_file (vam);
421 fclose (vam->ifp);
422 }
423
424 if (output_file)
425 fclose (vam->ofp);
426
427 if (interactive)
428 {
429 vam->ifp = stdin;
430 vam->ofp = stdout;
431 vam->current_file = (u8 *) "interactive";
432 do_one_file (vam);
433 fclose (vam->ifp);
434 }
435
Dave Barachcf5e8482017-10-17 11:48:29 -0400436 /*
437 * Particularly when running a script, don't be in a hurry to leave.
438 * A reply message queued to this process will end up constipating
439 * the allocation rings.
440 */
441 timeout = vat_time_now (vam) + 2.0;
442 while (vam->result_ready == 0 && vat_time_now (vam) < timeout)
443 ;
444
445 if (vat_time_now (vam) > timeout)
446 clib_warning ("BUG: message reply spin-wait timeout");
447
Damjan Marion7cd468a2016-12-19 23:05:39 +0100448 vl_client_disconnect_from_vlib ();
449 exit (0);
450}
451
452/*
453 * fd.io coding-style-patch-verification: ON
454 *
455 * Local Variables:
456 * eval: (c-set-style "gnu")
457 * End:
458 */