blob: 4aba95c863ea10fb85e3e643208fba548337083b [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * plugin.c: plugin handling
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#include <vlib/unix/plugin.h>
Damjan Marion3b46cba2017-01-23 21:13:45 +010019#include <vppinfra/elf.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020#include <dlfcn.h>
21#include <dirent.h>
22
23plugin_main_t vlib_plugin_main;
24
Damjan Marion3b46cba2017-01-23 21:13:45 +010025char *vlib_plugin_path __attribute__ ((weak));
26char *vlib_plugin_path = "";
27char *vlib_plugin_app_version __attribute__ ((weak));
28char *vlib_plugin_app_version = "";
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Damjan Marionf935e332017-01-06 14:33:05 +010030void *
31vlib_get_plugin_symbol (char *plugin_name, char *symbol_name)
32{
33 plugin_main_t *pm = &vlib_plugin_main;
34 uword *p;
35 plugin_info_t *pi;
36
37 if ((p = hash_get_mem (pm->plugin_by_name_hash, plugin_name)) == 0)
38 return 0;
39
40 pi = vec_elt_at_index (pm->plugin_info, p[0]);
41 return dlsym (pi->handle, symbol_name);
42}
43
Damjan Marion3b46cba2017-01-23 21:13:45 +010044static char *
45str_array_to_vec (char *array, int len)
46{
47 char c, *r = 0;
48 int n = 0;
49
50 do
51 {
52 c = array[n];
53 vec_add1 (r, c);
54 }
55 while (c && ++n < len);
56
57 if (c)
58 vec_add1 (r, 0);
59
60 return r;
61}
62
Dave Barach9b8ffd92016-07-08 08:13:45 -040063static int
64load_one_plugin (plugin_main_t * pm, plugin_info_t * pi, int from_early_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
Damjan Marion3b46cba2017-01-23 21:13:45 +010066 void *handle;
Dave Barach9b8ffd92016-07-08 08:13:45 -040067 clib_error_t *error;
Damjan Marion3b46cba2017-01-23 21:13:45 +010068 elf_main_t em = { 0 };
69 elf_section_t *section;
70 u8 *data;
71 char *version_required;
72 vlib_plugin_registration_t *reg;
73 plugin_config_t *pc = 0;
74 uword *p;
75
76 if (elf_read_file (&em, (char *) pi->filename))
77 return -1;
78
79 error = elf_get_section_by_name (&em, ".vlib_plugin_registration",
80 &section);
81 if (error)
82 {
83 clib_warning ("Not a plugin: %s\n", (char *) pi->name);
84 return -1;
85 }
86
87 data = elf_get_section_contents (&em, section->index, 1);
88 reg = (vlib_plugin_registration_t *) data;
89
90 if (vec_len (data) != sizeof (*reg))
91 {
92 clib_warning ("vlib_plugin_registration size mismatch in plugin %s\n",
93 (char *) pi->name);
94 goto error;
95 }
96
97 p = hash_get_mem (pm->config_index_by_name, pi->name);
98 if (p)
99 {
100 pc = vec_elt_at_index (pm->configs, p[0]);
101 if (pc->is_disabled)
102 {
103 clib_warning ("Plugin disabled: %s", pi->name);
104 goto error;
105 }
106 if (reg->default_disabled && pc->is_enabled == 0)
107 {
108 clib_warning ("Plugin disabled: %s (default)", pi->name);
109 goto error;
110 }
111 }
112 else if (reg->default_disabled)
113 {
114 clib_warning ("Plugin disabled: %s (default)", pi->name);
115 goto error;
116 }
117
118 version_required = str_array_to_vec ((char *) &reg->version_required,
119 sizeof (reg->version_required));
120
121 if ((strlen (version_required) > 0) &&
122 (strncmp (vlib_plugin_app_version, version_required,
123 strlen (version_required))))
124 {
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))
128 {
129 vec_free (version_required);
130 goto error;
131 }
132 }
133
134 vec_free (version_required);
135 vec_free (data);
136 elf_main_free (&em);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
Damjan Marionf935e332017-01-06 14:33:05 +0100138 handle = dlopen ((char *) pi->filename, RTLD_LAZY);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400139
140 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 * 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...
144 */
145 if (handle == 0)
146 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400147 clib_warning ("%s", dlerror ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 return -1;
149 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400150
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 pi->handle = handle;
152
Damjan Marion3b46cba2017-01-23 21:13:45 +0100153 reg = dlsym (pi->handle, "vlib_plugin_registration");
Ole Troan3b3688f2016-06-15 14:29:08 +0200154
Damjan Marion3b46cba2017-01-23 21:13:45 +0100155 if (reg == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 {
Damjan Marion3b46cba2017-01-23 21:13:45 +0100157 /* This should never happen unless somebody chagnes registration macro */
158 clib_warning ("Missing plugin registration in plugin '%s'", pi->name);
159 os_exit (1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 }
161
Damjan Marion3b46cba2017-01-23 21:13:45 +0100162 pi->reg = reg;
163 pi->version = str_array_to_vec ((char *) &reg->version,
164 sizeof (reg->version));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Damjan Marion3b46cba2017-01-23 21:13:45 +0100166 if (reg && reg->early_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 {
Damjan Marion3b46cba2017-01-23 21:13:45 +0100168 clib_error_t *(*ei) (vlib_main_t *);
169 void *h;
170
171 h = dlsym (pi->handle, reg->early_init);
172 if (h)
173 {
174 ei = h;
175 error = (*ei) (pm->vlib_main);
176 if (error)
177 {
178 clib_error_report (error);
179 os_exit (1);
180 }
181 }
182 else
183 clib_warning ("Plugin %s: early init function %s set but not found",
184 (char *) pi->name, reg->early_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 }
186
187 clib_warning ("Loaded plugin: %s", pi->name);
188
189 return 0;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100190error:
191 vec_free (data);
192 elf_main_free (&em);
193 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194}
195
Dave Barach9b8ffd92016-07-08 08:13:45 -0400196static u8 **
197split_plugin_path (plugin_main_t * pm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198{
199 int i;
200 u8 **rv = 0;
201 u8 *path = pm->plugin_path;
202 u8 *this = 0;
203
204 for (i = 0; i < vec_len (pm->plugin_path); i++)
205 {
206 if (path[i] != ':')
Dave Barach9b8ffd92016-07-08 08:13:45 -0400207 {
208 vec_add1 (this, path[i]);
209 continue;
210 }
211 vec_add1 (this, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 vec_add1 (rv, this);
213 this = 0;
214 }
215 if (this)
216 {
217 vec_add1 (this, 0);
218 vec_add1 (rv, this);
219 }
220 return rv;
221}
222
Dave Barach9b8ffd92016-07-08 08:13:45 -0400223int
224vlib_load_new_plugins (plugin_main_t * pm, int from_early_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225{
226 DIR *dp;
227 struct dirent *entry;
228 struct stat statb;
229 uword *p;
230 plugin_info_t *pi;
231 u8 **plugin_path;
232 int i;
233
234 plugin_path = split_plugin_path (pm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400235
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236 for (i = 0; i < vec_len (plugin_path); i++)
237 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400238 dp = opendir ((char *) plugin_path[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
Dave Barach9b8ffd92016-07-08 08:13:45 -0400240 if (dp == 0)
241 continue;
242
243 while ((entry = readdir (dp)))
244 {
245 u8 *plugin_name;
Damjan Marionf935e332017-01-06 14:33:05 +0100246 u8 *filename;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400247
248 if (pm->plugin_name_filter)
249 {
250 int j;
251 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
252 if (entry->d_name[j] != pm->plugin_name_filter[j])
253 goto next;
254 }
255
Damjan Marionf935e332017-01-06 14:33:05 +0100256 filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
Ole Troan3b3688f2016-06-15 14:29:08 +0200257
258 /* Only accept .so */
Damjan Marionf935e332017-01-06 14:33:05 +0100259 char *ext = strrchr ((const char *) filename, '.');
Dave Barach9b8ffd92016-07-08 08:13:45 -0400260 /* unreadable */
261 if (!ext || (strcmp (ext, ".so") != 0) ||
Damjan Marionf935e332017-01-06 14:33:05 +0100262 stat ((char *) filename, &statb) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400263 {
264 ignore:
Damjan Marionf935e332017-01-06 14:33:05 +0100265 vec_free (filename);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400266 continue;
267 }
268
269 /* a dir or other things which aren't plugins */
270 if (!S_ISREG (statb.st_mode))
271 goto ignore;
272
Damjan Marionf935e332017-01-06 14:33:05 +0100273 plugin_name = format (0, "%s%c", entry->d_name, 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400274 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
275 if (p == 0)
276 {
277 vec_add2 (pm->plugin_info, pi, 1);
278 pi->name = plugin_name;
Damjan Marionf935e332017-01-06 14:33:05 +0100279 pi->filename = filename;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400280 pi->file_info = statb;
281
282 if (load_one_plugin (pm, pi, from_early_init))
283 {
284 vec_free (plugin_name);
Damjan Marionf935e332017-01-06 14:33:05 +0100285 vec_free (filename);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400286 _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1;
Damjan Marionf935e332017-01-06 14:33:05 +0100287 memset (pi, 0, sizeof (*pi));
Dave Barach9b8ffd92016-07-08 08:13:45 -0400288 continue;
289 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400290 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
291 pi - pm->plugin_info);
292 }
293 next:
294 ;
295 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296 closedir (dp);
297 vec_free (plugin_path[i]);
298 }
299 vec_free (plugin_path);
300 return 0;
301}
Dave Barach9b8ffd92016-07-08 08:13:45 -0400302
Dave Barach9b8ffd92016-07-08 08:13:45 -0400303int
304vlib_plugin_early_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305{
306 plugin_main_t *pm = &vlib_plugin_main;
307
Damjan Marion3b46cba2017-01-23 21:13:45 +0100308 if (pm->plugin_path == 0)
309 pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310
311 clib_warning ("plugin path %s", pm->plugin_path);
312
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
314 pm->vlib_main = vm;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400315
316 return vlib_load_new_plugins (pm, 1 /* from_early_init */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317}
Dave Barach9b8ffd92016-07-08 08:13:45 -0400318
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100319static clib_error_t *
320vlib_plugins_show_cmd_fn (vlib_main_t * vm,
Ed Warnicke853e7202016-08-12 11:42:26 -0700321 unformat_input_t * input, vlib_cli_command_t * cmd)
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100322{
323 plugin_main_t *pm = &vlib_plugin_main;
324 u8 *s = 0;
325 u8 *key = 0;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100326 uword value = 0;
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100327 int index = 1;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100328 plugin_info_t *pi;
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100329
Damjan Marion3b46cba2017-01-23 21:13:45 +0100330 s = format (s, " Plugin path is: %s\n\n", pm->plugin_path);
331 s = format (s, " %-41s%s\n", "Plugin", "Version");
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100332
Damjan Marion3b46cba2017-01-23 21:13:45 +0100333 /* *INDENT-OFF* */
Ed Warnicke853e7202016-08-12 11:42:26 -0700334 hash_foreach_mem (key, value, pm->plugin_by_name_hash,
Damjan Marion3b46cba2017-01-23 21:13:45 +0100335 {
336 if (key != 0)
337 {
338 pi = vec_elt_at_index (pm->plugin_info, value);
339 s = format (s, "%3d. %-40s %s\n", index, key, pi->version);
340 index++;
341 }
342 });
343 /* *INDENT-ON* */
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100344
Ed Warnicke853e7202016-08-12 11:42:26 -0700345 vlib_cli_output (vm, "%v", s);
346 vec_free (s);
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100347 return 0;
348}
349
Dave Barachfe6bdfd2017-01-20 19:50:09 -0500350/* *INDENT-OFF* */
Ed Warnicke853e7202016-08-12 11:42:26 -0700351VLIB_CLI_COMMAND (plugins_show_cmd, static) =
352{
Dave Barachfe6bdfd2017-01-20 19:50:09 -0500353 .path = "show plugins",
354 .short_help = "show loaded plugins",
355 .function = vlib_plugins_show_cmd_fn,
356};
357/* *INDENT-ON* */
358
Damjan Marion3b46cba2017-01-23 21:13:45 +0100359static clib_error_t *
360config_one_plugin (vlib_main_t * vm, char *name, unformat_input_t * input)
361{
362 plugin_main_t *pm = &vlib_plugin_main;
363 plugin_config_t *pc;
364 clib_error_t *error = 0;
365 uword *p;
366 int is_enable = 0;
367 int is_disable = 0;
368 int skip_version_check = 0;
369
370 if (pm->config_index_by_name == 0)
371 pm->config_index_by_name = hash_create_string (0, sizeof (uword));
372
373 p = hash_get_mem (pm->config_index_by_name, name);
374
375 if (p)
376 {
377 error = clib_error_return (0, "plugin '%s' already configured", name);
378 goto done;
379 }
380
381 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
382 {
383 if (unformat (input, "enable"))
384 is_enable = 1;
385 else if (unformat (input, "disable"))
386 is_disable = 1;
387 else if (unformat (input, "skip-version-check"))
388 skip_version_check = 1;
389 else
390 {
391 error = clib_error_return (0, "unknown input '%U'",
392 format_unformat_error, input);
393 goto done;
394 }
395 }
396
397 if (is_enable && is_disable)
398 {
399 error = clib_error_return (0, "please specify either enable or disable"
400 " for plugin '%s'", name);
401 goto done;
402 }
403
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;
409 pc->name = name;
410
411done:
412 return error;
413}
414
415clib_error_t *
416vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input)
417{
418 plugin_main_t *pm = &vlib_plugin_main;
419 clib_error_t *error = 0;
420 unformat_input_t in;
421
422 unformat_init (&in, 0, 0);
423
424 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
425 {
426 u8 *s, *v;
427 if (unformat (input, "%s %v", &s, &v))
428 {
429 if (strncmp ((const char *) s, "plugins", 8) == 0)
430 {
431 if (vec_len (in.buffer) > 0)
432 vec_add1 (in.buffer, ' ');
433 vec_add (in.buffer, v, vec_len (v));
434 }
435 }
436 else
437 {
438 error = clib_error_return (0, "unknown input '%U'",
439 format_unformat_error, input);
440 goto done;
441 }
442
443 vec_free (v);
444 vec_free (s);
445 }
446done:
447 input = &in;
448 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
449 {
450 unformat_input_t sub_input;
451 u8 *s = 0;
452 if (unformat (input, "path %s", &s))
453 pm->plugin_path = s;
454 else if (unformat (input, "plugin %s %U", &s,
455 unformat_vlib_cli_sub_input, &sub_input))
456 {
457 error = config_one_plugin (vm, (char *) s, &sub_input);
458 unformat_free (&sub_input);
459 if (error)
460 goto done2;
461 }
462 else
463 {
464 error = clib_error_return (0, "unknown input '%U'",
465 format_unformat_error, input);
466 {
467 vec_free (s);
468 goto done2;
469 }
470 }
471 }
472
473done2:
474 unformat_free (&in);
475 return error;
476}
477
478/* discard whole 'plugins' section, as it is already consumed prior to
479 plugin load */
480static clib_error_t *
481plugins_config (vlib_main_t * vm, unformat_input_t * input)
482{
483 u8 *junk;
484
485 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
486 {
487 if (unformat (input, "%s", &junk))
488 {
489 vec_free (junk);
490 return 0;
491 }
492 else
493 return clib_error_return (0, "unknown input '%U'",
494 format_unformat_error, input);
495 }
496 return 0;
497}
498
499VLIB_CONFIG_FUNCTION (plugins_config, "plugins");
500
Dave Barach9b8ffd92016-07-08 08:13:45 -0400501/*
502 * fd.io coding-style-patch-verification: ON
503 *
504 * Local Variables:
505 * eval: (c-set-style "gnu")
506 * End:
507 */