2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * plugin.c: plugin handling
20 #include <vat/plugin.h>
24 plugin_main_t vat_plugin_main;
26 static vlib_log_class_t vat_builtin_logger;
28 #define PLUGIN_LOG_DBG(...) \
29 do {vlib_log_debug (vat_builtin_logger, __VA_ARGS__);} while(0)
30 #define PLUGIN_LOG_ERR(...) \
31 do {vlib_log_err (vat_builtin_logger, __VA_ARGS__);} while(0)
32 #define PLUGIN_LOG_NOTICE(...) \
33 do {vlib_log_notice (vat_builtin_logger, __VA_ARGS__);} while(0)
36 load_one_vat_plugin (plugin_main_t * pm, plugin_info_t * pi)
38 void *handle, *register_handle;
39 clib_error_t *(*fp) (vat_main_t *);
42 handle = dlopen ((char *) pi->filename, RTLD_LAZY);
45 * Note: this can happen if the plugin has an undefined symbol reference,
46 * so print a warning. Otherwise, the poor slob won't know what happened.
47 * Ask me how I know that...
51 PLUGIN_LOG_ERR ("%s", dlerror ());
57 register_handle = dlsym (pi->handle, "vat_plugin_register");
58 if (register_handle == 0)
60 PLUGIN_LOG_ERR ("%s: symbol vat_plugin_register not found", pi->name);
67 error = (*fp) (pm->vat_main);
71 u8 *err = format (0, "%U%c", format_clib_error, error, 0);
72 PLUGIN_LOG_ERR ((char *) err);
73 clib_error_free (error);
79 PLUGIN_LOG_NOTICE ("Loaded plugin: %s", pi->name);
85 split_plugin_path (plugin_main_t * pm)
89 u8 *path = pm->plugin_path;
92 for (i = 0; i < vec_len (pm->plugin_path); i++)
96 vec_add1 (this, path[i]);
112 vat_load_new_plugins (plugin_main_t * pm)
115 struct dirent *entry;
122 plugin_path = split_plugin_path (pm);
124 for (i = 0; i < vec_len (plugin_path); i++)
126 dp = opendir ((char *) plugin_path[i]);
131 while ((entry = readdir (dp)))
136 if (pm->plugin_name_filter)
139 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
140 if (entry->d_name[j] != pm->plugin_name_filter[j])
144 file_name = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
145 plugin_name = format (0, "%s%c", entry->d_name, 0);
148 if (stat ((char *) file_name, &statb) < 0)
151 vec_free (file_name);
152 vec_free (plugin_name);
156 /* a dir or other things which aren't plugins */
157 if (!S_ISREG (statb.st_mode))
160 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
163 vec_add2 (pm->plugin_info, pi, 1);
164 pi->name = plugin_name;
165 pi->filename = file_name;
166 pi->file_info = statb;
168 if (load_one_vat_plugin (pm, pi))
170 vec_free (file_name);
171 vec_free (plugin_name);
172 _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1;
175 clib_memset (pi, 0, sizeof (*pi));
176 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
177 pi - pm->plugin_info);
183 vec_free (plugin_path[i]);
185 vec_free (plugin_path);
190 #define QUOTE(x) QUOTE_(x)
192 extern char *vat_plugin_path;
194 char *vat_plugin_name_filter = 0;
197 vat_plugin_init (vat_main_t * vam)
199 plugin_main_t *pm = &vat_plugin_main;
200 u8 *vlib_get_vat_plugin_path (void);
201 u8 *vlib_get_vat_plugin_name_filter (void);
203 u8 *plugin_name_filter;
206 vlib_log_register_class_rate_limit ("vat-plug", "load",
207 0x7FFFFFFF /* aka no rate limit */ );
209 plugin_path = vlib_get_vat_plugin_path ();
210 plugin_name_filter = vlib_get_vat_plugin_name_filter ();
213 vat_plugin_path = (char *) plugin_path;
215 if (plugin_name_filter)
216 vat_plugin_name_filter = (char *) plugin_name_filter;
218 pm->plugin_path = format (0, "%s%c", vat_plugin_path, 0);
220 if (vat_plugin_name_filter)
221 pm->plugin_name_filter = format (0, "%s%c", vat_plugin_name_filter, 0);
223 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
226 return vat_load_new_plugins (pm);
230 * fd.io coding-style-patch-verification: ON
233 * eval: (c-set-style "gnu")