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 | |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 47 | vlib_global_main_t vlib_global_main; |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 48 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 49 | void |
| 50 | vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...) |
| 51 | { |
| 52 | clib_warning ("BUG"); |
| 53 | } |
| 54 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 55 | static u8 * |
| 56 | format_api_error (u8 * s, va_list * args) |
| 57 | { |
| 58 | vat_main_t *vam = va_arg (*args, vat_main_t *); |
| 59 | i32 error = va_arg (*args, u32); |
| 60 | uword *p; |
| 61 | |
| 62 | p = hash_get (vam->error_string_by_error_number, -error); |
| 63 | |
| 64 | if (p) |
| 65 | s = format (s, "%s", p[0]); |
| 66 | else |
| 67 | s = format (s, "%d", error); |
| 68 | return s; |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | do_one_file (vat_main_t * vam) |
| 73 | { |
| 74 | int rv; |
| 75 | int (*fp) (vat_main_t * vam); |
| 76 | int arg_len; |
| 77 | unformat_input_t _input; |
| 78 | u8 *cmdp, *argsp; |
| 79 | uword *p; |
| 80 | u8 *this_cmd = 0; |
| 81 | |
| 82 | vam->input = &_input; |
| 83 | |
| 84 | /* Used by the "quit" command handler */ |
| 85 | if (setjmp (vam->jump_buf) != 0) |
| 86 | return; |
| 87 | |
| 88 | vam->jump_buf_set = 1; |
| 89 | |
| 90 | while (1) |
| 91 | { |
| 92 | if (vam->ifp == stdin) |
| 93 | { |
| 94 | if (vam->exec_mode == 0) |
| 95 | rv = write (1, "vat# ", 5); |
| 96 | else |
| 97 | rv = write (1, "exec# ", 6); |
| 98 | } |
| 99 | |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 100 | vec_set_len (vam->inbuf, 4096); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 101 | |
| 102 | if (vam->do_exit || |
| 103 | fgets ((char *) vam->inbuf, vec_len (vam->inbuf), vam->ifp) == 0) |
| 104 | break; |
| 105 | |
| 106 | vam->input_line_number++; |
| 107 | |
| 108 | vec_free (this_cmd); |
| 109 | |
| 110 | this_cmd = |
Damjan Marion | ffe9d21 | 2018-05-30 09:26:11 +0200 | [diff] [blame] | 111 | (u8 *) clib_macro_eval (&vam->macro_main, (i8 *) vam->inbuf, |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 112 | 1 /* complain */ , |
| 113 | 0 /* level */ , |
| 114 | 8 /* max_level */ ); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 115 | |
| 116 | if (vam->exec_mode == 0) |
| 117 | { |
| 118 | /* Split input into cmd + args */ |
| 119 | cmdp = this_cmd; |
| 120 | |
| 121 | while (cmdp < (this_cmd + vec_len (this_cmd))) |
| 122 | { |
| 123 | if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n') |
| 124 | { |
| 125 | cmdp++; |
| 126 | } |
| 127 | else |
| 128 | break; |
| 129 | } |
| 130 | argsp = cmdp; |
| 131 | while (argsp < (this_cmd + vec_len (this_cmd))) |
| 132 | { |
| 133 | if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n') |
| 134 | { |
| 135 | argsp++; |
| 136 | } |
| 137 | else |
| 138 | break; |
| 139 | } |
| 140 | *argsp++ = 0; |
| 141 | while (argsp < (this_cmd + vec_len (this_cmd))) |
| 142 | { |
| 143 | if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n') |
| 144 | { |
| 145 | argsp++; |
| 146 | } |
| 147 | else |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /* Blank input line? */ |
| 153 | if (*cmdp == 0) |
| 154 | continue; |
| 155 | |
| 156 | p = hash_get_mem (vam->function_by_name, cmdp); |
| 157 | if (p == 0) |
| 158 | { |
| 159 | errmsg ("'%s': function not found\n", cmdp); |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | arg_len = strlen ((char *) argsp); |
| 164 | |
| 165 | unformat_init_string (vam->input, (char *) argsp, arg_len); |
| 166 | fp = (void *) p[0]; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | unformat_init_string (vam->input, (char *) this_cmd, |
| 171 | strlen ((char *) this_cmd)); |
| 172 | cmdp = this_cmd; |
| 173 | fp = exec; |
| 174 | } |
| 175 | |
| 176 | rv = (*fp) (vam); |
| 177 | if (rv < 0) |
| 178 | errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv); |
| 179 | unformat_free (vam->input); |
| 180 | |
| 181 | if (vam->regenerate_interface_table) |
| 182 | { |
| 183 | vam->regenerate_interface_table = 0; |
Filip Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame] | 184 | vam->api_sw_interface_dump (vam); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 185 | } |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 186 | |
| 187 | /* Hack to pick up new client index after memfd_segment_create pivot */ |
| 188 | if (vam->client_index_invalid) |
| 189 | { |
| 190 | vat_main_t *vam = &vat_main; |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 191 | api_main_t *am = vlibapi_get_main (); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 192 | |
| 193 | vam->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 194 | vam->my_client_index = am->my_client_index; |
| 195 | vam->client_index_invalid = 0; |
| 196 | } |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | init_error_string_table (vat_main_t * vam) |
| 202 | { |
| 203 | |
| 204 | vam->error_string_by_error_number = hash_create (0, sizeof (uword)); |
| 205 | |
| 206 | #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s); |
| 207 | foreach_vnet_api_error; |
| 208 | #undef _ |
| 209 | |
| 210 | hash_set (vam->error_string_by_error_number, 99, "Misc"); |
| 211 | } |
| 212 | |
| 213 | static i8 * |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 214 | eval_current_file (clib_macro_main_t * mm, i32 complain) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 215 | { |
| 216 | vat_main_t *vam = &vat_main; |
| 217 | return ((i8 *) format (0, "%s%c", vam->current_file, 0)); |
| 218 | } |
| 219 | |
| 220 | static i8 * |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 221 | eval_current_line (clib_macro_main_t * mm, i32 complain) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 222 | { |
| 223 | vat_main_t *vam = &vat_main; |
| 224 | return ((i8 *) format (0, "%d%c", vam->input_line_number, 0)); |
| 225 | } |
| 226 | |
| 227 | static void |
| 228 | signal_handler (int signum, siginfo_t * si, ucontext_t * uc) |
| 229 | { |
| 230 | vat_main_t *vam = &vat_main; |
| 231 | |
| 232 | switch (signum) |
| 233 | { |
| 234 | /* these (caught) signals cause the application to exit */ |
| 235 | case SIGINT: |
| 236 | case SIGTERM: |
| 237 | if (vam->jump_buf_set) |
| 238 | { |
| 239 | vam->do_exit = 1; |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | /* FALLTHROUGH on purpose */ |
| 244 | |
| 245 | default: |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | _exit (1); |
| 250 | } |
| 251 | |
| 252 | static void |
| 253 | setup_signal_handlers (void) |
| 254 | { |
| 255 | uword i; |
| 256 | struct sigaction sa; |
| 257 | |
| 258 | for (i = 1; i < 32; i++) |
| 259 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 260 | clib_memset (&sa, 0, sizeof (sa)); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 261 | sa.sa_sigaction = (void *) signal_handler; |
| 262 | sa.sa_flags = SA_SIGINFO; |
| 263 | |
| 264 | switch (i) |
| 265 | { |
| 266 | /* these signals take the default action */ |
| 267 | case SIGABRT: |
| 268 | case SIGKILL: |
Vladislav Grishenko | 8486283 | 2022-03-21 02:21:42 +0500 | [diff] [blame] | 269 | case SIGCONT: |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 270 | case SIGSTOP: |
| 271 | case SIGUSR1: |
| 272 | case SIGUSR2: |
Jieqiang Wang | 6f533d7 | 2020-01-20 13:43:38 +0800 | [diff] [blame] | 273 | case SIGPROF: |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 274 | continue; |
| 275 | |
| 276 | /* ignore SIGPIPE, SIGCHLD */ |
| 277 | case SIGPIPE: |
| 278 | case SIGCHLD: |
ezkexma | 7d3161a | 2019-03-26 10:24:38 -0400 | [diff] [blame] | 279 | case SIGWINCH: |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 280 | sa.sa_sigaction = (void *) SIG_IGN; |
| 281 | break; |
| 282 | |
| 283 | /* catch and handle all other signals */ |
| 284 | default: |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | if (sigaction (i, &sa, 0) < 0) |
| 289 | clib_unix_warning ("sigaction %U", format_signal, i); |
| 290 | } |
| 291 | } |
| 292 | |
Damjan Marion | 4d2f86a | 2019-01-18 13:28:22 +0100 | [diff] [blame] | 293 | static void |
| 294 | vat_find_plugin_path () |
| 295 | { |
Damjan Marion | 4d2f86a | 2019-01-18 13:28:22 +0100 | [diff] [blame] | 296 | char *p, path[PATH_MAX]; |
| 297 | int rv; |
| 298 | u8 *s; |
| 299 | |
| 300 | /* find executable path */ |
| 301 | if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1) |
| 302 | return; |
| 303 | |
| 304 | /* readlink doesn't provide null termination */ |
| 305 | path[rv] = 0; |
| 306 | |
| 307 | /* strip filename */ |
| 308 | if ((p = strrchr (path, '/')) == 0) |
| 309 | return; |
| 310 | *p = 0; |
| 311 | |
| 312 | /* strip bin/ */ |
| 313 | if ((p = strrchr (path, '/')) == 0) |
| 314 | return; |
| 315 | *p = 0; |
| 316 | |
Damjan Marion | 5546e43 | 2021-09-30 20:04:14 +0200 | [diff] [blame] | 317 | 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] | 318 | vec_add1 (s, 0); |
| 319 | vat_plugin_path = (char *) s; |
| 320 | } |
| 321 | |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame] | 322 | static void |
| 323 | load_features (void) |
| 324 | { |
| 325 | vat_registered_features_t *f; |
| 326 | vat_main_t *vam = &vat_main; |
| 327 | clib_error_t *error; |
| 328 | |
| 329 | f = vam->feature_function_registrations; |
| 330 | |
| 331 | while (f) |
| 332 | { |
| 333 | error = f->function (vam); |
| 334 | if (error) |
| 335 | { |
| 336 | clib_warning ("INIT FAILED"); |
| 337 | } |
| 338 | f = f->next; |
| 339 | } |
| 340 | } |
| 341 | |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 342 | static inline clib_error_t * |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 343 | call_init_exit_functions_internal (vlib_main_t *vm, |
| 344 | _vlib_init_function_list_elt_t **headp, |
| 345 | int call_once, int do_sort, int is_global) |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 346 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 347 | vlib_global_main_t *vgm = vlib_get_global_main (); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 348 | clib_error_t *error = 0; |
| 349 | _vlib_init_function_list_elt_t *i; |
| 350 | |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 351 | ASSERT (is_global == 1); |
| 352 | |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 353 | #if 0 |
| 354 | /* Not worth copying the topological sort code */ |
| 355 | if (do_sort && (error = vlib_sort_init_exit_functions (headp))) |
| 356 | return (error); |
| 357 | #endif |
| 358 | |
| 359 | i = *headp; |
| 360 | while (i) |
| 361 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 362 | if (call_once && !hash_get (vgm->init_functions_called, i->f)) |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 363 | { |
| 364 | if (call_once) |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 365 | hash_set1 (vgm->init_functions_called, i->f); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 366 | error = i->f (vm); |
| 367 | if (error) |
| 368 | return error; |
| 369 | } |
| 370 | i = i->next_init_function; |
| 371 | } |
| 372 | return error; |
| 373 | } |
| 374 | |
| 375 | clib_error_t * |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 376 | vlib_call_init_exit_functions (vlib_main_t *vm, |
| 377 | _vlib_init_function_list_elt_t **headp, |
| 378 | int call_once, int is_global) |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 379 | { |
| 380 | return call_init_exit_functions_internal (vm, headp, call_once, |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 381 | 1 /* do_sort */, is_global); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Filip Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame] | 384 | static void |
| 385 | vat_register_interface_dump (vat_main_t *vam) |
| 386 | { |
| 387 | void *handle; |
| 388 | plugin_info_t *pi; |
| 389 | |
| 390 | vec_foreach (pi, vat_plugin_main.plugin_info) |
| 391 | { |
| 392 | handle = dlsym (pi->handle, "api_sw_interface_dump"); |
| 393 | if (handle) |
| 394 | { |
| 395 | vam->api_sw_interface_dump = handle; |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (!vam->api_sw_interface_dump) |
| 401 | { |
| 402 | fformat (stderr, |
| 403 | "sw_interface_dump not found in interface_test plugin!\n"); |
| 404 | exit (1); |
| 405 | } |
| 406 | } |
| 407 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 408 | int |
| 409 | main (int argc, char **argv) |
| 410 | { |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 411 | vlib_global_main_t *vgm = vlib_get_global_main (); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 412 | vat_main_t *vam = &vat_main; |
| 413 | unformat_input_t _argv, *a = &_argv; |
| 414 | u8 **input_files = 0; |
| 415 | u8 *output_file = 0; |
| 416 | u8 *chroot_prefix; |
| 417 | u8 *this_input_file; |
| 418 | u8 interactive = 1; |
| 419 | u8 json_output = 0; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 420 | int i; |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 421 | f64 timeout; |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 422 | clib_error_t *error; |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 423 | vlib_main_t *vm; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 424 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 425 | clib_mem_init_thread_safe (0, 128 << 20); |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 426 | vlib_main_init (); |
| 427 | vm = vlib_get_first_main (); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 428 | |
| 429 | clib_macro_init (&vam->macro_main); |
| 430 | clib_macro_add_builtin (&vam->macro_main, "current_file", |
| 431 | eval_current_file); |
| 432 | clib_macro_add_builtin (&vam->macro_main, "current_line", |
| 433 | eval_current_line); |
| 434 | |
| 435 | init_error_string_table (vam); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 436 | vec_validate (vam->cmd_reply, 0); |
| 437 | vec_reset_length (vam->cmd_reply); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 438 | |
Damjan Marion | 4d2f86a | 2019-01-18 13:28:22 +0100 | [diff] [blame] | 439 | vat_find_plugin_path (); |
| 440 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 441 | unformat_init_command_line (a, argv); |
| 442 | |
| 443 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 444 | { |
| 445 | if (unformat (a, "in %s", &this_input_file)) |
| 446 | vec_add1 (input_files, this_input_file); |
| 447 | else if (unformat (a, "out %s", &output_file)) |
| 448 | ; |
| 449 | else if (unformat (a, "script")) |
| 450 | interactive = 0; |
| 451 | else if (unformat (a, "json")) |
| 452 | json_output = 1; |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 453 | else if (unformat (a, "socket-name %s", &vam->socket_name)) |
| 454 | ; |
| 455 | else if (unformat (a, "default-socket")) |
| 456 | { |
| 457 | vam->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0); |
| 458 | } |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 459 | else if (unformat (a, "plugin_path %s", (u8 *) & vat_plugin_path)) |
| 460 | vec_add1 (vat_plugin_path, 0); |
| 461 | else if (unformat (a, "plugin_name_filter %s", |
| 462 | (u8 *) & vat_plugin_name_filter)) |
| 463 | vec_add1 (vat_plugin_name_filter, 0); |
| 464 | else if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
| 465 | { |
| 466 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 467 | } |
| 468 | else |
| 469 | { |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 470 | fformat |
| 471 | (stderr, |
| 472 | "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n" |
| 473 | "[plugin_path <path>][default-socket][socket-name <name>]\n" |
| 474 | "[plugin_name_filter <filter>][chroot prefix <path>]\n", |
| 475 | argv[0]); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 476 | exit (1); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | if (output_file) |
| 481 | vam->ofp = fopen ((char *) output_file, "w"); |
| 482 | else |
| 483 | vam->ofp = stdout; |
| 484 | |
| 485 | if (vam->ofp == NULL) |
| 486 | { |
| 487 | fformat (stderr, "Couldn't open output file %s\n", |
| 488 | output_file ? (char *) output_file : "stdout"); |
| 489 | exit (1); |
| 490 | } |
| 491 | |
| 492 | clib_time_init (&vam->clib_time); |
| 493 | |
| 494 | vat_api_hookup (vam); |
| 495 | vat_plugin_api_reference (); |
| 496 | |
| 497 | setup_signal_handlers (); |
| 498 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 499 | if (vam->socket_name && vat_socket_connect (vam)) |
| 500 | fformat (stderr, "WARNING: socket connection failed"); |
| 501 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 502 | if ((!vam->socket_client_main || vam->socket_client_main->socket_fd == 0) |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 503 | && connect_to_vpe ("vpp_api_test") < 0) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 504 | { |
| 505 | svm_region_exit (); |
| 506 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 507 | exit (1); |
| 508 | } |
| 509 | |
| 510 | vam->json_output = json_output; |
| 511 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 512 | vec_validate (vam->inbuf, 4096); |
| 513 | |
Ole Troan | 3d81267 | 2020-09-15 10:53:34 +0200 | [diff] [blame] | 514 | load_features (); |
| 515 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 516 | vam->current_file = (u8 *) "plugin-init"; |
| 517 | vat_plugin_init (vam); |
| 518 | |
Filip Tehlar | f0e67d7 | 2021-07-23 22:03:05 +0000 | [diff] [blame] | 519 | vat_register_interface_dump (vam); |
| 520 | |
| 521 | if (!json_output) |
| 522 | vam->api_sw_interface_dump (vam); |
| 523 | |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 524 | /* Set up the init function hash table */ |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 525 | vgm->init_functions_called = hash_create (0, 0); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 526 | |
| 527 | /* Execute plugin init and api_init functions */ |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 528 | error = vlib_call_init_exit_functions (vm, &vgm->init_function_registrations, |
| 529 | 1 /* call once */, 1 /* is_global*/); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 530 | |
| 531 | if (error) |
| 532 | clib_error_report (error); |
| 533 | |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 534 | error = |
| 535 | vlib_call_init_exit_functions (vm, &vgm->api_init_function_registrations, |
| 536 | 1 /* call_once */, 1 /* is_global */); |
Jon Loeliger | c0b1954 | 2020-05-11 08:43:51 -0500 | [diff] [blame] | 537 | |
| 538 | if (error) |
| 539 | clib_error_report (error); |
| 540 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 541 | for (i = 0; i < vec_len (input_files); i++) |
| 542 | { |
| 543 | vam->ifp = fopen ((char *) input_files[i], "r"); |
| 544 | if (vam->ifp == NULL) |
| 545 | { |
| 546 | fformat (stderr, "Couldn't open input file %s\n", input_files[i]); |
| 547 | continue; |
| 548 | } |
| 549 | vam->current_file = input_files[i]; |
| 550 | vam->input_line_number = 0; |
| 551 | do_one_file (vam); |
| 552 | fclose (vam->ifp); |
| 553 | } |
| 554 | |
| 555 | if (output_file) |
| 556 | fclose (vam->ofp); |
| 557 | |
| 558 | if (interactive) |
| 559 | { |
| 560 | vam->ifp = stdin; |
| 561 | vam->ofp = stdout; |
| 562 | vam->current_file = (u8 *) "interactive"; |
| 563 | do_one_file (vam); |
| 564 | fclose (vam->ifp); |
| 565 | } |
| 566 | |
Dave Barach | cf5e848 | 2017-10-17 11:48:29 -0400 | [diff] [blame] | 567 | /* |
| 568 | * Particularly when running a script, don't be in a hurry to leave. |
| 569 | * A reply message queued to this process will end up constipating |
| 570 | * the allocation rings. |
| 571 | */ |
| 572 | timeout = vat_time_now (vam) + 2.0; |
| 573 | while (vam->result_ready == 0 && vat_time_now (vam) < timeout) |
| 574 | ; |
| 575 | |
| 576 | if (vat_time_now (vam) > timeout) |
| 577 | clib_warning ("BUG: message reply spin-wait timeout"); |
| 578 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 579 | vl_client_disconnect_from_vlib (); |
| 580 | exit (0); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * fd.io coding-style-patch-verification: ON |
| 585 | * |
| 586 | * Local Variables: |
| 587 | * eval: (c-set-style "gnu") |
| 588 | * End: |
| 589 | */ |