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> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 22 | |
| 23 | void |
| 24 | vat_suspend (vlib_main_t * vm, f64 interval) |
| 25 | { |
| 26 | /* do nothing in the standalone version, just return */ |
| 27 | } |
| 28 | |
| 29 | void |
| 30 | fformat_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 | |
| 40 | int |
| 41 | connect_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 | |
| 55 | vlib_main_t vlib_global_main; |
| 56 | vlib_main_t **vlib_mains; |
| 57 | void |
| 58 | vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...) |
| 59 | { |
| 60 | clib_warning ("BUG"); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | static u8 * |
| 65 | format_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 | |
| 80 | void |
| 81 | do_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 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | init_error_string_table (vat_main_t * vam) |
| 198 | { |
| 199 | |
| 200 | vam->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 201 | |
| 202 | #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s); |
| 203 | foreach_vnet_api_error; |
| 204 | #undef _ |
| 205 | |
| 206 | hash_set (vam->error_string_by_error_number, 99, "Misc"); |
| 207 | } |
| 208 | |
| 209 | static i8 * |
| 210 | eval_current_file (macro_main_t * mm, i32 complain) |
| 211 | { |
| 212 | vat_main_t *vam = &vat_main; |
| 213 | return ((i8 *) format (0, "%s%c", vam->current_file, 0)); |
| 214 | } |
| 215 | |
| 216 | static i8 * |
| 217 | eval_current_line (macro_main_t * mm, i32 complain) |
| 218 | { |
| 219 | vat_main_t *vam = &vat_main; |
| 220 | return ((i8 *) format (0, "%d%c", vam->input_line_number, 0)); |
| 221 | } |
| 222 | |
| 223 | static void |
| 224 | signal_handler (int signum, siginfo_t * si, ucontext_t * uc) |
| 225 | { |
| 226 | vat_main_t *vam = &vat_main; |
| 227 | |
| 228 | switch (signum) |
| 229 | { |
| 230 | /* these (caught) signals cause the application to exit */ |
| 231 | case SIGINT: |
| 232 | case SIGTERM: |
| 233 | if (vam->jump_buf_set) |
| 234 | { |
| 235 | vam->do_exit = 1; |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | /* FALLTHROUGH on purpose */ |
| 240 | |
| 241 | default: |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | _exit (1); |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | setup_signal_handlers (void) |
| 250 | { |
| 251 | uword i; |
| 252 | struct sigaction sa; |
| 253 | |
| 254 | for (i = 1; i < 32; i++) |
| 255 | { |
| 256 | memset (&sa, 0, sizeof (sa)); |
| 257 | sa.sa_sigaction = (void *) signal_handler; |
| 258 | sa.sa_flags = SA_SIGINFO; |
| 259 | |
| 260 | switch (i) |
| 261 | { |
| 262 | /* these signals take the default action */ |
| 263 | case SIGABRT: |
| 264 | case SIGKILL: |
| 265 | case SIGSTOP: |
| 266 | case SIGUSR1: |
| 267 | case SIGUSR2: |
| 268 | continue; |
| 269 | |
| 270 | /* ignore SIGPIPE, SIGCHLD */ |
| 271 | case SIGPIPE: |
| 272 | case SIGCHLD: |
| 273 | sa.sa_sigaction = (void *) SIG_IGN; |
| 274 | break; |
| 275 | |
| 276 | /* catch and handle all other signals */ |
| 277 | default: |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | if (sigaction (i, &sa, 0) < 0) |
| 282 | clib_unix_warning ("sigaction %U", format_signal, i); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | int |
| 287 | main (int argc, char **argv) |
| 288 | { |
| 289 | vat_main_t *vam = &vat_main; |
| 290 | unformat_input_t _argv, *a = &_argv; |
| 291 | u8 **input_files = 0; |
| 292 | u8 *output_file = 0; |
| 293 | u8 *chroot_prefix; |
| 294 | u8 *this_input_file; |
| 295 | u8 interactive = 1; |
| 296 | u8 json_output = 0; |
| 297 | u8 *heap; |
| 298 | mheap_t *h; |
| 299 | int i; |
| 300 | |
| 301 | clib_mem_init (0, 128 << 20); |
| 302 | |
| 303 | heap = clib_mem_get_per_cpu_heap (); |
| 304 | h = mheap_header (heap); |
| 305 | |
| 306 | /* make the main heap thread-safe */ |
| 307 | h->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 308 | |
| 309 | clib_macro_init (&vam->macro_main); |
| 310 | clib_macro_add_builtin (&vam->macro_main, "current_file", |
| 311 | eval_current_file); |
| 312 | clib_macro_add_builtin (&vam->macro_main, "current_line", |
| 313 | eval_current_line); |
| 314 | |
| 315 | init_error_string_table (vam); |
| 316 | |
| 317 | unformat_init_command_line (a, argv); |
| 318 | |
| 319 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 320 | { |
| 321 | if (unformat (a, "in %s", &this_input_file)) |
| 322 | vec_add1 (input_files, this_input_file); |
| 323 | else if (unformat (a, "out %s", &output_file)) |
| 324 | ; |
| 325 | else if (unformat (a, "script")) |
| 326 | interactive = 0; |
| 327 | else if (unformat (a, "json")) |
| 328 | json_output = 1; |
| 329 | else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path)) |
| 330 | vec_add1 (vat_plugin_path, 0); |
| 331 | else if (unformat (a, "plugin_name_filter %s", |
| 332 | (u8 *) & vat_plugin_name_filter)) |
| 333 | vec_add1 (vat_plugin_name_filter, 0); |
| 334 | else if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
| 335 | { |
| 336 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | fformat (stderr, |
| 341 | "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"); |
| 342 | exit (1); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | if (output_file) |
| 347 | vam->ofp = fopen ((char *) output_file, "w"); |
| 348 | else |
| 349 | vam->ofp = stdout; |
| 350 | |
| 351 | if (vam->ofp == NULL) |
| 352 | { |
| 353 | fformat (stderr, "Couldn't open output file %s\n", |
| 354 | output_file ? (char *) output_file : "stdout"); |
| 355 | exit (1); |
| 356 | } |
| 357 | |
| 358 | clib_time_init (&vam->clib_time); |
| 359 | |
| 360 | vat_api_hookup (vam); |
| 361 | vat_plugin_api_reference (); |
| 362 | |
| 363 | setup_signal_handlers (); |
| 364 | |
| 365 | if (connect_to_vpe ("vpp_api_test") < 0) |
| 366 | { |
| 367 | svm_region_exit (); |
| 368 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 369 | exit (1); |
| 370 | } |
| 371 | |
| 372 | vam->json_output = json_output; |
| 373 | |
| 374 | if (!json_output) |
| 375 | { |
| 376 | api_sw_interface_dump (vam); |
| 377 | } |
| 378 | |
| 379 | vec_validate (vam->inbuf, 4096); |
| 380 | |
| 381 | vam->current_file = (u8 *) "plugin-init"; |
| 382 | vat_plugin_init (vam); |
| 383 | |
| 384 | for (i = 0; i < vec_len (input_files); i++) |
| 385 | { |
| 386 | vam->ifp = fopen ((char *) input_files[i], "r"); |
| 387 | if (vam->ifp == NULL) |
| 388 | { |
| 389 | fformat (stderr, "Couldn't open input file %s\n", input_files[i]); |
| 390 | continue; |
| 391 | } |
| 392 | vam->current_file = input_files[i]; |
| 393 | vam->input_line_number = 0; |
| 394 | do_one_file (vam); |
| 395 | fclose (vam->ifp); |
| 396 | } |
| 397 | |
| 398 | if (output_file) |
| 399 | fclose (vam->ofp); |
| 400 | |
| 401 | if (interactive) |
| 402 | { |
| 403 | vam->ifp = stdin; |
| 404 | vam->ofp = stdout; |
| 405 | vam->current_file = (u8 *) "interactive"; |
| 406 | do_one_file (vam); |
| 407 | fclose (vam->ifp); |
| 408 | } |
| 409 | |
| 410 | vl_client_disconnect_from_vlib (); |
| 411 | exit (0); |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * fd.io coding-style-patch-verification: ON |
| 416 | * |
| 417 | * Local Variables: |
| 418 | * eval: (c-set-style "gnu") |
| 419 | * End: |
| 420 | */ |