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 #define PLUGIN_LOG_DBG(...) \
26 do {vlib_log_debug (vlib_plugin_main.logger, __VA_ARGS__);} while(0)
27 #define PLUGIN_LOG_ERR(...) \
28 do {vlib_log_err (vlib_plugin_main.logger, __VA_ARGS__);} while(0)
29 #define PLUGIN_LOG_NOTICE(...) \
30 do {vlib_log_notice (vlib_plugin_main.logger, __VA_ARGS__);} while(0)
32 char *vlib_plugin_path __attribute__ ((weak));
33 char *vlib_plugin_path = "";
34 char *vlib_plugin_app_version __attribute__ ((weak));
35 char *vlib_plugin_app_version = "";
38 vlib_get_plugin_symbol (char *plugin_name, char *symbol_name)
40 plugin_main_t *pm = &vlib_plugin_main;
44 if ((p = hash_get_mem (pm->plugin_by_name_hash, plugin_name)) == 0)
47 pi = vec_elt_at_index (pm->plugin_info, p[0]);
48 return dlsym (pi->handle, symbol_name);
52 str_array_to_vec (char *array, int len)
62 while (c && ++n < len);
71 extract (u8 * sp, u8 * ep)
85 load_one_plugin (plugin_main_t * pm, plugin_info_t * pi, int from_early_init)
89 elf_main_t em = { 0 };
90 elf_section_t *section;
92 char *version_required;
93 vlib_plugin_registration_t *reg;
94 plugin_config_t *pc = 0;
97 if (elf_read_file (&em, (char *) pi->filename))
100 error = elf_get_section_by_name (&em, ".vlib_plugin_registration",
104 PLUGIN_LOG_ERR ("Not a plugin: %s\n", (char *) pi->name);
108 data = elf_get_section_contents (&em, section->index, 1);
109 reg = (vlib_plugin_registration_t *) data;
111 if (vec_len (data) != sizeof (*reg))
113 PLUGIN_LOG_ERR ("vlib_plugin_registration size mismatch in plugin %s\n",
118 if (pm->plugins_default_disable)
119 reg->default_disabled = 1;
121 p = hash_get_mem (pm->config_index_by_name, pi->name);
124 pc = vec_elt_at_index (pm->configs, p[0]);
127 PLUGIN_LOG_NOTICE ("Plugin disabled: %s", pi->name);
130 if (reg->default_disabled && pc->is_enabled == 0)
132 PLUGIN_LOG_NOTICE ("Plugin disabled (default): %s", pi->name);
136 else if (reg->default_disabled)
138 PLUGIN_LOG_NOTICE ("Plugin disabled (default): %s", pi->name);
142 version_required = str_array_to_vec ((char *) ®->version_required,
143 sizeof (reg->version_required));
145 if ((strlen (version_required) > 0) &&
146 (strncmp (vlib_plugin_app_version, version_required,
147 strlen (version_required))))
149 PLUGIN_LOG_ERR ("Plugin %s version mismatch: %s != %s",
150 pi->name, vlib_plugin_app_version,
151 reg->version_required);
152 if (!(pc && pc->skip_version_check == 1))
154 vec_free (version_required);
160 * Collect names of plugins overridden (disabled) by the
163 if (reg->overrides[0])
165 const char *overrides = reg->overrides;
166 u8 *override_name_copy, *overridden_by_name_copy;
170 sp = ep = (u8 *) overrides;
175 || (sp >= (u8 *) overrides + ARRAY_LEN (reg->overrides)))
177 if (*sp == ' ' || *sp == ',')
183 while (*ep && *ep != ' ' && *ep != ',' &&
184 ep < (u8 *) overrides + ARRAY_LEN (reg->overrides))
186 if (*ep == ' ' || *ep == ',')
189 override_name_copy = extract (sp, ep);
192 p = hash_get_mem (pm->plugin_overrides_by_name_hash,
194 /* Already overridden... */
196 vec_free (override_name_copy);
199 overridden_by_name_copy = format (0, "%s%c", pi->name, 0);
200 hash_set_mem (pm->plugin_overrides_by_name_hash,
201 override_name_copy, overridden_by_name_copy);
203 sp = *ep ? ep + 1 : ep;
206 vec_free (version_required);
208 handle = dlopen ((char *) pi->filename, RTLD_LAZY);
212 PLUGIN_LOG_ERR ("%s", dlerror ());
213 PLUGIN_LOG_ERR ("Failed to load plugin '%s'", pi->name);
219 reg = dlsym (pi->handle, "vlib_plugin_registration");
223 /* This should never happen unless somebody changes registration macro */
224 PLUGIN_LOG_ERR ("Missing plugin registration in plugin '%s'", pi->name);
225 dlclose (pi->handle);
230 pi->version = str_array_to_vec ((char *) ®->version,
231 sizeof (reg->version));
235 clib_error_t *(*ei) (vlib_main_t *);
238 h = dlsym (pi->handle, reg->early_init);
242 error = (*ei) (pm->vlib_main);
245 u8 *err = format (0, "%s: %U%c", pi->name,
246 format_clib_error, error, 0);
247 PLUGIN_LOG_ERR ((char *) err);
248 clib_error_free (error);
249 dlclose (pi->handle);
255 PLUGIN_LOG_ERR ("Plugin %s: early init function %s set but not found",
256 (char *) pi->name, reg->early_init);
259 if (reg->description)
260 PLUGIN_LOG_NOTICE ("Loaded plugin: %s (%s)", pi->name, reg->description);
262 PLUGIN_LOG_NOTICE ("Loaded plugin: %s", pi->name);
275 split_plugin_path (plugin_main_t * pm)
279 u8 *path = pm->plugin_path;
282 for (i = 0; i < vec_len (pm->plugin_path); i++)
286 vec_add1 (this, path[i]);
302 plugin_name_sort_cmp (void *a1, void *a2)
304 plugin_info_t *p1 = a1;
305 plugin_info_t *p2 = a2;
307 return strcmp ((char *) p1->name, (char *) p2->name);
311 index_cmp (void *a1, void *a2)
313 uword *i1 = (uword *) a1, *i2 = (uword *) a2;
324 vlib_load_new_plugins (plugin_main_t * pm, int from_early_init)
327 struct dirent *entry;
332 uword *not_loaded_indices = 0;
335 plugin_path = split_plugin_path (pm);
337 for (i = 0; i < vec_len (plugin_path); i++)
339 dp = opendir ((char *) plugin_path[i]);
344 while ((entry = readdir (dp)))
349 if (pm->plugin_name_filter)
352 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
353 if (entry->d_name[j] != pm->plugin_name_filter[j])
357 filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
359 /* Only accept .so */
360 char *ext = strrchr ((const char *) filename, '.');
362 if (!ext || (strcmp (ext, ".so") != 0) ||
363 stat ((char *) filename, &statb) < 0)
370 /* a dir or other things which aren't plugins */
371 if (!S_ISREG (statb.st_mode))
374 plugin_name = format (0, "%s%c", entry->d_name, 0);
375 /* Have we seen this plugin already? */
376 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
379 /* No, add it to the plugin vector */
380 vec_add2 (pm->plugin_info, pi, 1);
381 pi->name = plugin_name;
382 pi->filename = filename;
383 pi->file_info = statb;
385 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
386 pi - pm->plugin_info);
392 vec_free (plugin_path[i]);
394 vec_free (plugin_path);
398 * Sort the plugins by name. This is important.
399 * API traces contain absolute message numbers.
400 * Loading plugins in directory (vs. alphabetical) order
401 * makes trace replay incredibly fragile.
403 vec_sort_with_function (pm->plugin_info, plugin_name_sort_cmp);
406 * Attempt to load the plugins
408 for (i = 0; i < vec_len (pm->plugin_info); i++)
410 pi = vec_elt_at_index (pm->plugin_info, i);
412 if (load_one_plugin (pm, pi, from_early_init))
414 /* Make a note of any which fail to load */
415 vec_add1 (not_loaded_indices, i);
420 * Honor override list
422 for (i = 0; i < vec_len (pm->plugin_info); i++)
426 pi = vec_elt_at_index (pm->plugin_info, i);
428 p = hash_get_mem (pm->plugin_overrides_by_name_hash, pi->name);
430 /* Plugin overridden? */
433 PLUGIN_LOG_NOTICE ("Plugin '%s' overridden by '%s'", pi->name,
435 vec_add1 (not_loaded_indices, i);
440 * Sort the vector of indices to delete to avoid screwing up
441 * the indices as we delete them.
443 vec_sort_with_function (not_loaded_indices, index_cmp);
446 * Remove duplicates, which can happen if a plugin is
447 * disabled from the command line and disabled by
448 * a plugin which is loaded.
450 for (i = 0; i < vec_len (not_loaded_indices); i++)
452 if (i < vec_len (not_loaded_indices) - 1)
454 if (not_loaded_indices[i + 1] == not_loaded_indices[i])
456 vec_delete (not_loaded_indices, 1, i);
462 /* Remove plugin info vector elements corresponding to load failures */
463 if (vec_len (not_loaded_indices) > 0)
465 for (i = vec_len (not_loaded_indices) - 1; i >= 0; i--)
467 pi = vec_elt_at_index (pm->plugin_info, not_loaded_indices[i]);
468 hash_unset_mem (pm->plugin_by_name_hash, pi->name);
471 dlclose (pi->handle);
472 PLUGIN_LOG_NOTICE ("Unloaded plugin: %s", pi->name);
475 vec_free (pi->filename);
476 vec_delete (pm->plugin_info, 1, not_loaded_indices[i]);
478 vec_free (not_loaded_indices);
481 /* Recreate the plugin name hash */
482 hash_free (pm->plugin_by_name_hash);
483 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
485 for (i = 0; i < vec_len (pm->plugin_info); i++)
487 pi = vec_elt_at_index (pm->plugin_info, i);
488 hash_set_mem (pm->plugin_by_name_hash, pi->name, pi - pm->plugin_info);
495 vlib_plugin_early_init (vlib_main_t * vm)
497 plugin_main_t *pm = &vlib_plugin_main;
500 vlib_log_register_class_rate_limit ("plugin", "load",
501 0x7FFFFFFF /* aka no rate limit */ );
503 if (pm->plugin_path == 0)
504 pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
506 PLUGIN_LOG_DBG ("plugin path %s", pm->plugin_path);
508 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
509 pm->plugin_overrides_by_name_hash = hash_create_string (0, sizeof (uword));
512 return vlib_load_new_plugins (pm, 1 /* from_early_init */ );
516 vlib_get_vat_plugin_path (void)
518 plugin_main_t *pm = &vlib_plugin_main;
519 return (pm->vat_plugin_path);
523 vlib_get_vat_plugin_name_filter (void)
525 plugin_main_t *pm = &vlib_plugin_main;
526 return (pm->vat_plugin_name_filter);
529 static clib_error_t *
530 vlib_plugins_show_cmd_fn (vlib_main_t * vm,
531 unformat_input_t * input, vlib_cli_command_t * cmd)
533 plugin_main_t *pm = &vlib_plugin_main;
540 s = format (s, " Plugin path is: %s\n\n", pm->plugin_path);
541 s = format (s, " %-41s%-33s%s\n", "Plugin", "Version", "Description");
544 hash_foreach_mem (key, value, pm->plugin_by_name_hash,
548 pi = vec_elt_at_index (pm->plugin_info, value);
549 s = format (s, "%3d. %-40s %-32s %s\n", index, key, pi->version,
550 (pi->reg && pi->reg->description) ?
551 pi->reg->description : "");
557 vlib_cli_output (vm, "%v", s);
563 VLIB_CLI_COMMAND (plugins_show_cmd, static) =
565 .path = "show plugins",
566 .short_help = "show loaded plugins",
567 .function = vlib_plugins_show_cmd_fn,
571 static clib_error_t *
572 config_one_plugin (vlib_main_t * vm, char *name, unformat_input_t * input)
574 plugin_main_t *pm = &vlib_plugin_main;
576 clib_error_t *error = 0;
580 int skip_version_check = 0;
582 if (pm->config_index_by_name == 0)
583 pm->config_index_by_name = hash_create_string (0, sizeof (uword));
585 p = hash_get_mem (pm->config_index_by_name, name);
589 error = clib_error_return (0, "plugin '%s' already configured", name);
593 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
595 if (unformat (input, "enable"))
597 else if (unformat (input, "disable"))
599 else if (unformat (input, "skip-version-check"))
600 skip_version_check = 1;
603 error = clib_error_return (0, "unknown input '%U'",
604 format_unformat_error, input);
609 if (is_enable && is_disable)
611 error = clib_error_return (0, "please specify either enable or disable"
612 " for plugin '%s'", name);
616 vec_add2 (pm->configs, pc, 1);
617 hash_set_mem (pm->config_index_by_name, name, pc - pm->configs);
618 pc->is_enabled = is_enable;
619 pc->is_disabled = is_disable;
620 pc->skip_version_check = skip_version_check;
628 vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input)
630 plugin_main_t *pm = &vlib_plugin_main;
631 clib_error_t *error = 0;
634 unformat_init (&in, 0, 0);
636 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
639 if (unformat (input, "%s %v", &s, &v))
641 if (strncmp ((const char *) s, "plugins", 8) == 0)
643 if (vec_len (in.buffer) > 0)
644 vec_add1 (in.buffer, ' ');
645 vec_add (in.buffer, v, vec_len (v));
650 error = clib_error_return (0, "unknown input '%U'",
651 format_unformat_error, input);
660 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
662 unformat_input_t sub_input;
664 if (unformat (input, "path %s", &s))
666 else if (unformat (input, "name-filter %s", &s))
667 pm->plugin_name_filter = s;
668 else if (unformat (input, "vat-path %s", &s))
669 pm->vat_plugin_path = s;
670 else if (unformat (input, "vat-name-filter %s", &s))
671 pm->vat_plugin_name_filter = s;
672 else if (unformat (input, "plugin default %U",
673 unformat_vlib_cli_sub_input, &sub_input))
675 pm->plugins_default_disable =
676 unformat (&sub_input, "disable") ? 1 : 0;
677 unformat_free (&sub_input);
679 else if (unformat (input, "plugin %s %U", &s,
680 unformat_vlib_cli_sub_input, &sub_input))
682 error = config_one_plugin (vm, (char *) s, &sub_input);
683 unformat_free (&sub_input);
689 error = clib_error_return (0, "unknown input '%U'",
690 format_unformat_error, input);
703 /* discard whole 'plugins' section, as it is already consumed prior to
705 static clib_error_t *
706 plugins_config (vlib_main_t * vm, unformat_input_t * input)
710 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
712 if (unformat (input, "%s", &junk))
718 return clib_error_return (0, "unknown input '%U'",
719 format_unformat_error, input);
724 VLIB_CONFIG_FUNCTION (plugins_config, "plugins");
727 * fd.io coding-style-patch-verification: ON
730 * eval: (c-set-style "gnu")