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 (const char *plugin_name, const 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 * If a plugin .so contains a ".vlib_plugin_r2" section,
86 * this function converts the vlib_plugin_r2_t to
87 * a vlib_plugin_registration_t.
91 r2_to_reg (elf_main_t * em, vlib_plugin_r2_t * r2,
92 vlib_plugin_registration_t * reg)
95 elf_section_t *section;
96 uword data_segment_offset;
99 /* It turns out that the strings land in the ".data" section */
100 error = elf_get_section_by_name (em, ".data", §ion);
103 data = elf_get_section_contents (em, section->index, 1);
106 * Offsets in the ".vlib_plugin_r2" section
107 * need to have the data section base subtracted from them.
108 * The offset is in the first 8 bytes of the ".data" section
111 data_segment_offset = *((uword *) data);
113 /* Relocate pointers, subtract data_segment_offset */
114 #define _(a) r2->a.data_segment_offset -= data_segment_offset;
115 foreach_r2_string_field;
118 if (r2->version.length >= ARRAY_LEN (reg->version) - 1)
119 return clib_error_return (0, "Version string too long");
121 if (r2->version_required.length >= ARRAY_LEN (reg->version_required) - 1)
122 return clib_error_return (0, "Version-required string too long");
124 if (r2->overrides.length >= ARRAY_LEN (reg->overrides) - 1)
125 return clib_error_return (0, "Override string too long");
127 /* Compatibility with C-initializer */
128 memcpy ((void *) reg->version, data + r2->version.data_segment_offset,
130 memcpy ((void *) reg->version_required,
131 data + r2->version_required.data_segment_offset,
132 r2->version_required.length);
133 memcpy ((void *) reg->overrides, data + r2->overrides.data_segment_offset,
134 r2->overrides.length);
136 if (r2->early_init.length > 0)
139 vec_validate (ei, r2->early_init.length + 1);
140 memcpy (ei, data + r2->early_init.data_segment_offset,
141 r2->early_init.length);
142 reg->early_init = (void *) ei;
145 if (r2->description.length > 0)
148 vec_validate (desc, r2->description.length + 1);
149 memcpy (desc, data + r2->description.data_segment_offset,
150 r2->description.length);
151 reg->description = (void *) desc;
159 load_one_plugin (plugin_main_t * pm, plugin_info_t * pi, int from_early_init)
164 elf_main_t em = { 0 };
165 elf_section_t *section;
167 char *version_required;
168 vlib_plugin_registration_t *reg;
169 vlib_plugin_r2_t *r2;
170 plugin_config_t *pc = 0;
173 if (elf_read_file (&em, (char *) pi->filename))
176 /* New / improved (well, not really) registration structure? */
177 error = elf_get_section_by_name (&em, ".vlib_plugin_r2", §ion);
180 data = elf_get_section_contents (&em, section->index, 1);
181 r2 = (vlib_plugin_r2_t *) data;
182 reg = clib_mem_alloc (sizeof (*reg));
183 memset (reg, 0, sizeof (*reg));
185 reg->default_disabled = r2->default_disabled != 0;
186 error = r2_to_reg (&em, r2, reg);
189 PLUGIN_LOG_ERR ("Bad r2 registration: %s\n", (char *) pi->name);
192 if (pm->plugins_default_disable)
193 reg->default_disabled = 1;
198 clib_error_free (error);
200 error = elf_get_section_by_name (&em, ".vlib_plugin_registration",
204 PLUGIN_LOG_ERR ("Not a plugin: %s\n", (char *) pi->name);
208 data = elf_get_section_contents (&em, section->index, 1);
209 reg = (vlib_plugin_registration_t *) data;
211 if (vec_len (data) != sizeof (*reg))
213 PLUGIN_LOG_ERR ("vlib_plugin_registration size mismatch in plugin %s\n",
218 if (pm->plugins_default_disable)
219 reg->default_disabled = 1;
222 p = hash_get_mem (pm->config_index_by_name, pi->name);
225 pc = vec_elt_at_index (pm->configs, p[0]);
228 PLUGIN_LOG_NOTICE ("Plugin disabled: %s", pi->name);
231 if (reg->default_disabled && pc->is_enabled == 0)
233 PLUGIN_LOG_NOTICE ("Plugin disabled (default): %s", pi->name);
237 else if (reg->default_disabled)
239 PLUGIN_LOG_NOTICE ("Plugin disabled (default): %s", pi->name);
243 version_required = str_array_to_vec ((char *) ®->version_required,
244 sizeof (reg->version_required));
246 if ((strlen (version_required) > 0) &&
247 (strncmp (vlib_plugin_app_version, version_required,
248 strlen (version_required))))
250 PLUGIN_LOG_ERR ("Plugin %s version mismatch: %s != %s",
251 pi->name, vlib_plugin_app_version,
252 reg->version_required);
253 if (!(pc && pc->skip_version_check == 1))
255 vec_free (version_required);
261 * Collect names of plugins overridden (disabled) by the
264 if (reg->overrides[0])
266 const char *overrides = reg->overrides;
267 u8 *override_name_copy, *overridden_by_name_copy;
271 sp = ep = (u8 *) overrides;
276 || (sp >= (u8 *) overrides + ARRAY_LEN (reg->overrides)))
278 if (*sp == ' ' || *sp == ',')
284 while (*ep && *ep != ' ' && *ep != ',' &&
285 ep < (u8 *) overrides + ARRAY_LEN (reg->overrides))
287 if (*ep == ' ' || *ep == ',')
290 override_name_copy = extract (sp, ep);
293 p = hash_get_mem (pm->plugin_overrides_by_name_hash,
295 /* Already overridden... */
297 vec_free (override_name_copy);
300 overridden_by_name_copy = format (0, "%s%c", pi->name, 0);
301 hash_set_mem (pm->plugin_overrides_by_name_hash,
302 override_name_copy, overridden_by_name_copy);
304 sp = *ep ? ep + 1 : ep;
307 vec_free (version_required);
309 #if defined(RTLD_DEEPBIND)
310 handle = dlopen ((char *) pi->filename,
311 RTLD_LAZY | (reg->deep_bind ? RTLD_DEEPBIND : 0));
313 handle = dlopen ((char *) pi->filename, RTLD_LAZY);
318 PLUGIN_LOG_ERR ("%s", dlerror ());
319 PLUGIN_LOG_ERR ("Failed to load plugin '%s'", pi->name);
326 reg = dlsym (pi->handle, "vlib_plugin_registration");
329 pi->version = str_array_to_vec ((char *) ®->version,
330 sizeof (reg->version));
334 clib_error_t *(*ei) (vlib_main_t *);
337 h = dlsym (pi->handle, reg->early_init);
341 error = (*ei) (pm->vlib_main);
344 u8 *err = format (0, "%s: %U%c", pi->name,
345 format_clib_error, error, 0);
346 PLUGIN_LOG_ERR ((char *) err);
347 clib_error_free (error);
348 dlclose (pi->handle);
354 PLUGIN_LOG_ERR ("Plugin %s: early init function %s set but not found",
355 (char *) pi->name, reg->early_init);
358 if (reg->description)
359 PLUGIN_LOG_NOTICE ("Loaded plugin: %s (%s)", pi->name, reg->description);
361 PLUGIN_LOG_NOTICE ("Loaded plugin: %s", pi->name);
374 split_plugin_path (plugin_main_t * pm)
378 u8 *path = pm->plugin_path;
381 for (i = 0; i < vec_len (pm->plugin_path); i++)
385 vec_add1 (this, path[i]);
401 plugin_name_sort_cmp (void *a1, void *a2)
403 plugin_info_t *p1 = a1;
404 plugin_info_t *p2 = a2;
406 return strcmp ((char *) p1->name, (char *) p2->name);
410 index_cmp (void *a1, void *a2)
412 uword *i1 = (uword *) a1, *i2 = (uword *) a2;
423 vlib_load_new_plugins (plugin_main_t * pm, int from_early_init)
426 struct dirent *entry;
431 uword *not_loaded_indices = 0;
434 plugin_path = split_plugin_path (pm);
436 for (i = 0; i < vec_len (plugin_path); i++)
438 dp = opendir ((char *) plugin_path[i]);
443 while ((entry = readdir (dp)))
448 if (pm->plugin_name_filter)
451 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
452 if (entry->d_name[j] != pm->plugin_name_filter[j])
456 filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
458 /* Only accept .so */
459 char *ext = strrchr ((const char *) filename, '.');
461 if (!ext || (strcmp (ext, ".so") != 0) ||
462 stat ((char *) filename, &statb) < 0)
469 /* a dir or other things which aren't plugins */
470 if (!S_ISREG (statb.st_mode))
473 plugin_name = format (0, "%s%c", entry->d_name, 0);
474 /* Have we seen this plugin already? */
475 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
478 /* No, add it to the plugin vector */
479 vec_add2 (pm->plugin_info, pi, 1);
480 pi->name = plugin_name;
481 pi->filename = filename;
482 pi->file_info = statb;
484 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
485 pi - pm->plugin_info);
491 vec_free (plugin_path[i]);
493 vec_free (plugin_path);
497 * Sort the plugins by name. This is important.
498 * API traces contain absolute message numbers.
499 * Loading plugins in directory (vs. alphabetical) order
500 * makes trace replay incredibly fragile.
502 vec_sort_with_function (pm->plugin_info, plugin_name_sort_cmp);
505 * Attempt to load the plugins
507 for (i = 0; i < vec_len (pm->plugin_info); i++)
509 pi = vec_elt_at_index (pm->plugin_info, i);
511 if (load_one_plugin (pm, pi, from_early_init))
513 /* Make a note of any which fail to load */
514 vec_add1 (not_loaded_indices, i);
519 * Honor override list
521 for (i = 0; i < vec_len (pm->plugin_info); i++)
525 pi = vec_elt_at_index (pm->plugin_info, i);
527 p = hash_get_mem (pm->plugin_overrides_by_name_hash, pi->name);
529 /* Plugin overridden? */
532 PLUGIN_LOG_NOTICE ("Plugin '%s' overridden by '%s'", pi->name,
534 vec_add1 (not_loaded_indices, i);
539 * Sort the vector of indices to delete to avoid screwing up
540 * the indices as we delete them.
542 vec_sort_with_function (not_loaded_indices, index_cmp);
545 * Remove duplicates, which can happen if a plugin is
546 * disabled from the command line and disabled by
547 * a plugin which is loaded.
549 for (i = 0; i < vec_len (not_loaded_indices); i++)
551 if (i < vec_len (not_loaded_indices) - 1)
553 if (not_loaded_indices[i + 1] == not_loaded_indices[i])
555 vec_delete (not_loaded_indices, 1, i);
561 /* Remove plugin info vector elements corresponding to load failures */
562 if (vec_len (not_loaded_indices) > 0)
564 for (i = vec_len (not_loaded_indices) - 1; i >= 0; i--)
566 pi = vec_elt_at_index (pm->plugin_info, not_loaded_indices[i]);
567 hash_unset_mem (pm->plugin_by_name_hash, pi->name);
570 dlclose (pi->handle);
571 PLUGIN_LOG_NOTICE ("Unloaded plugin: %s", pi->name);
574 vec_free (pi->filename);
575 vec_delete (pm->plugin_info, 1, not_loaded_indices[i]);
577 vec_free (not_loaded_indices);
580 /* Recreate the plugin name hash */
581 hash_free (pm->plugin_by_name_hash);
582 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
584 for (i = 0; i < vec_len (pm->plugin_info); i++)
586 pi = vec_elt_at_index (pm->plugin_info, i);
587 hash_set_mem (pm->plugin_by_name_hash, pi->name, pi - pm->plugin_info);
594 vlib_plugin_early_init (vlib_main_t * vm)
596 plugin_main_t *pm = &vlib_plugin_main;
599 vlib_log_register_class_rate_limit ("plugin", "load",
600 0x7FFFFFFF /* aka no rate limit */ );
602 if (pm->plugin_path == 0)
603 pm->plugin_path = format (0, "%s", vlib_plugin_path);
605 if (pm->plugin_path_add)
606 pm->plugin_path = format (pm->plugin_path, ":%s", pm->plugin_path_add);
608 pm->plugin_path = format (pm->plugin_path, "%c", 0);
610 PLUGIN_LOG_DBG ("plugin path %s", pm->plugin_path);
612 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
613 pm->plugin_overrides_by_name_hash = hash_create_string (0, sizeof (uword));
616 return vlib_load_new_plugins (pm, 1 /* from_early_init */ );
620 vlib_get_vat_plugin_path (void)
622 plugin_main_t *pm = &vlib_plugin_main;
623 return (pm->vat_plugin_path);
627 vlib_get_vat_plugin_name_filter (void)
629 plugin_main_t *pm = &vlib_plugin_main;
630 return (pm->vat_plugin_name_filter);
633 static clib_error_t *
634 vlib_plugins_show_cmd_fn (vlib_main_t * vm,
635 unformat_input_t * input, vlib_cli_command_t * cmd)
637 plugin_main_t *pm = &vlib_plugin_main;
644 s = format (s, " Plugin path is: %s\n\n", pm->plugin_path);
645 s = format (s, " %-41s%-33s%s\n", "Plugin", "Version", "Description");
647 hash_foreach_mem (key, value, pm->plugin_by_name_hash,
651 pi = vec_elt_at_index (pm->plugin_info, value);
652 s = format (s, "%3d. %-40s %-32s %s\n", index, key, pi->version,
653 (pi->reg && pi->reg->description) ?
654 pi->reg->description : "");
659 vlib_cli_output (vm, "%v", s);
664 VLIB_CLI_COMMAND (plugins_show_cmd, static) =
666 .path = "show plugins",
667 .short_help = "show loaded plugins",
668 .function = vlib_plugins_show_cmd_fn,
671 static clib_error_t *
672 config_one_plugin (vlib_main_t * vm, char *name, unformat_input_t * input)
674 plugin_main_t *pm = &vlib_plugin_main;
676 clib_error_t *error = 0;
680 int skip_version_check = 0;
682 if (pm->config_index_by_name == 0)
683 pm->config_index_by_name = hash_create_string (0, sizeof (uword));
685 p = hash_get_mem (pm->config_index_by_name, name);
689 error = clib_error_return (0, "plugin '%s' already configured", name);
693 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
695 if (unformat (input, "enable"))
697 else if (unformat (input, "disable"))
699 else if (unformat (input, "skip-version-check"))
700 skip_version_check = 1;
703 error = clib_error_return (0, "unknown input '%U'",
704 format_unformat_error, input);
709 if (is_enable && is_disable)
711 error = clib_error_return (0, "please specify either enable or disable"
712 " for plugin '%s'", name);
716 vec_add2 (pm->configs, pc, 1);
717 hash_set_mem (pm->config_index_by_name, name, pc - pm->configs);
718 pc->is_enabled = is_enable;
719 pc->is_disabled = is_disable;
720 pc->skip_version_check = skip_version_check;
728 vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input)
730 plugin_main_t *pm = &vlib_plugin_main;
731 clib_error_t *error = 0;
734 unformat_init (&in, 0, 0);
736 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
739 if (unformat (input, "%s %v", &s, &v))
741 if (strncmp ((const char *) s, "plugins", 8) == 0)
743 if (vec_len (in.buffer) > 0)
744 vec_add1 (in.buffer, ' ');
745 vec_add (in.buffer, v, vec_len (v));
750 error = clib_error_return (0, "unknown input '%U'",
751 format_unformat_error, input);
760 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
762 unformat_input_t sub_input;
764 if (unformat (input, "path %s", &s))
766 else if (unformat (input, "add-path %s", &s))
767 pm->plugin_path_add = s;
768 else if (unformat (input, "name-filter %s", &s))
769 pm->plugin_name_filter = s;
770 else if (unformat (input, "vat-path %s", &s))
771 pm->vat_plugin_path = s;
772 else if (unformat (input, "vat-name-filter %s", &s))
773 pm->vat_plugin_name_filter = s;
774 else if (unformat (input, "plugin default %U",
775 unformat_vlib_cli_sub_input, &sub_input))
777 pm->plugins_default_disable =
778 unformat (&sub_input, "disable") ? 1 : 0;
779 unformat_free (&sub_input);
781 else if (unformat (input, "plugin %s %U", &s,
782 unformat_vlib_cli_sub_input, &sub_input))
784 error = config_one_plugin (vm, (char *) s, &sub_input);
785 unformat_free (&sub_input);
791 error = clib_error_return (0, "unknown input '%U'",
792 format_unformat_error, input);
805 /* discard whole 'plugins' section, as it is already consumed prior to
807 static clib_error_t *
808 plugins_config (vlib_main_t * vm, unformat_input_t * input)
812 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
814 if (unformat (input, "%s", &junk))
820 return clib_error_return (0, "unknown input '%U'",
821 format_unformat_error, input);
826 VLIB_CONFIG_FUNCTION (plugins_config, "plugins");
829 * fd.io coding-style-patch-verification: ON
832 * eval: (c-set-style "gnu")