blob: 058afcfcae0f9ba3b1e5165c945c19dcaa704212 [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 {
Damjan Marion1bfb0dd2017-03-22 11:08:39 +0100108 clib_warning ("Plugin disabled (default): %s", pi->name);
Damjan Marion3b46cba2017-01-23 21:13:45 +0100109 goto error;
110 }
111 }
112 else if (reg->default_disabled)
113 {
Damjan Marion1bfb0dd2017-03-22 11:08:39 +0100114 clib_warning ("Plugin disabled (default): %s", pi->name);
Damjan Marion3b46cba2017-01-23 21:13:45 +0100115 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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 if (handle == 0)
141 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400142 clib_warning ("%s", dlerror ());
Damjan Marionc9fc77c2017-04-25 21:07:52 +0200143 clib_warning ("Failed to load plugin '%s'", pi->name);
144 os_exit (1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400146
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 pi->handle = handle;
148
Damjan Marion3b46cba2017-01-23 21:13:45 +0100149 reg = dlsym (pi->handle, "vlib_plugin_registration");
Ole Troan3b3688f2016-06-15 14:29:08 +0200150
Damjan Marion3b46cba2017-01-23 21:13:45 +0100151 if (reg == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 {
Damjan Marion3b46cba2017-01-23 21:13:45 +0100153 /* This should never happen unless somebody chagnes registration macro */
154 clib_warning ("Missing plugin registration in plugin '%s'", pi->name);
155 os_exit (1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 }
157
Damjan Marion3b46cba2017-01-23 21:13:45 +0100158 pi->reg = reg;
159 pi->version = str_array_to_vec ((char *) &reg->version,
160 sizeof (reg->version));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Ole Troane4ad8cc2017-02-07 11:22:33 +0100162 if (reg->early_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 {
Damjan Marion3b46cba2017-01-23 21:13:45 +0100164 clib_error_t *(*ei) (vlib_main_t *);
165 void *h;
166
167 h = dlsym (pi->handle, reg->early_init);
168 if (h)
169 {
170 ei = h;
171 error = (*ei) (pm->vlib_main);
172 if (error)
173 {
174 clib_error_report (error);
175 os_exit (1);
176 }
177 }
178 else
179 clib_warning ("Plugin %s: early init function %s set but not found",
180 (char *) pi->name, reg->early_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 }
182
Damjan Marion1bfb0dd2017-03-22 11:08:39 +0100183 if (reg->description)
184 clib_warning ("Loaded plugin: %s (%s)", pi->name, reg->description);
185 else
186 clib_warning ("Loaded plugin: %s", pi->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
188 return 0;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100189error:
190 vec_free (data);
191 elf_main_free (&em);
192 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193}
194
Dave Barach9b8ffd92016-07-08 08:13:45 -0400195static u8 **
196split_plugin_path (plugin_main_t * pm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197{
198 int i;
199 u8 **rv = 0;
200 u8 *path = pm->plugin_path;
201 u8 *this = 0;
202
203 for (i = 0; i < vec_len (pm->plugin_path); i++)
204 {
205 if (path[i] != ':')
Dave Barach9b8ffd92016-07-08 08:13:45 -0400206 {
207 vec_add1 (this, path[i]);
208 continue;
209 }
210 vec_add1 (this, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211 vec_add1 (rv, this);
212 this = 0;
213 }
214 if (this)
215 {
216 vec_add1 (this, 0);
217 vec_add1 (rv, this);
218 }
219 return rv;
220}
221
Dave Baracha83bc302017-02-25 16:38:12 -0500222static int
223plugin_name_sort_cmp (void *a1, void *a2)
224{
225 plugin_info_t *p1 = a1;
226 plugin_info_t *p2 = a2;
227
228 return strcmp ((char *) p1->name, (char *) p2->name);
229}
230
Dave Barach9b8ffd92016-07-08 08:13:45 -0400231int
232vlib_load_new_plugins (plugin_main_t * pm, int from_early_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233{
234 DIR *dp;
235 struct dirent *entry;
236 struct stat statb;
237 uword *p;
238 plugin_info_t *pi;
239 u8 **plugin_path;
Dave Baracha83bc302017-02-25 16:38:12 -0500240 u32 *load_fail_indices = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 int i;
242
243 plugin_path = split_plugin_path (pm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400244
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245 for (i = 0; i < vec_len (plugin_path); i++)
246 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400247 dp = opendir ((char *) plugin_path[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Dave Barach9b8ffd92016-07-08 08:13:45 -0400249 if (dp == 0)
250 continue;
251
252 while ((entry = readdir (dp)))
253 {
254 u8 *plugin_name;
Damjan Marionf935e332017-01-06 14:33:05 +0100255 u8 *filename;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400256
257 if (pm->plugin_name_filter)
258 {
259 int j;
260 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
261 if (entry->d_name[j] != pm->plugin_name_filter[j])
262 goto next;
263 }
264
Damjan Marionf935e332017-01-06 14:33:05 +0100265 filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
Ole Troan3b3688f2016-06-15 14:29:08 +0200266
267 /* Only accept .so */
Damjan Marionf935e332017-01-06 14:33:05 +0100268 char *ext = strrchr ((const char *) filename, '.');
Dave Barach9b8ffd92016-07-08 08:13:45 -0400269 /* unreadable */
270 if (!ext || (strcmp (ext, ".so") != 0) ||
Damjan Marionf935e332017-01-06 14:33:05 +0100271 stat ((char *) filename, &statb) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400272 {
273 ignore:
Damjan Marionf935e332017-01-06 14:33:05 +0100274 vec_free (filename);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400275 continue;
276 }
277
278 /* a dir or other things which aren't plugins */
279 if (!S_ISREG (statb.st_mode))
280 goto ignore;
281
Damjan Marionf935e332017-01-06 14:33:05 +0100282 plugin_name = format (0, "%s%c", entry->d_name, 0);
Dave Baracha83bc302017-02-25 16:38:12 -0500283 /* Have we seen this plugin already? */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400284 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
285 if (p == 0)
286 {
Dave Baracha83bc302017-02-25 16:38:12 -0500287 /* No, add it to the plugin vector */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400288 vec_add2 (pm->plugin_info, pi, 1);
289 pi->name = plugin_name;
Damjan Marionf935e332017-01-06 14:33:05 +0100290 pi->filename = filename;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400291 pi->file_info = statb;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400292 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
293 pi - pm->plugin_info);
294 }
295 next:
296 ;
297 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 closedir (dp);
299 vec_free (plugin_path[i]);
300 }
301 vec_free (plugin_path);
Dave Baracha83bc302017-02-25 16:38:12 -0500302
303
304 /*
305 * Sort the plugins by name. This is important.
306 * API traces contain absolute message numbers.
307 * Loading plugins in directory (vs. alphabetical) order
308 * makes trace replay incredibly fragile.
309 */
310 vec_sort_with_function (pm->plugin_info, plugin_name_sort_cmp);
311
312 /*
313 * Attempt to load the plugins
314 */
315 for (i = 0; i < vec_len (pm->plugin_info); i++)
316 {
317 pi = vec_elt_at_index (pm->plugin_info, i);
318
319 if (load_one_plugin (pm, pi, from_early_init))
320 {
321 /* Make a note of any which fail to load */
322 vec_add1 (load_fail_indices, i);
323 hash_unset_mem (pm->plugin_by_name_hash, pi->name);
324 vec_free (pi->name);
325 vec_free (pi->filename);
326 }
327 }
328
329 /* Remove plugin info vector elements corresponding to load failures */
330 if (vec_len (load_fail_indices) > 0)
331 {
332 for (i = vec_len (load_fail_indices) - 1; i >= 0; i--)
333 vec_delete (pm->plugin_info, 1, load_fail_indices[i]);
334 vec_free (load_fail_indices);
335 }
336
337 /* Recreate the plugin name hash */
338 for (i = 0; i < vec_len (pm->plugin_info); i++)
339 {
340 pi = vec_elt_at_index (pm->plugin_info, i);
341 hash_unset_mem (pm->plugin_by_name_hash, pi->name);
342 hash_set_mem (pm->plugin_by_name_hash, pi->name, pi - pm->plugin_info);
343 }
344
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 return 0;
346}
Dave Barach9b8ffd92016-07-08 08:13:45 -0400347
Dave Barach9b8ffd92016-07-08 08:13:45 -0400348int
349vlib_plugin_early_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350{
351 plugin_main_t *pm = &vlib_plugin_main;
352
Damjan Marion3b46cba2017-01-23 21:13:45 +0100353 if (pm->plugin_path == 0)
354 pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355
356 clib_warning ("plugin path %s", pm->plugin_path);
357
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
359 pm->vlib_main = vm;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360
361 return vlib_load_new_plugins (pm, 1 /* from_early_init */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362}
Dave Barach9b8ffd92016-07-08 08:13:45 -0400363
Dave Barach0a32b602017-11-28 18:55:09 -0500364u8 *
365vlib_get_vat_plugin_path (void)
366{
367 plugin_main_t *pm = &vlib_plugin_main;
368 return (pm->vat_plugin_path);
369}
370
371u8 *
372vlib_get_vat_plugin_name_filter (void)
373{
374 plugin_main_t *pm = &vlib_plugin_main;
375 return (pm->vat_plugin_name_filter);
376}
377
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100378static clib_error_t *
379vlib_plugins_show_cmd_fn (vlib_main_t * vm,
Ed Warnicke853e7202016-08-12 11:42:26 -0700380 unformat_input_t * input, vlib_cli_command_t * cmd)
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100381{
382 plugin_main_t *pm = &vlib_plugin_main;
383 u8 *s = 0;
384 u8 *key = 0;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100385 uword value = 0;
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100386 int index = 1;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100387 plugin_info_t *pi;
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100388
Damjan Marion3b46cba2017-01-23 21:13:45 +0100389 s = format (s, " Plugin path is: %s\n\n", pm->plugin_path);
Damjan Marion1bfb0dd2017-03-22 11:08:39 +0100390 s = format (s, " %-41s%-33s%s\n", "Plugin", "Version", "Description");
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100391
Damjan Marion3b46cba2017-01-23 21:13:45 +0100392 /* *INDENT-OFF* */
Ed Warnicke853e7202016-08-12 11:42:26 -0700393 hash_foreach_mem (key, value, pm->plugin_by_name_hash,
Damjan Marion3b46cba2017-01-23 21:13:45 +0100394 {
395 if (key != 0)
396 {
397 pi = vec_elt_at_index (pm->plugin_info, value);
Damjan Marion1bfb0dd2017-03-22 11:08:39 +0100398 s = format (s, "%3d. %-40s %-32s %s\n", index, key, pi->version,
399 pi->reg->description ? pi->reg->description : "");
Damjan Marion3b46cba2017-01-23 21:13:45 +0100400 index++;
401 }
402 });
403 /* *INDENT-ON* */
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100404
Ed Warnicke853e7202016-08-12 11:42:26 -0700405 vlib_cli_output (vm, "%v", s);
406 vec_free (s);
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100407 return 0;
408}
409
Dave Barachfe6bdfd2017-01-20 19:50:09 -0500410/* *INDENT-OFF* */
Ed Warnicke853e7202016-08-12 11:42:26 -0700411VLIB_CLI_COMMAND (plugins_show_cmd, static) =
412{
Dave Barachfe6bdfd2017-01-20 19:50:09 -0500413 .path = "show plugins",
414 .short_help = "show loaded plugins",
415 .function = vlib_plugins_show_cmd_fn,
416};
417/* *INDENT-ON* */
418
Damjan Marion3b46cba2017-01-23 21:13:45 +0100419static clib_error_t *
420config_one_plugin (vlib_main_t * vm, char *name, unformat_input_t * input)
421{
422 plugin_main_t *pm = &vlib_plugin_main;
423 plugin_config_t *pc;
424 clib_error_t *error = 0;
425 uword *p;
426 int is_enable = 0;
427 int is_disable = 0;
428 int skip_version_check = 0;
429
430 if (pm->config_index_by_name == 0)
431 pm->config_index_by_name = hash_create_string (0, sizeof (uword));
432
433 p = hash_get_mem (pm->config_index_by_name, name);
434
435 if (p)
436 {
437 error = clib_error_return (0, "plugin '%s' already configured", name);
438 goto done;
439 }
440
441 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
442 {
443 if (unformat (input, "enable"))
444 is_enable = 1;
445 else if (unformat (input, "disable"))
446 is_disable = 1;
447 else if (unformat (input, "skip-version-check"))
448 skip_version_check = 1;
449 else
450 {
451 error = clib_error_return (0, "unknown input '%U'",
452 format_unformat_error, input);
453 goto done;
454 }
455 }
456
457 if (is_enable && is_disable)
458 {
459 error = clib_error_return (0, "please specify either enable or disable"
460 " for plugin '%s'", name);
461 goto done;
462 }
463
464 vec_add2 (pm->configs, pc, 1);
465 hash_set_mem (pm->config_index_by_name, name, pc - pm->configs);
466 pc->is_enabled = is_enable;
467 pc->is_disabled = is_disable;
468 pc->skip_version_check = skip_version_check;
469 pc->name = name;
470
471done:
472 return error;
473}
474
475clib_error_t *
476vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input)
477{
478 plugin_main_t *pm = &vlib_plugin_main;
479 clib_error_t *error = 0;
480 unformat_input_t in;
481
482 unformat_init (&in, 0, 0);
483
484 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
485 {
486 u8 *s, *v;
487 if (unformat (input, "%s %v", &s, &v))
488 {
489 if (strncmp ((const char *) s, "plugins", 8) == 0)
490 {
491 if (vec_len (in.buffer) > 0)
492 vec_add1 (in.buffer, ' ');
493 vec_add (in.buffer, v, vec_len (v));
494 }
495 }
496 else
497 {
498 error = clib_error_return (0, "unknown input '%U'",
499 format_unformat_error, input);
500 goto done;
501 }
502
503 vec_free (v);
504 vec_free (s);
505 }
506done:
507 input = &in;
508 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
509 {
510 unformat_input_t sub_input;
511 u8 *s = 0;
512 if (unformat (input, "path %s", &s))
513 pm->plugin_path = s;
Dave Barach0a32b602017-11-28 18:55:09 -0500514 else if (unformat (input, "name-filter %s", &s))
515 pm->plugin_name_filter = s;
516 else if (unformat (input, "vat-path %s", &s))
517 pm->vat_plugin_path = s;
518 else if (unformat (input, "vat-name-filter %s", &s))
519 pm->vat_plugin_name_filter = s;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100520 else if (unformat (input, "plugin %s %U", &s,
521 unformat_vlib_cli_sub_input, &sub_input))
522 {
523 error = config_one_plugin (vm, (char *) s, &sub_input);
524 unformat_free (&sub_input);
525 if (error)
526 goto done2;
527 }
528 else
529 {
530 error = clib_error_return (0, "unknown input '%U'",
531 format_unformat_error, input);
532 {
533 vec_free (s);
534 goto done2;
535 }
536 }
537 }
538
539done2:
540 unformat_free (&in);
541 return error;
542}
543
544/* discard whole 'plugins' section, as it is already consumed prior to
545 plugin load */
546static clib_error_t *
547plugins_config (vlib_main_t * vm, unformat_input_t * input)
548{
549 u8 *junk;
550
551 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
552 {
553 if (unformat (input, "%s", &junk))
554 {
555 vec_free (junk);
556 return 0;
557 }
558 else
559 return clib_error_return (0, "unknown input '%U'",
560 format_unformat_error, input);
561 }
562 return 0;
563}
564
565VLIB_CONFIG_FUNCTION (plugins_config, "plugins");
566
Dave Barach9b8ffd92016-07-08 08:13:45 -0400567/*
568 * fd.io coding-style-patch-verification: ON
569 *
570 * Local Variables:
571 * eval: (c-set-style "gnu")
572 * End:
573 */