Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | vat_main_t vat_main; |
| 20 | |
| 21 | #include <vlibapi/api_helper_macros.h> |
| 22 | vpe_api_main_t vpe_api_main; |
| 23 | |
| 24 | void |
| 25 | vat_suspend (vlib_main_t * vm, f64 interval) |
| 26 | { |
| 27 | /* do nothing in the standalone version, just return */ |
| 28 | } |
| 29 | |
| 30 | void |
| 31 | fformat_append_cr (FILE * ofp, const char *fmt, ...) |
| 32 | { |
| 33 | va_list va; |
| 34 | |
| 35 | va_start (va, fmt); |
| 36 | (void) va_fformat (ofp, (char *) fmt, &va); |
| 37 | va_end (va); |
| 38 | fformat (ofp, "\n"); |
| 39 | } |
| 40 | |
| 41 | int |
| 42 | connect_to_vpe (char *name) |
| 43 | { |
| 44 | vat_main_t *vam = &vat_main; |
| 45 | api_main_t *am = &api_main; |
| 46 | |
| 47 | if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0) |
| 48 | return -1; |
| 49 | |
| 50 | vam->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 51 | vam->my_client_index = am->my_client_index; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | vlib_main_t vlib_global_main; |
| 57 | vlib_main_t **vlib_mains; |
| 58 | void |
| 59 | vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...) |
| 60 | { |
| 61 | clib_warning ("BUG"); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static u8 * |
| 66 | format_api_error (u8 * s, va_list * args) |
| 67 | { |
| 68 | vat_main_t *vam = va_arg (*args, vat_main_t *); |
| 69 | i32 error = va_arg (*args, u32); |
| 70 | uword *p; |
| 71 | |
| 72 | p = hash_get (vam->error_string_by_error_number, -error); |
| 73 | |
| 74 | if (p) |
| 75 | s = format (s, "%s", p[0]); |
| 76 | else |
| 77 | s = format (s, "%d", error); |
| 78 | return s; |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | do_one_file (vat_main_t * vam) |
| 83 | { |
| 84 | int rv; |
| 85 | int (*fp) (vat_main_t * vam); |
| 86 | int arg_len; |
| 87 | unformat_input_t _input; |
| 88 | u8 *cmdp, *argsp; |
| 89 | uword *p; |
| 90 | u8 *this_cmd = 0; |
| 91 | |
| 92 | vam->input = &_input; |
| 93 | |
| 94 | /* Used by the "quit" command handler */ |
| 95 | if (setjmp (vam->jump_buf) != 0) |
| 96 | return; |
| 97 | |
| 98 | vam->jump_buf_set = 1; |
| 99 | |
| 100 | while (1) |
| 101 | { |
| 102 | if (vam->ifp == stdin) |
| 103 | { |
| 104 | if (vam->exec_mode == 0) |
| 105 | rv = write (1, "vat# ", 5); |
| 106 | else |
| 107 | rv = write (1, "exec# ", 6); |
| 108 | } |
| 109 | |
| 110 | _vec_len (vam->inbuf) = 4096; |
| 111 | |
| 112 | if (vam->do_exit || |
| 113 | fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0) |
| 114 | break; |
| 115 | |
| 116 | vam->input_line_number++; |
| 117 | |
| 118 | vec_free (this_cmd); |
| 119 | |
| 120 | this_cmd = |
| 121 | (u8 *) clib_macro_eval (&vam->macro_main, (char *) vam->inbuf, |
| 122 | 1 /* complain */ ); |
| 123 | |
| 124 | if (vam->exec_mode == 0) |
| 125 | { |
| 126 | /* Split input into cmd + args */ |
| 127 | cmdp = this_cmd; |
| 128 | |
| 129 | while (cmdp < (this_cmd + vec_len (this_cmd))) |
| 130 | { |
| 131 | if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n') |
| 132 | { |
| 133 | cmdp++; |
| 134 | } |
| 135 | else |
| 136 | break; |
| 137 | } |
| 138 | argsp = cmdp; |
| 139 | while (argsp < (this_cmd + vec_len (this_cmd))) |
| 140 | { |
| 141 | if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n') |
| 142 | { |
| 143 | argsp++; |
| 144 | } |
| 145 | else |
| 146 | break; |
| 147 | } |
| 148 | *argsp++ = 0; |
| 149 | while (argsp < (this_cmd + vec_len (this_cmd))) |
| 150 | { |
| 151 | if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n') |
| 152 | { |
| 153 | argsp++; |
| 154 | } |
| 155 | else |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /* Blank input line? */ |
| 161 | if (*cmdp == 0) |
| 162 | continue; |
| 163 | |
| 164 | p = hash_get_mem (vam->function_by_name, cmdp); |
| 165 | if (p == 0) |
| 166 | { |
| 167 | errmsg ("'%s': function not found\n", cmdp); |
| 168 | continue; |
| 169 | } |
| 170 | |
| 171 | arg_len = strlen ((char *) argsp); |
| 172 | |
| 173 | unformat_init_string (vam->input, (char *) argsp, arg_len); |
| 174 | fp = (void *) p[0]; |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | unformat_init_string (vam->input, (char *) this_cmd, |
| 179 | strlen ((char *) this_cmd)); |
| 180 | cmdp = this_cmd; |
| 181 | fp = exec; |
| 182 | } |
| 183 | |
| 184 | rv = (*fp) (vam); |
| 185 | if (rv < 0) |
| 186 | errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv); |
| 187 | unformat_free (vam->input); |
| 188 | |
| 189 | if (vam->regenerate_interface_table) |
| 190 | { |
| 191 | vam->regenerate_interface_table = 0; |
| 192 | api_sw_interface_dump (vam); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static void |
| 198 | init_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 | |
| 210 | static i8 * |
| 211 | eval_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 | |
| 217 | static i8 * |
| 218 | eval_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 | |
| 224 | static void |
| 225 | signal_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 | |
| 249 | static void |
| 250 | setup_signal_handlers (void) |
| 251 | { |
| 252 | uword i; |
| 253 | struct sigaction sa; |
| 254 | |
| 255 | for (i = 1; i < 32; i++) |
| 256 | { |
| 257 | memset (&sa, 0, sizeof (sa)); |
| 258 | 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: |
| 274 | sa.sa_sigaction = (void *) SIG_IGN; |
| 275 | break; |
| 276 | |
| 277 | /* catch and handle all other signals */ |
| 278 | default: |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | if (sigaction (i, &sa, 0) < 0) |
| 283 | clib_unix_warning ("sigaction %U", format_signal, i); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | int |
| 288 | main (int argc, char **argv) |
| 289 | { |
| 290 | vat_main_t *vam = &vat_main; |
| 291 | unformat_input_t _argv, *a = &_argv; |
| 292 | u8 **input_files = 0; |
| 293 | u8 *output_file = 0; |
| 294 | u8 *chroot_prefix; |
| 295 | u8 *this_input_file; |
| 296 | u8 interactive = 1; |
| 297 | u8 json_output = 0; |
| 298 | u8 *heap; |
| 299 | mheap_t *h; |
| 300 | int i; |
| 301 | |
| 302 | clib_mem_init (0, 128 << 20); |
| 303 | |
| 304 | heap = clib_mem_get_per_cpu_heap (); |
| 305 | h = mheap_header (heap); |
| 306 | |
| 307 | /* make the main heap thread-safe */ |
| 308 | h->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 309 | |
| 310 | clib_macro_init (&vam->macro_main); |
| 311 | clib_macro_add_builtin (&vam->macro_main, "current_file", |
| 312 | eval_current_file); |
| 313 | clib_macro_add_builtin (&vam->macro_main, "current_line", |
| 314 | eval_current_line); |
| 315 | |
| 316 | init_error_string_table (vam); |
| 317 | |
| 318 | unformat_init_command_line (a, argv); |
| 319 | |
| 320 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 321 | { |
| 322 | if (unformat (a, "in %s", &this_input_file)) |
| 323 | vec_add1 (input_files, this_input_file); |
| 324 | else if (unformat (a, "out %s", &output_file)) |
| 325 | ; |
| 326 | else if (unformat (a, "script")) |
| 327 | interactive = 0; |
| 328 | else if (unformat (a, "json")) |
| 329 | json_output = 1; |
| 330 | else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path)) |
| 331 | vec_add1 (vat_plugin_path, 0); |
| 332 | else if (unformat (a, "plugin_name_filter %s", |
| 333 | (u8 *) & vat_plugin_name_filter)) |
| 334 | vec_add1 (vat_plugin_name_filter, 0); |
| 335 | else if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
| 336 | { |
| 337 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 338 | } |
| 339 | else |
| 340 | { |
| 341 | fformat (stderr, |
| 342 | "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"); |
| 343 | exit (1); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (output_file) |
| 348 | vam->ofp = fopen ((char *) output_file, "w"); |
| 349 | else |
| 350 | vam->ofp = stdout; |
| 351 | |
| 352 | if (vam->ofp == NULL) |
| 353 | { |
| 354 | fformat (stderr, "Couldn't open output file %s\n", |
| 355 | output_file ? (char *) output_file : "stdout"); |
| 356 | exit (1); |
| 357 | } |
| 358 | |
| 359 | clib_time_init (&vam->clib_time); |
| 360 | |
| 361 | vat_api_hookup (vam); |
| 362 | vat_plugin_api_reference (); |
| 363 | |
| 364 | setup_signal_handlers (); |
| 365 | |
| 366 | if (connect_to_vpe ("vpp_api_test") < 0) |
| 367 | { |
| 368 | svm_region_exit (); |
| 369 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 370 | exit (1); |
| 371 | } |
| 372 | |
| 373 | vam->json_output = json_output; |
| 374 | |
| 375 | if (!json_output) |
| 376 | { |
| 377 | api_sw_interface_dump (vam); |
| 378 | } |
| 379 | |
| 380 | vec_validate (vam->inbuf, 4096); |
| 381 | |
| 382 | vam->current_file = (u8 *) "plugin-init"; |
| 383 | vat_plugin_init (vam); |
| 384 | |
| 385 | for (i = 0; i < vec_len (input_files); i++) |
| 386 | { |
| 387 | vam->ifp = fopen ((char *) input_files[i], "r"); |
| 388 | if (vam->ifp == NULL) |
| 389 | { |
| 390 | fformat (stderr, "Couldn't open input file %s\n", input_files[i]); |
| 391 | continue; |
| 392 | } |
| 393 | vam->current_file = input_files[i]; |
| 394 | vam->input_line_number = 0; |
| 395 | do_one_file (vam); |
| 396 | fclose (vam->ifp); |
| 397 | } |
| 398 | |
| 399 | if (output_file) |
| 400 | fclose (vam->ofp); |
| 401 | |
| 402 | if (interactive) |
| 403 | { |
| 404 | vam->ifp = stdin; |
| 405 | vam->ofp = stdout; |
| 406 | vam->current_file = (u8 *) "interactive"; |
| 407 | do_one_file (vam); |
| 408 | fclose (vam->ifp); |
| 409 | } |
| 410 | |
| 411 | vl_client_disconnect_from_vlib (); |
| 412 | exit (0); |
| 413 | } |
| 414 | |
| 415 | /* |
| 416 | * fd.io coding-style-patch-verification: ON |
| 417 | * |
| 418 | * Local Variables: |
| 419 | * eval: (c-set-style "gnu") |
| 420 | * End: |
| 421 | */ |