2 * plugin.c: plugin handling
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/unix/plugin.h>
19 #include <vppinfra/elf.h>
23 plugin_main_t vlib_plugin_main;
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 = "";
31 vlib_get_plugin_symbol (char *plugin_name, char *symbol_name)
33 plugin_main_t *pm = &vlib_plugin_main;
37 if ((p = hash_get_mem (pm->plugin_by_name_hash, plugin_name)) == 0)
40 pi = vec_elt_at_index (pm->plugin_info, p[0]);
41 return dlsym (pi->handle, symbol_name);
45 str_array_to_vec (char *array, int len)
55 while (c && ++n < len);
64 load_one_plugin (plugin_main_t * pm, plugin_info_t * pi, int from_early_init)
68 elf_main_t em = { 0 };
69 elf_section_t *section;
71 char *version_required;
72 vlib_plugin_registration_t *reg;
73 plugin_config_t *pc = 0;
76 if (elf_read_file (&em, (char *) pi->filename))
79 error = elf_get_section_by_name (&em, ".vlib_plugin_registration",
83 clib_warning ("Not a plugin: %s\n", (char *) pi->name);
87 data = elf_get_section_contents (&em, section->index, 1);
88 reg = (vlib_plugin_registration_t *) data;
90 if (vec_len (data) != sizeof (*reg))
92 clib_warning ("vlib_plugin_registration size mismatch in plugin %s\n",
97 if (pm->plugins_default_disable)
98 reg->default_disabled = 1;
100 p = hash_get_mem (pm->config_index_by_name, pi->name);
103 pc = vec_elt_at_index (pm->configs, p[0]);
106 clib_warning ("Plugin disabled: %s", pi->name);
109 if (reg->default_disabled && pc->is_enabled == 0)
111 clib_warning ("Plugin disabled (default): %s", pi->name);
115 else if (reg->default_disabled)
117 clib_warning ("Plugin disabled (default): %s", pi->name);
121 version_required = str_array_to_vec ((char *) ®->version_required,
122 sizeof (reg->version_required));
124 if ((strlen (version_required) > 0) &&
125 (strncmp (vlib_plugin_app_version, version_required,
126 strlen (version_required))))
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))
132 vec_free (version_required);
137 vec_free (version_required);
141 handle = dlopen ((char *) pi->filename, RTLD_LAZY);
145 clib_warning ("%s", dlerror ());
146 clib_warning ("Failed to load plugin '%s'", pi->name);
152 reg = dlsym (pi->handle, "vlib_plugin_registration");
156 /* This should never happen unless somebody chagnes registration macro */
157 clib_warning ("Missing plugin registration in plugin '%s'", pi->name);
158 dlclose (pi->handle);
163 pi->version = str_array_to_vec ((char *) ®->version,
164 sizeof (reg->version));
168 clib_error_t *(*ei) (vlib_main_t *);
171 h = dlsym (pi->handle, reg->early_init);
175 error = (*ei) (pm->vlib_main);
178 clib_error_report (error);
179 dlclose (pi->handle);
184 clib_warning ("Plugin %s: early init function %s set but not found",
185 (char *) pi->name, reg->early_init);
188 if (reg->description)
189 clib_warning ("Loaded plugin: %s (%s)", pi->name, reg->description);
191 clib_warning ("Loaded plugin: %s", pi->name);
201 split_plugin_path (plugin_main_t * pm)
205 u8 *path = pm->plugin_path;
208 for (i = 0; i < vec_len (pm->plugin_path); i++)
212 vec_add1 (this, path[i]);
228 plugin_name_sort_cmp (void *a1, void *a2)
230 plugin_info_t *p1 = a1;
231 plugin_info_t *p2 = a2;
233 return strcmp ((char *) p1->name, (char *) p2->name);
237 vlib_load_new_plugins (plugin_main_t * pm, int from_early_init)
240 struct dirent *entry;
245 u32 *load_fail_indices = 0;
248 plugin_path = split_plugin_path (pm);
250 for (i = 0; i < vec_len (plugin_path); i++)
252 dp = opendir ((char *) plugin_path[i]);
257 while ((entry = readdir (dp)))
262 if (pm->plugin_name_filter)
265 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
266 if (entry->d_name[j] != pm->plugin_name_filter[j])
270 filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
272 /* Only accept .so */
273 char *ext = strrchr ((const char *) filename, '.');
275 if (!ext || (strcmp (ext, ".so") != 0) ||
276 stat ((char *) filename, &statb) < 0)
283 /* a dir or other things which aren't plugins */
284 if (!S_ISREG (statb.st_mode))
287 plugin_name = format (0, "%s%c", entry->d_name, 0);
288 /* Have we seen this plugin already? */
289 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
292 /* No, add it to the plugin vector */
293 vec_add2 (pm->plugin_info, pi, 1);
294 pi->name = plugin_name;
295 pi->filename = filename;
296 pi->file_info = statb;
297 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
298 pi - pm->plugin_info);
304 vec_free (plugin_path[i]);
306 vec_free (plugin_path);
310 * Sort the plugins by name. This is important.
311 * API traces contain absolute message numbers.
312 * Loading plugins in directory (vs. alphabetical) order
313 * makes trace replay incredibly fragile.
315 vec_sort_with_function (pm->plugin_info, plugin_name_sort_cmp);
318 * Attempt to load the plugins
320 for (i = 0; i < vec_len (pm->plugin_info); i++)
322 pi = vec_elt_at_index (pm->plugin_info, i);
324 if (load_one_plugin (pm, pi, from_early_init))
326 /* Make a note of any which fail to load */
327 vec_add1 (load_fail_indices, i);
328 hash_unset_mem (pm->plugin_by_name_hash, pi->name);
330 vec_free (pi->filename);
334 /* Remove plugin info vector elements corresponding to load failures */
335 if (vec_len (load_fail_indices) > 0)
337 for (i = vec_len (load_fail_indices) - 1; i >= 0; i--)
338 vec_delete (pm->plugin_info, 1, load_fail_indices[i]);
339 vec_free (load_fail_indices);
342 /* Recreate the plugin name hash */
343 for (i = 0; i < vec_len (pm->plugin_info); i++)
345 pi = vec_elt_at_index (pm->plugin_info, i);
346 hash_unset_mem (pm->plugin_by_name_hash, pi->name);
347 hash_set_mem (pm->plugin_by_name_hash, pi->name, pi - pm->plugin_info);
354 vlib_plugin_early_init (vlib_main_t * vm)
356 plugin_main_t *pm = &vlib_plugin_main;
358 if (pm->plugin_path == 0)
359 pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
361 clib_warning ("plugin path %s", pm->plugin_path);
363 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
366 return vlib_load_new_plugins (pm, 1 /* from_early_init */ );
370 vlib_get_vat_plugin_path (void)
372 plugin_main_t *pm = &vlib_plugin_main;
373 return (pm->vat_plugin_path);
377 vlib_get_vat_plugin_name_filter (void)
379 plugin_main_t *pm = &vlib_plugin_main;
380 return (pm->vat_plugin_name_filter);
383 static clib_error_t *
384 vlib_plugins_show_cmd_fn (vlib_main_t * vm,
385 unformat_input_t * input, vlib_cli_command_t * cmd)
387 plugin_main_t *pm = &vlib_plugin_main;
394 s = format (s, " Plugin path is: %s\n\n", pm->plugin_path);
395 s = format (s, " %-41s%-33s%s\n", "Plugin", "Version", "Description");
398 hash_foreach_mem (key, value, pm->plugin_by_name_hash,
402 pi = vec_elt_at_index (pm->plugin_info, value);
403 s = format (s, "%3d. %-40s %-32s %s\n", index, key, pi->version,
404 pi->reg->description ? pi->reg->description : "");
410 vlib_cli_output (vm, "%v", s);
416 VLIB_CLI_COMMAND (plugins_show_cmd, static) =
418 .path = "show plugins",
419 .short_help = "show loaded plugins",
420 .function = vlib_plugins_show_cmd_fn,
424 static clib_error_t *
425 config_one_plugin (vlib_main_t * vm, char *name, unformat_input_t * input)
427 plugin_main_t *pm = &vlib_plugin_main;
429 clib_error_t *error = 0;
433 int skip_version_check = 0;
435 if (pm->config_index_by_name == 0)
436 pm->config_index_by_name = hash_create_string (0, sizeof (uword));
438 p = hash_get_mem (pm->config_index_by_name, name);
442 error = clib_error_return (0, "plugin '%s' already configured", name);
446 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
448 if (unformat (input, "enable"))
450 else if (unformat (input, "disable"))
452 else if (unformat (input, "skip-version-check"))
453 skip_version_check = 1;
456 error = clib_error_return (0, "unknown input '%U'",
457 format_unformat_error, input);
462 if (is_enable && is_disable)
464 error = clib_error_return (0, "please specify either enable or disable"
465 " for plugin '%s'", name);
469 vec_add2 (pm->configs, pc, 1);
470 hash_set_mem (pm->config_index_by_name, name, pc - pm->configs);
471 pc->is_enabled = is_enable;
472 pc->is_disabled = is_disable;
473 pc->skip_version_check = skip_version_check;
481 vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input)
483 plugin_main_t *pm = &vlib_plugin_main;
484 clib_error_t *error = 0;
487 unformat_init (&in, 0, 0);
489 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
492 if (unformat (input, "%s %v", &s, &v))
494 if (strncmp ((const char *) s, "plugins", 8) == 0)
496 if (vec_len (in.buffer) > 0)
497 vec_add1 (in.buffer, ' ');
498 vec_add (in.buffer, v, vec_len (v));
503 error = clib_error_return (0, "unknown input '%U'",
504 format_unformat_error, input);
513 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
515 unformat_input_t sub_input;
517 if (unformat (input, "path %s", &s))
519 else if (unformat (input, "name-filter %s", &s))
520 pm->plugin_name_filter = s;
521 else if (unformat (input, "vat-path %s", &s))
522 pm->vat_plugin_path = s;
523 else if (unformat (input, "vat-name-filter %s", &s))
524 pm->vat_plugin_name_filter = s;
525 else if (unformat (input, "plugin default %U",
526 unformat_vlib_cli_sub_input, &sub_input))
528 pm->plugins_default_disable =
529 unformat (&sub_input, "disable") ? 1 : 0;
530 unformat_free (&sub_input);
532 else if (unformat (input, "plugin %s %U", &s,
533 unformat_vlib_cli_sub_input, &sub_input))
535 error = config_one_plugin (vm, (char *) s, &sub_input);
536 unformat_free (&sub_input);
542 error = clib_error_return (0, "unknown input '%U'",
543 format_unformat_error, input);
556 /* discard whole 'plugins' section, as it is already consumed prior to
558 static clib_error_t *
559 plugins_config (vlib_main_t * vm, unformat_input_t * input)
563 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
565 if (unformat (input, "%s", &junk))
571 return clib_error_return (0, "unknown input '%U'",
572 format_unformat_error, input);
577 VLIB_CONFIG_FUNCTION (plugins_config, "plugins");
580 * fd.io coding-style-patch-verification: ON
583 * eval: (c-set-style "gnu")