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;
24 void vlib_set_get_handoff_structure_cb (void *cb)
26 plugin_main_t * pm = &vlib_plugin_main;
27 pm->handoff_structure_get_cb = cb;
30 static void * vnet_get_handoff_structure (void)
34 fp = vlib_plugin_main.handoff_structure_get_cb;
42 load_one_plugin (plugin_main_t *pm, plugin_info_t *pi, int from_early_init)
44 void *handle, *register_handle;
45 clib_error_t * (*fp)(vlib_main_t *, void *, int);
47 void *handoff_structure;
49 handle = dlopen ((char *)pi->name, RTLD_LAZY);
52 * Note: this can happen if the plugin has an undefined symbol reference,
53 * so print a warning. Otherwise, the poor slob won't know what happened.
54 * Ask me how I know that...
58 clib_warning ("%s", dlerror());
64 register_handle = dlsym (pi->handle, "vlib_plugin_register");
65 if (register_handle == 0)
73 handoff_structure = vnet_get_handoff_structure();
75 if (handoff_structure == 0)
76 error = clib_error_return (0, "handoff structure callback returned 0");
78 error = (*fp)(pm->vlib_main, handoff_structure, from_early_init);
82 clib_error_report (error);
87 clib_warning ("Loaded plugin: %s", pi->name);
92 static u8 **split_plugin_path (plugin_main_t *pm)
96 u8 *path = pm->plugin_path;
99 for (i = 0; i < vec_len (pm->plugin_path); i++)
103 vec_add1(this, path[i]);
118 int vlib_load_new_plugins (plugin_main_t *pm, int from_early_init)
121 struct dirent *entry;
128 plugin_path = split_plugin_path (pm);
130 for (i = 0; i < vec_len (plugin_path); i++)
132 dp = opendir ((char *)plugin_path[i]);
137 while ((entry = readdir (dp)))
141 if (pm->plugin_name_filter)
144 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
145 if (entry->d_name[j] != pm->plugin_name_filter[j])
149 plugin_name = format (0, "%s/%s%c", plugin_path[i],
153 if (stat ((char *)plugin_name, &statb) < 0)
156 vec_free (plugin_name);
160 /* a dir or other things which aren't plugins */
161 if (!S_ISREG(statb.st_mode))
164 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
167 vec_add2 (pm->plugin_info, pi, 1);
168 pi->name = plugin_name;
169 pi->file_info = statb;
171 if (load_one_plugin (pm, pi, from_early_init))
173 vec_free (plugin_name);
174 _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1;
177 memset (pi, 0, sizeof (*pi));
178 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
179 pi - pm->plugin_info);
185 vec_free (plugin_path[i]);
187 vec_free (plugin_path);
190 char *vlib_plugin_path __attribute__((weak));
191 char *vlib_plugin_path = "";
192 char *vlib_plugin_name_filter __attribute__((weak));
193 char *vlib_plugin_name_filter = 0;
195 int vlib_plugin_early_init (vlib_main_t *vm)
197 plugin_main_t *pm = &vlib_plugin_main;
199 pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
201 clib_warning ("plugin path %s", pm->plugin_path);
203 if (vlib_plugin_name_filter)
204 pm->plugin_name_filter = format (0, "%s%c", vlib_plugin_name_filter, 0);
206 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
209 return vlib_load_new_plugins (pm, 1 /* from_early_init */);