Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [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 | |
| 18 | vat_main_t vat_main; |
| 19 | |
| 20 | int connect_to_vpe(char *name) |
| 21 | { |
| 22 | vat_main_t * vam = &vat_main; |
| 23 | api_main_t * am = &api_main; |
| 24 | |
| 25 | if (vl_client_connect_to_vlib("/vpe-api", name, 32) < 0) |
| 26 | return -1; |
| 27 | |
| 28 | vam->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 29 | vam->my_client_index = am->my_client_index; |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | void vlib_cli_output(struct vlib_main_t * vm, char * fmt, ...) |
| 35 | { clib_warning ("BUG");} |
| 36 | |
| 37 | |
| 38 | static u8 * format_api_error (u8 * s, va_list * args) |
| 39 | { |
| 40 | vat_main_t * vam = va_arg (*args, vat_main_t *); |
| 41 | i32 error = va_arg (*args, u32); |
| 42 | uword * p; |
| 43 | |
| 44 | p = hash_get (vam->error_string_by_error_number, -error); |
| 45 | |
| 46 | if (p) |
| 47 | s = format (s, "%s", p[0]); |
| 48 | else |
| 49 | s = format (s, "%d", error); |
| 50 | return s; |
| 51 | } |
| 52 | |
| 53 | void do_one_file (vat_main_t * vam) |
| 54 | { |
| 55 | int rv; |
| 56 | int (*fp)(vat_main_t *vam); |
| 57 | int arg_len; |
| 58 | unformat_input_t _input; |
| 59 | u8 *cmdp, *argsp; |
| 60 | uword * p; |
| 61 | u8 * this_cmd = 0; |
| 62 | |
| 63 | vam->input = &_input; |
| 64 | |
| 65 | /* Used by the "quit" command handler */ |
| 66 | if (setjmp (vam->jump_buf) != 0) |
| 67 | return; |
| 68 | |
| 69 | while (1) { |
| 70 | if (vam->ifp == stdin) { |
| 71 | if (vam->exec_mode == 0) |
| 72 | rv = write (1, "vat# ", 5); |
| 73 | else |
| 74 | rv = write (1, "exec# ", 6); |
| 75 | } |
| 76 | |
| 77 | _vec_len(vam->inbuf) = 4096; |
| 78 | |
| 79 | if (fgets ((char *)vam->inbuf, vec_len(vam->inbuf), vam->ifp) == 0) |
| 80 | break; |
| 81 | |
| 82 | vam->input_line_number ++; |
| 83 | |
| 84 | vec_free (this_cmd); |
| 85 | |
| 86 | this_cmd = (u8 *) clib_macro_eval (&vam->macro_main, (char *)vam->inbuf, |
| 87 | 1 /* complain */); |
| 88 | |
| 89 | if (vam->exec_mode == 0) { |
| 90 | /* Split input into cmd + args */ |
| 91 | cmdp = this_cmd; |
| 92 | |
| 93 | while (cmdp < (this_cmd + vec_len(this_cmd))) { |
| 94 | if (*cmdp == ' ' || *cmdp == '\t' || *cmdp == '\n') { |
| 95 | cmdp++; |
| 96 | } else |
| 97 | break; |
| 98 | } |
| 99 | argsp = cmdp; |
| 100 | while (argsp < (this_cmd + vec_len(this_cmd))) { |
| 101 | if (*argsp != ' ' && *argsp != '\t' && *argsp != '\n') { |
| 102 | argsp++; |
| 103 | } else |
| 104 | break; |
| 105 | } |
| 106 | *argsp++ = 0; |
| 107 | while (argsp < (this_cmd + vec_len(this_cmd))) { |
| 108 | if (*argsp == ' ' || *argsp == '\t' || *argsp == '\n') { |
| 109 | argsp++; |
| 110 | } else |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /* Blank input line? */ |
| 116 | if (*cmdp == 0) |
| 117 | continue; |
| 118 | |
| 119 | p = hash_get_mem (vam->function_by_name, cmdp); |
| 120 | if (p == 0) { |
| 121 | errmsg ("'%s': function not found\n", cmdp); |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | arg_len = strlen((char *) argsp); |
| 126 | |
| 127 | unformat_init_string (vam->input, (char *)argsp, arg_len); |
| 128 | fp = (void *)p[0]; |
| 129 | } else { |
| 130 | unformat_init_string (vam->input, (char *) this_cmd, |
| 131 | strlen((char *) this_cmd)); |
| 132 | cmdp = this_cmd; |
| 133 | fp = exec; |
| 134 | } |
| 135 | |
| 136 | rv = (*fp)(vam); |
| 137 | if (rv < 0) |
| 138 | errmsg ("%s error: %U\n", cmdp, format_api_error, vam, rv); |
| 139 | unformat_free (vam->input); |
| 140 | |
| 141 | if (vam->regenerate_interface_table) { |
| 142 | vam->regenerate_interface_table = 0; |
| 143 | api_sw_interface_dump (vam); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void init_error_string_table (vat_main_t * vam) |
| 149 | { |
| 150 | |
| 151 | vam->error_string_by_error_number = hash_create (0, sizeof(uword)); |
| 152 | |
| 153 | #define _(n,v,s) hash_set (vam->error_string_by_error_number, -v, s); |
| 154 | foreach_vnet_api_error; |
| 155 | #undef _ |
| 156 | |
| 157 | hash_set (vam->error_string_by_error_number, 99, "Misc"); |
| 158 | } |
| 159 | |
| 160 | static i8 *eval_current_file (macro_main_t *mm, i32 complain) |
| 161 | { |
| 162 | vat_main_t * vam = &vat_main; |
| 163 | return ((i8 *) format (0, "%s%c", vam->current_file, 0)); |
| 164 | } |
| 165 | |
| 166 | static i8 *eval_current_line (macro_main_t *mm, i32 complain) |
| 167 | { |
| 168 | vat_main_t * vam = &vat_main; |
| 169 | return ((i8 *) format (0, "%d%c", vam->input_line_number, 0)); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | int main (int argc, char ** argv) |
| 174 | { |
| 175 | vat_main_t * vam = &vat_main; |
| 176 | unformat_input_t _argv, *a = &_argv; |
| 177 | u8 **input_files = 0; |
| 178 | u8 *output_file = 0; |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 179 | u8 *chroot_prefix; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | u8 *this_input_file; |
| 181 | u8 interactive = 1; |
| 182 | u8 json_output = 0; |
| 183 | u8 * heap; |
| 184 | mheap_t * h; |
| 185 | int i; |
| 186 | |
| 187 | clib_mem_init (0, 128<<20); |
| 188 | |
| 189 | heap = clib_mem_get_per_cpu_heap(); |
| 190 | h = mheap_header (heap); |
| 191 | |
| 192 | /* make the main heap thread-safe */ |
| 193 | h->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 194 | |
| 195 | clib_macro_init (&vam->macro_main); |
| 196 | clib_macro_add_builtin (&vam->macro_main, "current_file", eval_current_file); |
| 197 | clib_macro_add_builtin (&vam->macro_main, "current_line", eval_current_line); |
| 198 | |
| 199 | init_error_string_table (vam); |
| 200 | |
| 201 | unformat_init_command_line (a, argv); |
| 202 | |
| 203 | while (unformat_check_input(a) != UNFORMAT_END_OF_INPUT) { |
| 204 | if (unformat (a, "in %s", &this_input_file)) |
| 205 | vec_add1 (input_files, this_input_file); |
| 206 | else if (unformat (a, "out %s", &output_file)) |
| 207 | ; |
| 208 | else if (unformat (a, "script")) |
| 209 | interactive = 0; |
| 210 | else if (unformat (a, "json")) |
| 211 | json_output = 1; |
| 212 | else if (unformat (a, "plugin_path %s", (u8 *)&vat_plugin_path)) |
| 213 | vec_add1 (vat_plugin_path, 0); |
| 214 | else if (unformat (a, "plugin_name_filter %s", |
| 215 | (u8 *)&vat_plugin_name_filter)) |
| 216 | vec_add1 (vat_plugin_name_filter, 0); |
Dave Barach | 309bef2 | 2016-01-22 16:09:52 -0500 | [diff] [blame] | 217 | else if (unformat (a, "chroot prefix %s", &chroot_prefix)) { |
| 218 | vl_set_memory_root_path ((char *)chroot_prefix); |
| 219 | } else { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | fformat (stderr, |
| 221 | "%s: usage [in <f1> ... in <fn>] [out <fn>] [script] [json]\n"); |
| 222 | exit (1); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (output_file) |
| 227 | vam->ofp = fopen ((char *) output_file, "w"); |
| 228 | else |
| 229 | vam->ofp = stdout; |
| 230 | |
| 231 | if (vam->ofp == NULL) { |
| 232 | fformat (stderr, "Couldn't open output file %s\n", |
| 233 | output_file ? (char *) output_file : "stdout"); |
| 234 | exit (1); |
| 235 | } |
| 236 | |
| 237 | clib_time_init (&vam->clib_time); |
| 238 | |
| 239 | vat_api_hookup(vam); |
| 240 | vat_plugin_api_reference(); |
| 241 | |
Damjan Marion | 08ff7e0 | 2016-01-20 13:45:36 +0100 | [diff] [blame] | 242 | if (connect_to_vpe("vpp_api_test") < 0) { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | svm_region_exit(); |
| 244 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 245 | exit (1); |
| 246 | } |
| 247 | |
| 248 | vam->json_output = json_output; |
| 249 | |
| 250 | if (!json_output) { |
| 251 | api_sw_interface_dump (vam); |
| 252 | } |
| 253 | |
| 254 | vec_validate (vam->inbuf, 4096); |
| 255 | |
| 256 | vam->current_file = (u8 *) "plugin-init"; |
| 257 | vat_plugin_init (vam); |
| 258 | |
| 259 | for (i = 0; i < vec_len (input_files); i++) { |
| 260 | vam->ifp = fopen ((char *) input_files[i], "r"); |
| 261 | if (vam->ifp == NULL) { |
| 262 | fformat (stderr, "Couldn't open input file %s\n", |
| 263 | input_files[i]); |
| 264 | continue; |
| 265 | } |
| 266 | vam->current_file = input_files[i]; |
| 267 | vam->input_line_number = 0; |
| 268 | do_one_file (vam); |
| 269 | fclose (vam->ifp); |
| 270 | } |
| 271 | |
| 272 | if (output_file) |
| 273 | fclose (vam->ofp); |
| 274 | |
| 275 | if (interactive) { |
| 276 | vam->ifp = stdin; |
| 277 | vam->ofp = stdout; |
| 278 | vam->current_file = (u8 *) "interactive"; |
| 279 | do_one_file (vam); |
| 280 | fclose(vam->ifp); |
| 281 | } |
| 282 | |
| 283 | vl_client_disconnect_from_vlib(); |
| 284 | exit (0); |
| 285 | } |