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 p = hash_get_mem (pm->config_index_by_name, pi->name);
100 pc = vec_elt_at_index (pm->configs, p[0]);
103 clib_warning ("Plugin disabled: %s", pi->name);
106 if (reg->default_disabled && pc->is_enabled == 0)
108 clib_warning ("Plugin disabled: %s (default)", pi->name);
112 else if (reg->default_disabled)
114 clib_warning ("Plugin disabled: %s (default)", pi->name);
118 version_required = str_array_to_vec ((char *) ®->version_required,
119 sizeof (reg->version_required));
121 if ((strlen (version_required) > 0) &&
122 (strncmp (vlib_plugin_app_version, version_required,
123 strlen (version_required))))
125 clib_warning ("Plugin %s version mismatch: %s != %s",
126 pi->name, vlib_plugin_app_version, reg->version_required);
127 if (!(pc && pc->skip_version_check == 1))
129 vec_free (version_required);
134 vec_free (version_required);
138 handle = dlopen ((char *) pi->filename, RTLD_LAZY);
141 * Note: this can happen if the plugin has an undefined symbol reference,
142 * so print a warning. Otherwise, the poor slob won't know what happened.
143 * Ask me how I know that...
147 clib_warning ("%s", dlerror ());
153 reg = dlsym (pi->handle, "vlib_plugin_registration");
157 /* This should never happen unless somebody chagnes registration macro */
158 clib_warning ("Missing plugin registration in plugin '%s'", pi->name);
163 pi->version = str_array_to_vec ((char *) ®->version,
164 sizeof (reg->version));
166 if (reg && reg->early_init)
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);
183 clib_warning ("Plugin %s: early init function %s set but not found",
184 (char *) pi->name, reg->early_init);
187 clib_warning ("Loaded plugin: %s", pi->name);
197 split_plugin_path (plugin_main_t * pm)
201 u8 *path = pm->plugin_path;
204 for (i = 0; i < vec_len (pm->plugin_path); i++)
208 vec_add1 (this, path[i]);
224 vlib_load_new_plugins (plugin_main_t * pm, int from_early_init)
227 struct dirent *entry;
234 plugin_path = split_plugin_path (pm);
236 for (i = 0; i < vec_len (plugin_path); i++)
238 dp = opendir ((char *) plugin_path[i]);
243 while ((entry = readdir (dp)))
248 if (pm->plugin_name_filter)
251 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
252 if (entry->d_name[j] != pm->plugin_name_filter[j])
256 filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
258 /* Only accept .so */
259 char *ext = strrchr ((const char *) filename, '.');
261 if (!ext || (strcmp (ext, ".so") != 0) ||
262 stat ((char *) filename, &statb) < 0)
269 /* a dir or other things which aren't plugins */
270 if (!S_ISREG (statb.st_mode))
273 plugin_name = format (0, "%s%c", entry->d_name, 0);
274 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
277 vec_add2 (pm->plugin_info, pi, 1);
278 pi->name = plugin_name;
279 pi->filename = filename;
280 pi->file_info = statb;
282 if (load_one_plugin (pm, pi, from_early_init))
284 vec_free (plugin_name);
286 _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1;
287 memset (pi, 0, sizeof (*pi));
290 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
291 pi - pm->plugin_info);
297 vec_free (plugin_path[i]);
299 vec_free (plugin_path);
304 vlib_plugin_early_init (vlib_main_t * vm)
306 plugin_main_t *pm = &vlib_plugin_main;
308 if (pm->plugin_path == 0)
309 pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
311 clib_warning ("plugin path %s", pm->plugin_path);
313 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
316 return vlib_load_new_plugins (pm, 1 /* from_early_init */ );
319 static clib_error_t *
320 vlib_plugins_show_cmd_fn (vlib_main_t * vm,
321 unformat_input_t * input, vlib_cli_command_t * cmd)
323 plugin_main_t *pm = &vlib_plugin_main;
330 s = format (s, " Plugin path is: %s\n\n", pm->plugin_path);
331 s = format (s, " %-41s%s\n", "Plugin", "Version");
334 hash_foreach_mem (key, value, pm->plugin_by_name_hash,
338 pi = vec_elt_at_index (pm->plugin_info, value);
339 s = format (s, "%3d. %-40s %s\n", index, key, pi->version);
345 vlib_cli_output (vm, "%v", s);
351 VLIB_CLI_COMMAND (plugins_show_cmd, static) =
353 .path = "show plugins",
354 .short_help = "show loaded plugins",
355 .function = vlib_plugins_show_cmd_fn,
359 static clib_error_t *
360 config_one_plugin (vlib_main_t * vm, char *name, unformat_input_t * input)
362 plugin_main_t *pm = &vlib_plugin_main;
364 clib_error_t *error = 0;
368 int skip_version_check = 0;
370 if (pm->config_index_by_name == 0)
371 pm->config_index_by_name = hash_create_string (0, sizeof (uword));
373 p = hash_get_mem (pm->config_index_by_name, name);
377 error = clib_error_return (0, "plugin '%s' already configured", name);
381 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
383 if (unformat (input, "enable"))
385 else if (unformat (input, "disable"))
387 else if (unformat (input, "skip-version-check"))
388 skip_version_check = 1;
391 error = clib_error_return (0, "unknown input '%U'",
392 format_unformat_error, input);
397 if (is_enable && is_disable)
399 error = clib_error_return (0, "please specify either enable or disable"
400 " for plugin '%s'", name);
404 vec_add2 (pm->configs, pc, 1);
405 hash_set_mem (pm->config_index_by_name, name, pc - pm->configs);
406 pc->is_enabled = is_enable;
407 pc->is_disabled = is_disable;
408 pc->skip_version_check = skip_version_check;
416 vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input)
418 plugin_main_t *pm = &vlib_plugin_main;
419 clib_error_t *error = 0;
422 unformat_init (&in, 0, 0);
424 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
427 if (unformat (input, "%s %v", &s, &v))
429 if (strncmp ((const char *) s, "plugins", 8) == 0)
431 if (vec_len (in.buffer) > 0)
432 vec_add1 (in.buffer, ' ');
433 vec_add (in.buffer, v, vec_len (v));
438 error = clib_error_return (0, "unknown input '%U'",
439 format_unformat_error, input);
448 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
450 unformat_input_t sub_input;
452 if (unformat (input, "path %s", &s))
454 else if (unformat (input, "plugin %s %U", &s,
455 unformat_vlib_cli_sub_input, &sub_input))
457 error = config_one_plugin (vm, (char *) s, &sub_input);
458 unformat_free (&sub_input);
464 error = clib_error_return (0, "unknown input '%U'",
465 format_unformat_error, input);
478 /* discard whole 'plugins' section, as it is already consumed prior to
480 static clib_error_t *
481 plugins_config (vlib_main_t * vm, unformat_input_t * input)
485 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
487 if (unformat (input, "%s", &junk))
493 return clib_error_return (0, "unknown input '%U'",
494 format_unformat_error, input);
499 VLIB_CONFIG_FUNCTION (plugins_config, "plugins");
502 * fd.io coding-style-patch-verification: ON
505 * eval: (c-set-style "gnu")