Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * plugin.c: plugin handling |
| 3 | * |
| 4 | * Copyright (c) 2011 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vlib/unix/plugin.h> |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 19 | #include <vppinfra/elf.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | #include <dlfcn.h> |
| 21 | #include <dirent.h> |
| 22 | |
| 23 | plugin_main_t vlib_plugin_main; |
| 24 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 25 | char *vlib_plugin_path __attribute__ ((weak)); |
| 26 | char *vlib_plugin_path = ""; |
| 27 | char *vlib_plugin_app_version __attribute__ ((weak)); |
| 28 | char *vlib_plugin_app_version = ""; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 30 | void * |
| 31 | vlib_get_plugin_symbol (char *plugin_name, char *symbol_name) |
| 32 | { |
| 33 | plugin_main_t *pm = &vlib_plugin_main; |
| 34 | uword *p; |
| 35 | plugin_info_t *pi; |
| 36 | |
| 37 | if ((p = hash_get_mem (pm->plugin_by_name_hash, plugin_name)) == 0) |
| 38 | return 0; |
| 39 | |
| 40 | pi = vec_elt_at_index (pm->plugin_info, p[0]); |
| 41 | return dlsym (pi->handle, symbol_name); |
| 42 | } |
| 43 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 44 | static char * |
| 45 | str_array_to_vec (char *array, int len) |
| 46 | { |
| 47 | char c, *r = 0; |
| 48 | int n = 0; |
| 49 | |
| 50 | do |
| 51 | { |
| 52 | c = array[n]; |
| 53 | vec_add1 (r, c); |
| 54 | } |
| 55 | while (c && ++n < len); |
| 56 | |
| 57 | if (c) |
| 58 | vec_add1 (r, 0); |
| 59 | |
| 60 | return r; |
| 61 | } |
| 62 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 63 | static int |
| 64 | load_one_plugin (plugin_main_t * pm, plugin_info_t * pi, int from_early_init) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | { |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 66 | void *handle; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 67 | clib_error_t *error; |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 68 | elf_main_t em = { 0 }; |
| 69 | elf_section_t *section; |
| 70 | u8 *data; |
| 71 | char *version_required; |
| 72 | vlib_plugin_registration_t *reg; |
| 73 | plugin_config_t *pc = 0; |
| 74 | uword *p; |
| 75 | |
| 76 | if (elf_read_file (&em, (char *) pi->filename)) |
| 77 | return -1; |
| 78 | |
| 79 | error = elf_get_section_by_name (&em, ".vlib_plugin_registration", |
| 80 | §ion); |
| 81 | if (error) |
| 82 | { |
| 83 | clib_warning ("Not a plugin: %s\n", (char *) pi->name); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | data = elf_get_section_contents (&em, section->index, 1); |
| 88 | reg = (vlib_plugin_registration_t *) data; |
| 89 | |
| 90 | if (vec_len (data) != sizeof (*reg)) |
| 91 | { |
| 92 | clib_warning ("vlib_plugin_registration size mismatch in plugin %s\n", |
| 93 | (char *) pi->name); |
| 94 | goto error; |
| 95 | } |
| 96 | |
Mohsin Kazmi | 06bc260 | 2018-03-22 23:45:23 +0100 | [diff] [blame] | 97 | if (pm->plugins_default_disable) |
| 98 | reg->default_disabled = 1; |
| 99 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 100 | p = hash_get_mem (pm->config_index_by_name, pi->name); |
| 101 | if (p) |
| 102 | { |
| 103 | pc = vec_elt_at_index (pm->configs, p[0]); |
| 104 | if (pc->is_disabled) |
| 105 | { |
| 106 | clib_warning ("Plugin disabled: %s", pi->name); |
| 107 | goto error; |
| 108 | } |
| 109 | if (reg->default_disabled && pc->is_enabled == 0) |
| 110 | { |
Damjan Marion | 1bfb0dd | 2017-03-22 11:08:39 +0100 | [diff] [blame] | 111 | clib_warning ("Plugin disabled (default): %s", pi->name); |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 112 | goto error; |
| 113 | } |
| 114 | } |
| 115 | else if (reg->default_disabled) |
| 116 | { |
Damjan Marion | 1bfb0dd | 2017-03-22 11:08:39 +0100 | [diff] [blame] | 117 | clib_warning ("Plugin disabled (default): %s", pi->name); |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 118 | goto error; |
| 119 | } |
| 120 | |
| 121 | version_required = str_array_to_vec ((char *) ®->version_required, |
| 122 | sizeof (reg->version_required)); |
| 123 | |
| 124 | if ((strlen (version_required) > 0) && |
| 125 | (strncmp (vlib_plugin_app_version, version_required, |
| 126 | strlen (version_required)))) |
| 127 | { |
| 128 | clib_warning ("Plugin %s version mismatch: %s != %s", |
| 129 | pi->name, vlib_plugin_app_version, reg->version_required); |
| 130 | if (!(pc && pc->skip_version_check == 1)) |
| 131 | { |
| 132 | vec_free (version_required); |
| 133 | goto error; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | vec_free (version_required); |
| 138 | vec_free (data); |
| 139 | elf_main_free (&em); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 141 | handle = dlopen ((char *) pi->filename, RTLD_LAZY); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 142 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | if (handle == 0) |
| 144 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 145 | clib_warning ("%s", dlerror ()); |
Damjan Marion | c9fc77c | 2017-04-25 21:07:52 +0200 | [diff] [blame] | 146 | clib_warning ("Failed to load plugin '%s'", pi->name); |
Dave Barach | 3464c86 | 2018-03-12 17:38:31 -0400 | [diff] [blame] | 147 | goto error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 149 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | pi->handle = handle; |
| 151 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 152 | reg = dlsym (pi->handle, "vlib_plugin_registration"); |
Ole Troan | 3b3688f | 2016-06-15 14:29:08 +0200 | [diff] [blame] | 153 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 154 | if (reg == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | { |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 156 | /* This should never happen unless somebody chagnes registration macro */ |
| 157 | clib_warning ("Missing plugin registration in plugin '%s'", pi->name); |
| 158 | os_exit (1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 161 | pi->reg = reg; |
| 162 | pi->version = str_array_to_vec ((char *) ®->version, |
| 163 | sizeof (reg->version)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | |
Ole Troan | e4ad8cc | 2017-02-07 11:22:33 +0100 | [diff] [blame] | 165 | if (reg->early_init) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | { |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 167 | clib_error_t *(*ei) (vlib_main_t *); |
| 168 | void *h; |
| 169 | |
| 170 | h = dlsym (pi->handle, reg->early_init); |
| 171 | if (h) |
| 172 | { |
| 173 | ei = h; |
| 174 | error = (*ei) (pm->vlib_main); |
| 175 | if (error) |
| 176 | { |
| 177 | clib_error_report (error); |
| 178 | os_exit (1); |
| 179 | } |
| 180 | } |
| 181 | else |
| 182 | clib_warning ("Plugin %s: early init function %s set but not found", |
| 183 | (char *) pi->name, reg->early_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Damjan Marion | 1bfb0dd | 2017-03-22 11:08:39 +0100 | [diff] [blame] | 186 | if (reg->description) |
| 187 | clib_warning ("Loaded plugin: %s (%s)", pi->name, reg->description); |
| 188 | else |
| 189 | clib_warning ("Loaded plugin: %s", pi->name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | |
| 191 | return 0; |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 192 | error: |
| 193 | vec_free (data); |
| 194 | elf_main_free (&em); |
| 195 | return -1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 198 | static u8 ** |
| 199 | split_plugin_path (plugin_main_t * pm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | { |
| 201 | int i; |
| 202 | u8 **rv = 0; |
| 203 | u8 *path = pm->plugin_path; |
| 204 | u8 *this = 0; |
| 205 | |
| 206 | for (i = 0; i < vec_len (pm->plugin_path); i++) |
| 207 | { |
| 208 | if (path[i] != ':') |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 209 | { |
| 210 | vec_add1 (this, path[i]); |
| 211 | continue; |
| 212 | } |
| 213 | vec_add1 (this, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | vec_add1 (rv, this); |
| 215 | this = 0; |
| 216 | } |
| 217 | if (this) |
| 218 | { |
| 219 | vec_add1 (this, 0); |
| 220 | vec_add1 (rv, this); |
| 221 | } |
| 222 | return rv; |
| 223 | } |
| 224 | |
Dave Barach | a83bc30 | 2017-02-25 16:38:12 -0500 | [diff] [blame] | 225 | static int |
| 226 | plugin_name_sort_cmp (void *a1, void *a2) |
| 227 | { |
| 228 | plugin_info_t *p1 = a1; |
| 229 | plugin_info_t *p2 = a2; |
| 230 | |
| 231 | return strcmp ((char *) p1->name, (char *) p2->name); |
| 232 | } |
| 233 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 234 | int |
| 235 | vlib_load_new_plugins (plugin_main_t * pm, int from_early_init) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | { |
| 237 | DIR *dp; |
| 238 | struct dirent *entry; |
| 239 | struct stat statb; |
| 240 | uword *p; |
| 241 | plugin_info_t *pi; |
| 242 | u8 **plugin_path; |
Dave Barach | a83bc30 | 2017-02-25 16:38:12 -0500 | [diff] [blame] | 243 | u32 *load_fail_indices = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | int i; |
| 245 | |
| 246 | plugin_path = split_plugin_path (pm); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 247 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 248 | for (i = 0; i < vec_len (plugin_path); i++) |
| 249 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 250 | dp = opendir ((char *) plugin_path[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 252 | if (dp == 0) |
| 253 | continue; |
| 254 | |
| 255 | while ((entry = readdir (dp))) |
| 256 | { |
| 257 | u8 *plugin_name; |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 258 | u8 *filename; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 259 | |
| 260 | if (pm->plugin_name_filter) |
| 261 | { |
| 262 | int j; |
| 263 | for (j = 0; j < vec_len (pm->plugin_name_filter); j++) |
| 264 | if (entry->d_name[j] != pm->plugin_name_filter[j]) |
| 265 | goto next; |
| 266 | } |
| 267 | |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 268 | filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0); |
Ole Troan | 3b3688f | 2016-06-15 14:29:08 +0200 | [diff] [blame] | 269 | |
| 270 | /* Only accept .so */ |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 271 | char *ext = strrchr ((const char *) filename, '.'); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 272 | /* unreadable */ |
| 273 | if (!ext || (strcmp (ext, ".so") != 0) || |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 274 | stat ((char *) filename, &statb) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 275 | { |
| 276 | ignore: |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 277 | vec_free (filename); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 278 | continue; |
| 279 | } |
| 280 | |
| 281 | /* a dir or other things which aren't plugins */ |
| 282 | if (!S_ISREG (statb.st_mode)) |
| 283 | goto ignore; |
| 284 | |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 285 | plugin_name = format (0, "%s%c", entry->d_name, 0); |
Dave Barach | a83bc30 | 2017-02-25 16:38:12 -0500 | [diff] [blame] | 286 | /* Have we seen this plugin already? */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 287 | p = hash_get_mem (pm->plugin_by_name_hash, plugin_name); |
| 288 | if (p == 0) |
| 289 | { |
Dave Barach | a83bc30 | 2017-02-25 16:38:12 -0500 | [diff] [blame] | 290 | /* No, add it to the plugin vector */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 291 | vec_add2 (pm->plugin_info, pi, 1); |
| 292 | pi->name = plugin_name; |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 293 | pi->filename = filename; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 294 | pi->file_info = statb; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 295 | hash_set_mem (pm->plugin_by_name_hash, plugin_name, |
| 296 | pi - pm->plugin_info); |
| 297 | } |
| 298 | next: |
| 299 | ; |
| 300 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | closedir (dp); |
| 302 | vec_free (plugin_path[i]); |
| 303 | } |
| 304 | vec_free (plugin_path); |
Dave Barach | a83bc30 | 2017-02-25 16:38:12 -0500 | [diff] [blame] | 305 | |
| 306 | |
| 307 | /* |
| 308 | * Sort the plugins by name. This is important. |
| 309 | * API traces contain absolute message numbers. |
| 310 | * Loading plugins in directory (vs. alphabetical) order |
| 311 | * makes trace replay incredibly fragile. |
| 312 | */ |
| 313 | vec_sort_with_function (pm->plugin_info, plugin_name_sort_cmp); |
| 314 | |
| 315 | /* |
| 316 | * Attempt to load the plugins |
| 317 | */ |
| 318 | for (i = 0; i < vec_len (pm->plugin_info); i++) |
| 319 | { |
| 320 | pi = vec_elt_at_index (pm->plugin_info, i); |
| 321 | |
| 322 | if (load_one_plugin (pm, pi, from_early_init)) |
| 323 | { |
| 324 | /* Make a note of any which fail to load */ |
| 325 | vec_add1 (load_fail_indices, i); |
| 326 | hash_unset_mem (pm->plugin_by_name_hash, pi->name); |
| 327 | vec_free (pi->name); |
| 328 | vec_free (pi->filename); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* Remove plugin info vector elements corresponding to load failures */ |
| 333 | if (vec_len (load_fail_indices) > 0) |
| 334 | { |
| 335 | for (i = vec_len (load_fail_indices) - 1; i >= 0; i--) |
| 336 | vec_delete (pm->plugin_info, 1, load_fail_indices[i]); |
| 337 | vec_free (load_fail_indices); |
| 338 | } |
| 339 | |
| 340 | /* Recreate the plugin name hash */ |
| 341 | for (i = 0; i < vec_len (pm->plugin_info); i++) |
| 342 | { |
| 343 | pi = vec_elt_at_index (pm->plugin_info, i); |
| 344 | hash_unset_mem (pm->plugin_by_name_hash, pi->name); |
| 345 | hash_set_mem (pm->plugin_by_name_hash, pi->name, pi - pm->plugin_info); |
| 346 | } |
| 347 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | return 0; |
| 349 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 350 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 351 | int |
| 352 | vlib_plugin_early_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | { |
| 354 | plugin_main_t *pm = &vlib_plugin_main; |
| 355 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 356 | if (pm->plugin_path == 0) |
| 357 | pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | |
| 359 | clib_warning ("plugin path %s", pm->plugin_path); |
| 360 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword)); |
| 362 | pm->vlib_main = vm; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 363 | |
| 364 | return vlib_load_new_plugins (pm, 1 /* from_early_init */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 365 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 366 | |
Dave Barach | 0a32b60 | 2017-11-28 18:55:09 -0500 | [diff] [blame] | 367 | u8 * |
| 368 | vlib_get_vat_plugin_path (void) |
| 369 | { |
| 370 | plugin_main_t *pm = &vlib_plugin_main; |
| 371 | return (pm->vat_plugin_path); |
| 372 | } |
| 373 | |
| 374 | u8 * |
| 375 | vlib_get_vat_plugin_name_filter (void) |
| 376 | { |
| 377 | plugin_main_t *pm = &vlib_plugin_main; |
| 378 | return (pm->vat_plugin_name_filter); |
| 379 | } |
| 380 | |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 381 | static clib_error_t * |
| 382 | vlib_plugins_show_cmd_fn (vlib_main_t * vm, |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 383 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 384 | { |
| 385 | plugin_main_t *pm = &vlib_plugin_main; |
| 386 | u8 *s = 0; |
| 387 | u8 *key = 0; |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 388 | uword value = 0; |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 389 | int index = 1; |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 390 | plugin_info_t *pi; |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 391 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 392 | s = format (s, " Plugin path is: %s\n\n", pm->plugin_path); |
Damjan Marion | 1bfb0dd | 2017-03-22 11:08:39 +0100 | [diff] [blame] | 393 | s = format (s, " %-41s%-33s%s\n", "Plugin", "Version", "Description"); |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 394 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 395 | /* *INDENT-OFF* */ |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 396 | hash_foreach_mem (key, value, pm->plugin_by_name_hash, |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 397 | { |
| 398 | if (key != 0) |
| 399 | { |
| 400 | pi = vec_elt_at_index (pm->plugin_info, value); |
Damjan Marion | 1bfb0dd | 2017-03-22 11:08:39 +0100 | [diff] [blame] | 401 | s = format (s, "%3d. %-40s %-32s %s\n", index, key, pi->version, |
| 402 | pi->reg->description ? pi->reg->description : ""); |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 403 | index++; |
| 404 | } |
| 405 | }); |
| 406 | /* *INDENT-ON* */ |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 407 | |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 408 | vlib_cli_output (vm, "%v", s); |
| 409 | vec_free (s); |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 413 | /* *INDENT-OFF* */ |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 414 | VLIB_CLI_COMMAND (plugins_show_cmd, static) = |
| 415 | { |
Dave Barach | fe6bdfd | 2017-01-20 19:50:09 -0500 | [diff] [blame] | 416 | .path = "show plugins", |
| 417 | .short_help = "show loaded plugins", |
| 418 | .function = vlib_plugins_show_cmd_fn, |
| 419 | }; |
| 420 | /* *INDENT-ON* */ |
| 421 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 422 | static clib_error_t * |
| 423 | config_one_plugin (vlib_main_t * vm, char *name, unformat_input_t * input) |
| 424 | { |
| 425 | plugin_main_t *pm = &vlib_plugin_main; |
| 426 | plugin_config_t *pc; |
| 427 | clib_error_t *error = 0; |
| 428 | uword *p; |
| 429 | int is_enable = 0; |
| 430 | int is_disable = 0; |
| 431 | int skip_version_check = 0; |
| 432 | |
| 433 | if (pm->config_index_by_name == 0) |
| 434 | pm->config_index_by_name = hash_create_string (0, sizeof (uword)); |
| 435 | |
| 436 | p = hash_get_mem (pm->config_index_by_name, name); |
| 437 | |
| 438 | if (p) |
| 439 | { |
| 440 | error = clib_error_return (0, "plugin '%s' already configured", name); |
| 441 | goto done; |
| 442 | } |
| 443 | |
| 444 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 445 | { |
| 446 | if (unformat (input, "enable")) |
| 447 | is_enable = 1; |
| 448 | else if (unformat (input, "disable")) |
| 449 | is_disable = 1; |
| 450 | else if (unformat (input, "skip-version-check")) |
| 451 | skip_version_check = 1; |
| 452 | else |
| 453 | { |
| 454 | error = clib_error_return (0, "unknown input '%U'", |
| 455 | format_unformat_error, input); |
| 456 | goto done; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | if (is_enable && is_disable) |
| 461 | { |
| 462 | error = clib_error_return (0, "please specify either enable or disable" |
| 463 | " for plugin '%s'", name); |
| 464 | goto done; |
| 465 | } |
| 466 | |
| 467 | vec_add2 (pm->configs, pc, 1); |
| 468 | hash_set_mem (pm->config_index_by_name, name, pc - pm->configs); |
| 469 | pc->is_enabled = is_enable; |
| 470 | pc->is_disabled = is_disable; |
| 471 | pc->skip_version_check = skip_version_check; |
| 472 | pc->name = name; |
| 473 | |
| 474 | done: |
| 475 | return error; |
| 476 | } |
| 477 | |
| 478 | clib_error_t * |
| 479 | vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input) |
| 480 | { |
| 481 | plugin_main_t *pm = &vlib_plugin_main; |
| 482 | clib_error_t *error = 0; |
| 483 | unformat_input_t in; |
| 484 | |
| 485 | unformat_init (&in, 0, 0); |
| 486 | |
| 487 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 488 | { |
| 489 | u8 *s, *v; |
| 490 | if (unformat (input, "%s %v", &s, &v)) |
| 491 | { |
| 492 | if (strncmp ((const char *) s, "plugins", 8) == 0) |
| 493 | { |
| 494 | if (vec_len (in.buffer) > 0) |
| 495 | vec_add1 (in.buffer, ' '); |
| 496 | vec_add (in.buffer, v, vec_len (v)); |
| 497 | } |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | error = clib_error_return (0, "unknown input '%U'", |
| 502 | format_unformat_error, input); |
| 503 | goto done; |
| 504 | } |
| 505 | |
| 506 | vec_free (v); |
| 507 | vec_free (s); |
| 508 | } |
| 509 | done: |
| 510 | input = ∈ |
| 511 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 512 | { |
| 513 | unformat_input_t sub_input; |
| 514 | u8 *s = 0; |
| 515 | if (unformat (input, "path %s", &s)) |
| 516 | pm->plugin_path = s; |
Dave Barach | 0a32b60 | 2017-11-28 18:55:09 -0500 | [diff] [blame] | 517 | else if (unformat (input, "name-filter %s", &s)) |
| 518 | pm->plugin_name_filter = s; |
| 519 | else if (unformat (input, "vat-path %s", &s)) |
| 520 | pm->vat_plugin_path = s; |
| 521 | else if (unformat (input, "vat-name-filter %s", &s)) |
| 522 | pm->vat_plugin_name_filter = s; |
Mohsin Kazmi | 06bc260 | 2018-03-22 23:45:23 +0100 | [diff] [blame] | 523 | else if (unformat (input, "plugin default %U", |
| 524 | unformat_vlib_cli_sub_input, &sub_input)) |
| 525 | { |
| 526 | pm->plugins_default_disable = |
| 527 | unformat (&sub_input, "disable") ? 1 : 0; |
| 528 | unformat_free (&sub_input); |
| 529 | } |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 530 | else if (unformat (input, "plugin %s %U", &s, |
| 531 | unformat_vlib_cli_sub_input, &sub_input)) |
| 532 | { |
| 533 | error = config_one_plugin (vm, (char *) s, &sub_input); |
| 534 | unformat_free (&sub_input); |
| 535 | if (error) |
| 536 | goto done2; |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | error = clib_error_return (0, "unknown input '%U'", |
| 541 | format_unformat_error, input); |
| 542 | { |
| 543 | vec_free (s); |
| 544 | goto done2; |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | done2: |
| 550 | unformat_free (&in); |
| 551 | return error; |
| 552 | } |
| 553 | |
| 554 | /* discard whole 'plugins' section, as it is already consumed prior to |
| 555 | plugin load */ |
| 556 | static clib_error_t * |
| 557 | plugins_config (vlib_main_t * vm, unformat_input_t * input) |
| 558 | { |
| 559 | u8 *junk; |
| 560 | |
| 561 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 562 | { |
| 563 | if (unformat (input, "%s", &junk)) |
| 564 | { |
| 565 | vec_free (junk); |
| 566 | return 0; |
| 567 | } |
| 568 | else |
| 569 | return clib_error_return (0, "unknown input '%U'", |
| 570 | format_unformat_error, input); |
| 571 | } |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | VLIB_CONFIG_FUNCTION (plugins_config, "plugins"); |
| 576 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 577 | /* |
| 578 | * fd.io coding-style-patch-verification: ON |
| 579 | * |
| 580 | * Local Variables: |
| 581 | * eval: (c-set-style "gnu") |
| 582 | * End: |
| 583 | */ |