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" |
Filip Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame] | 16 | #include <dlfcn.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 17 | #include "plugin.h" |
| 18 | #include <signal.h> |
Damjan Marion | 4d2f86a | 2019-01-18 13:28:22 +0100 | [diff] [blame] | 19 | #include <limits.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 20 | |
| 21 | vat_main_t vat_main; |
| 22 | |
| 23 | #include <vlibapi/api_helper_macros.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 24 | |
| 25 | void |
| 26 | vat_suspend (vlib_main_t * vm, f64 interval) |
| 27 | { |
| 28 | /* do nothing in the standalone version, just return */ |
| 29 | } |
| 30 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 31 | int |
| 32 | connect_to_vpe (char *name) |
| 33 | { |
| 34 | vat_main_t *vam = &vat_main; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 35 | api_main_t *am = vlibapi_get_main (); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 36 | |
| 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 Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 46 | /* *INDENT-OFF* */ |
| 47 | |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 48 | vlib_global_main_t vlib_global_main; |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 49 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 50 | void |
| 51 | vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...) |
| 52 | { |
| 53 | clib_warning ("BUG"); |
| 54 | } |
| 55 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 56 | static u8 * |
| 57 | format_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 | |
| 72 | void |
| 73 | do_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 | |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 101 | vec_set_len (vam->inbuf, 4096); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 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 Marion | ffe9d21 | 2018-05-30 09:26:11 +0200 | [diff] [blame] | 112 | (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf, |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 113 | 1 /* complain */ , |
| 114 | 0 /* level */ , |
| 115 | 8 /* max_level */ ); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 116 | |
| 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 Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame] | 185 | vam->api_sw_interface_dump (vam); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 186 | } |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 187 | |
| 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 Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 192 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 193 | |
| 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 Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | init_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 | |
| 214 | static i8 * |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 215 | eval_current_file (clib_macro_main_t * mm, i32 complain) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 216 | { |
| 217 | vat_main_t *vam = &vat_main; |
| 218 | return ((i8 *) format (0, "%s%c", vam->current_file, 0)); |
| 219 | } |
| 220 | |
| 221 | static i8 * |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 222 | eval_current_line (clib_macro_main_t * mm, i32 complain) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 223 | { |
| 224 | vat_main_t *vam = &vat_main; |
| 225 | return ((i8 *) format (0, "%d%c", vam->input_line_number, 0)); |
| 226 | } |
| 227 | |
| 228 | static void |
| 229 | signal_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 | |
| 253 | static void |
| 254 | setup_signal_handlers (void) |
| 255 | { |
| 256 | uword i; |
| 257 | struct sigaction sa; |
| 258 | |
| 259 | for (i = 1; i < 32; i++) |
| 260 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 261 | clib_memset (&sa, 0, sizeof (sa)); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 262 | 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: |
Vladislav Grishenko | 8486283 | 2022-03-21 02:21:42 +0500 | [diff] [blame] | 270 | case SIGCONT: |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 271 | case SIGSTOP: |
| 272 | case SIGUSR1: |
| 273 | case SIGUSR2: |
Jieqiang Wang | 6f533d7 | 2020-01-20 13:43:38 +0800 | [diff] [blame] | 274 | case SIGPROF: |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 275 | continue; |
| 276 | |
| 277 | /* ignore SIGPIPE, SIGCHLD */ |
| 278 | case SIGPIPE: |
| 279 | case SIGCHLD: |
ezkexma | 7d3161a | 2019-03-26 10:24:38 -0400 | [diff] [blame] | 280 | case SIGWINCH: |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 281 | sa.sa_sigaction = (void *) SIG_IGN; |
| 282 | break; |
| 283 | |
| 284 | /* catch and handle all other signals */ |
| 285 | default: |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | if (sigaction (i, &sa, 0) < 0) |
| 290 | clib_unix_warning ("sigaction %U", format_signal, i); |
| 291 | } |
| 292 | } |
| 293 | |
Damjan Marion | 4d2f86a | 2019-01-18 13:28:22 +0100 | [diff] [blame] | 294 | static void |
| 295 | vat_find_plugin_path () |
| 296 | { |
Damjan Marion | 4d2f86a | 2019-01-18 13:28:22 +0100 | [diff] [blame] | 297 | char *p, path[PATH_MAX]; |
| 298 | int rv; |
| 299 | u8 *s; |
| 300 | |
| 301 | /* find executable path */ |
| 302 | if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1) |
| 303 | return; |
| 304 | |
| 305 | /* readlink doesn't provide null termination */ |
| 306 | path[rv] = 0; |
| 307 | |
| 308 | /* strip filename */ |
| 309 | if ((p = strrchr (path, '/')) == 0) |
| 310 | return; |
| 311 | *p = 0; |
| 312 | |
| 313 | /* strip bin/ */ |
| 314 | if ((p = strrchr (path, '/')) == 0) |
| 315 | return; |
| 316 | *p = 0; |
| 317 | |
Damjan Marion | 5546e43 | 2021-09-30 20:04:14 +0200 | [diff] [blame] | 318 | s = format (0, "%s/" CLIB_LIB_DIR "/vpp_api_test_plugins", path, path); |
Damjan Marion | 4d2f86a | 2019-01-18 13:28:22 +0100 | [diff] [blame] | 319 | vec_add1 (s, 0); |
| 320 | vat_plugin_path = (char *) s; |
| 321 | } |
| 322 | |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame] | 323 | static void |
| 324 | load_features (void) |
| 325 | { |
| 326 | vat_registered_features_t *f; |
| 327 | vat_main_t *vam = &vat_main; |
| 328 | clib_error_t *error; |
| 329 | |
| 330 | f = vam->feature_function_registrations; |
| 331 | |
| 332 | while (f) |
| 333 | { |
| 334 | error = f->function (vam); |
| 335 | if (error) |
| 336 | { |
| 337 | clib_warning ("INIT FAILED"); |
| 338 | } |
| 339 | f = f->next; |
| 340 | } |
| 341 | } |
| 342 | |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 343 | static inline clib_error_t * |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 344 | call_init_exit_functions_internal (vlib_main_t *vm, |
| 345 | _vlib_init_function_list_elt_t **headp, |
| 346 | int call_once, int do_sort, int is_global) |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 347 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 348 | vlib_global_main_t *vgm = vlib_get_global_main (); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 349 | clib_error_t *error = 0; |
| 350 | _vlib_init_function_list_elt_t *i; |
| 351 | |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 352 | ASSERT (is_global == 1); |
| 353 | |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 354 | #if 0 |
| 355 | /* Not worth copying the topological sort code */ |
| 356 | if (do_sort && (error = vlib_sort_init_exit_functions (headp))) |
| 357 | return (error); |
| 358 | #endif |
| 359 | |
| 360 | i = *headp; |
| 361 | while (i) |
| 362 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 363 | if (call_once && !hash_get (vgm->init_functions_called, i->f)) |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 364 | { |
| 365 | if (call_once) |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 366 | hash_set1 (vgm->init_functions_called, i->f); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 367 | error = i->f (vm); |
| 368 | if (error) |
| 369 | return error; |
| 370 | } |
| 371 | i = i->next_init_function; |
| 372 | } |
| 373 | return error; |
| 374 | } |
| 375 | |
| 376 | clib_error_t * |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 377 | vlib_call_init_exit_functions (vlib_main_t *vm, |
| 378 | _vlib_init_function_list_elt_t **headp, |
| 379 | int call_once, int is_global) |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 380 | { |
| 381 | return call_init_exit_functions_internal (vm, headp, call_once, |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 382 | 1 /* do_sort */, is_global); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 383 | } |
| 384 | |
Filip Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame] | 385 | static void |
| 386 | vat_register_interface_dump (vat_main_t *vam) |
| 387 | { |
| 388 | void *handle; |
| 389 | plugin_info_t *pi; |
| 390 | |
| 391 | vec_foreach (pi, vat_plugin_main.plugin_info) |
| 392 | { |
| 393 | handle = dlsym (pi->handle, "api_sw_interface_dump"); |
| 394 | if (handle) |
| 395 | { |
| 396 | vam->api_sw_interface_dump = handle; |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (!vam->api_sw_interface_dump) |
| 402 | { |
| 403 | fformat (stderr, |
| 404 | "sw_interface_dump not found in interface_test plugin!\n"); |
| 405 | exit (1); |
| 406 | } |
| 407 | } |
| 408 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 409 | int |
| 410 | main (int argc, char **argv) |
| 411 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 412 | vlib_global_main_t *vgm = vlib_get_global_main (); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 413 | vat_main_t *vam = &vat_main; |
| 414 | unformat_input_t _argv, *a = &_argv; |
| 415 | u8 **input_files = 0; |
| 416 | u8 *output_file = 0; |
| 417 | u8 *chroot_prefix; |
| 418 | u8 *this_input_file; |
| 419 | u8 interactive = 1; |
| 420 | u8 json_output = 0; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 421 | int i; |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 422 | f64 timeout; |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 423 | clib_error_t *error; |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 424 | vlib_main_t *vm; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 425 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 426 | clib_mem_init_thread_safe (0, 128 << 20); |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 427 | vlib_main_init (); |
| 428 | vm = vlib_get_first_main (); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 429 | |
| 430 | clib_macro_init (&vam->macro_main); |
| 431 | clib_macro_add_builtin (&vam->macro_main, "current_file", |
| 432 | eval_current_file); |
| 433 | clib_macro_add_builtin (&vam->macro_main, "current_line", |
| 434 | eval_current_line); |
| 435 | |
| 436 | init_error_string_table (vam); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 437 | vec_validate (vam->cmd_reply, 0); |
| 438 | vec_reset_length (vam->cmd_reply); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 439 | |
Damjan Marion | 4d2f86a | 2019-01-18 13:28:22 +0100 | [diff] [blame] | 440 | vat_find_plugin_path (); |
| 441 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 442 | unformat_init_command_line (a, argv); |
| 443 | |
| 444 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 445 | { |
| 446 | if (unformat (a, "in %s", &this_input_file)) |
| 447 | vec_add1 (input_files, this_input_file); |
| 448 | else if (unformat (a, "out %s", &output_file)) |
| 449 | ; |
| 450 | else if (unformat (a, "script")) |
| 451 | interactive = 0; |
| 452 | else if (unformat (a, "json")) |
| 453 | json_output = 1; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 454 | else if (unformat (a, "socket-name %s", &vam->socket_name)) |
| 455 | ; |
| 456 | else if (unformat (a, "default-socket")) |
| 457 | { |
| 458 | vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0); |
| 459 | } |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 460 | else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path)) |
| 461 | vec_add1 (vat_plugin_path, 0); |
| 462 | else if (unformat (a, "plugin_name_filter %s", |
| 463 | (u8 *) & vat_plugin_name_filter)) |
| 464 | vec_add1 (vat_plugin_name_filter, 0); |
| 465 | else if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
| 466 | { |
| 467 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 468 | } |
| 469 | else |
| 470 | { |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 471 | fformat |
| 472 | (stderr, |
| 473 | "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n" |
| 474 | "[plugin_path <path>][default-socket][socket-name <name>]\n" |
| 475 | "[plugin_name_filter <filter>][chroot prefix <path>]\n", |
| 476 | argv[0]); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 477 | exit (1); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (output_file) |
| 482 | vam->ofp = fopen ((char *) output_file, "w"); |
| 483 | else |
| 484 | vam->ofp = stdout; |
| 485 | |
| 486 | if (vam->ofp == NULL) |
| 487 | { |
| 488 | fformat (stderr, "Couldn't open output file %s\n", |
| 489 | output_file ? (char *) output_file : "stdout"); |
| 490 | exit (1); |
| 491 | } |
| 492 | |
| 493 | clib_time_init (&vam->clib_time); |
| 494 | |
| 495 | vat_api_hookup (vam); |
| 496 | vat_plugin_api_reference (); |
| 497 | |
| 498 | setup_signal_handlers (); |
| 499 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 500 | if (vam->socket_name && vat_socket_connect (vam)) |
| 501 | fformat (stderr, "WARNING: socket connection failed"); |
| 502 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 503 | if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 504 | && connect_to_vpe ("vpp_api_test") < 0) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 505 | { |
| 506 | svm_region_exit (); |
| 507 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 508 | exit (1); |
| 509 | } |
| 510 | |
| 511 | vam->json_output = json_output; |
| 512 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 513 | vec_validate (vam->inbuf, 4096); |
| 514 | |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame] | 515 | load_features (); |
| 516 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 517 | vam->current_file = (u8 *) "plugin-init"; |
| 518 | vat_plugin_init (vam); |
| 519 | |
Filip Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame] | 520 | vat_register_interface_dump (vam); |
| 521 | |
| 522 | if (!json_output) |
| 523 | vam->api_sw_interface_dump (vam); |
| 524 | |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 525 | /* Set up the init function hash table */ |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 526 | vgm->init_functions_called = hash_create (0, 0); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 527 | |
| 528 | /* Execute plugin init and api_init functions */ |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 529 | error = vlib_call_init_exit_functions (vm, &vgm->init_function_registrations, |
| 530 | 1 /* call once */, 1 /* is_global*/); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 531 | |
| 532 | if (error) |
| 533 | clib_error_report (error); |
| 534 | |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 535 | error = |
| 536 | vlib_call_init_exit_functions (vm, &vgm->api_init_function_registrations, |
| 537 | 1 /* call_once */, 1 /* is_global */); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 538 | |
| 539 | if (error) |
| 540 | clib_error_report (error); |
| 541 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 542 | for (i = 0; i < vec_len (input_files); i++) |
| 543 | { |
| 544 | vam->ifp = fopen ((char *) input_files[i], "r"); |
| 545 | if (vam->ifp == NULL) |
| 546 | { |
| 547 | fformat (stderr, "Couldn't open input file %s\n", input_files[i]); |
| 548 | continue; |
| 549 | } |
| 550 | vam->current_file = input_files[i]; |
| 551 | vam->input_line_number = 0; |
| 552 | do_one_file (vam); |
| 553 | fclose (vam->ifp); |
| 554 | } |
| 555 | |
| 556 | if (output_file) |
| 557 | fclose (vam->ofp); |
| 558 | |
| 559 | if (interactive) |
| 560 | { |
| 561 | vam->ifp = stdin; |
| 562 | vam->ofp = stdout; |
| 563 | vam->current_file = (u8 *) "interactive"; |
| 564 | do_one_file (vam); |
| 565 | fclose (vam->ifp); |
| 566 | } |
| 567 | |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 568 | /* |
| 569 | * Particularly when running a script, don't be in a hurry to leave. |
| 570 | * A reply message queued to this process will end up constipating |
| 571 | * the allocation rings. |
| 572 | */ |
| 573 | timeout = vat_time_now (vam) + 2.0; |
| 574 | while (vam->result_ready == 0 && vat_time_now (vam) < timeout) |
| 575 | ; |
| 576 | |
| 577 | if (vat_time_now (vam) > timeout) |
| 578 | clib_warning ("BUG: message reply spin-wait timeout"); |
| 579 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 580 | vl_client_disconnect_from_vlib (); |
| 581 | exit (0); |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * fd.io coding-style-patch-verification: ON |
| 586 | * |
| 587 | * Local Variables: |
| 588 | * eval: (c-set-style "gnu") |
| 589 | * End: |
| 590 | */ |