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>
22 plugin_main_t vlib_plugin_main;
25 vlib_set_get_handoff_structure_cb (void *cb)
27 plugin_main_t *pm = &vlib_plugin_main;
28 pm->handoff_structure_get_cb = cb;
32 vnet_get_handoff_structure (void)
36 fp = vlib_plugin_main.handoff_structure_get_cb;
44 vlib_get_plugin_symbol (char *plugin_name, char *symbol_name)
46 plugin_main_t *pm = &vlib_plugin_main;
50 if ((p = hash_get_mem (pm->plugin_by_name_hash, plugin_name)) == 0)
53 pi = vec_elt_at_index (pm->plugin_info, p[0]);
54 return dlsym (pi->handle, symbol_name);
58 load_one_plugin (plugin_main_t * pm, plugin_info_t * pi, int from_early_init)
60 void *handle, *register_handle;
61 clib_error_t *(*fp) (vlib_main_t *, void *, int);
63 void *handoff_structure;
65 handle = dlopen ((char *) pi->filename, RTLD_LAZY);
68 * Note: this can happen if the plugin has an undefined symbol reference,
69 * so print a warning. Otherwise, the poor slob won't know what happened.
70 * Ask me how I know that...
74 clib_warning ("%s", dlerror ());
81 register_handle = dlsym (pi->handle, "vlib_plugin_register");
82 if (register_handle == 0)
85 clib_warning ("Plugin missing vlib_plugin_register: %s\n",
92 handoff_structure = vnet_get_handoff_structure ();
94 if (handoff_structure == 0)
95 error = clib_error_return (0, "handoff structure callback returned 0");
97 error = (*fp) (pm->vlib_main, handoff_structure, from_early_init);
101 clib_error_report (error);
106 clib_warning ("Loaded plugin: %s", pi->name);
112 split_plugin_path (plugin_main_t * pm)
116 u8 *path = pm->plugin_path;
119 for (i = 0; i < vec_len (pm->plugin_path); i++)
123 vec_add1 (this, path[i]);
139 vlib_load_new_plugins (plugin_main_t * pm, int from_early_init)
142 struct dirent *entry;
149 plugin_path = split_plugin_path (pm);
151 for (i = 0; i < vec_len (plugin_path); i++)
153 dp = opendir ((char *) plugin_path[i]);
158 while ((entry = readdir (dp)))
163 if (pm->plugin_name_filter)
166 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
167 if (entry->d_name[j] != pm->plugin_name_filter[j])
171 filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
173 /* Only accept .so */
174 char *ext = strrchr ((const char *) filename, '.');
176 if (!ext || (strcmp (ext, ".so") != 0) ||
177 stat ((char *) filename, &statb) < 0)
184 /* a dir or other things which aren't plugins */
185 if (!S_ISREG (statb.st_mode))
188 plugin_name = format (0, "%s%c", entry->d_name, 0);
189 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
192 vec_add2 (pm->plugin_info, pi, 1);
193 pi->name = plugin_name;
194 pi->filename = filename;
195 pi->file_info = statb;
197 if (load_one_plugin (pm, pi, from_early_init))
199 vec_free (plugin_name);
201 _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1;
202 memset (pi, 0, sizeof (*pi));
205 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
206 pi - pm->plugin_info);
212 vec_free (plugin_path[i]);
214 vec_free (plugin_path);
218 char *vlib_plugin_path __attribute__ ((weak));
219 char *vlib_plugin_path = "";
220 char *vlib_plugin_name_filter __attribute__ ((weak));
221 char *vlib_plugin_name_filter = 0;
224 vlib_plugin_early_init (vlib_main_t * vm)
226 plugin_main_t *pm = &vlib_plugin_main;
228 pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
230 clib_warning ("plugin path %s", pm->plugin_path);
232 if (vlib_plugin_name_filter)
233 pm->plugin_name_filter = format (0, "%s%c", vlib_plugin_name_filter, 0);
235 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
238 return vlib_load_new_plugins (pm, 1 /* from_early_init */ );
241 static clib_error_t *
242 vlib_plugins_show_cmd_fn (vlib_main_t * vm,
243 unformat_input_t * input, vlib_cli_command_t * cmd)
245 plugin_main_t *pm = &vlib_plugin_main;
251 s = format (s, " Plugin path is: %s\n", pm->plugin_path);
252 if (vlib_plugin_name_filter)
253 s = format (s, " Plugin filter: %s\n", vlib_plugin_name_filter);
255 s = format (s, " Plugins loaded: \n");
256 hash_foreach_mem (key, value, pm->plugin_by_name_hash,
259 s = format (s, " %d.%s\n", index, key); index++;}
262 vlib_cli_output (vm, "%v", s);
268 VLIB_CLI_COMMAND (plugins_show_cmd, static) =
270 .path = "show plugins",
271 .short_help = "show loaded plugins",
272 .function = vlib_plugins_show_cmd_fn,
277 * fd.io coding-style-patch-verification: ON
280 * eval: (c-set-style "gnu")