e9846e3bbe2a99f29937cb8b3adde12989b2d8bf
[vpp.git] / src / vlib / unix / plugin.c
1 /*
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>
19 #include <vppinfra/elf.h>
20 #include <dlfcn.h>
21 #include <dirent.h>
22
23 plugin_main_t vlib_plugin_main;
24
25 char *vlib_plugin_path __attribute__ ((weak));
26 char *vlib_plugin_path = "";
27 char *vlib_plugin_app_version __attribute__ ((weak));
28 char *vlib_plugin_app_version = "";
29
30 void *
31 vlib_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
44 static char *
45 str_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
63 static int
64 load_one_plugin (plugin_main_t * pm, plugin_info_t * pi, int from_early_init)
65 {
66   void *handle;
67   clib_error_t *error;
68   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);
137
138   handle = dlopen ((char *) pi->filename, RTLD_LAZY);
139
140   /*
141    * 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     {
147       clib_warning ("%s", dlerror ());
148       return -1;
149     }
150
151   pi->handle = handle;
152
153   reg = dlsym (pi->handle, "vlib_plugin_registration");
154
155   if (reg == 0)
156     {
157       /* This should never happen unless somebody chagnes registration macro */
158       clib_warning ("Missing plugin registration in plugin '%s'", pi->name);
159       os_exit (1);
160     }
161
162   pi->reg = reg;
163   pi->version = str_array_to_vec ((char *) &reg->version,
164                                   sizeof (reg->version));
165
166   if (reg->early_init)
167     {
168       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);
185     }
186
187   clib_warning ("Loaded plugin: %s", pi->name);
188
189   return 0;
190 error:
191   vec_free (data);
192   elf_main_free (&em);
193   return -1;
194 }
195
196 static u8 **
197 split_plugin_path (plugin_main_t * pm)
198 {
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] != ':')
207         {
208           vec_add1 (this, path[i]);
209           continue;
210         }
211       vec_add1 (this, 0);
212       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
223 static int
224 plugin_name_sort_cmp (void *a1, void *a2)
225 {
226   plugin_info_t *p1 = a1;
227   plugin_info_t *p2 = a2;
228
229   return strcmp ((char *) p1->name, (char *) p2->name);
230 }
231
232 int
233 vlib_load_new_plugins (plugin_main_t * pm, int from_early_init)
234 {
235   DIR *dp;
236   struct dirent *entry;
237   struct stat statb;
238   uword *p;
239   plugin_info_t *pi;
240   u8 **plugin_path;
241   u32 *load_fail_indices = 0;
242   int i;
243
244   plugin_path = split_plugin_path (pm);
245
246   for (i = 0; i < vec_len (plugin_path); i++)
247     {
248       dp = opendir ((char *) plugin_path[i]);
249
250       if (dp == 0)
251         continue;
252
253       while ((entry = readdir (dp)))
254         {
255           u8 *plugin_name;
256           u8 *filename;
257
258           if (pm->plugin_name_filter)
259             {
260               int j;
261               for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
262                 if (entry->d_name[j] != pm->plugin_name_filter[j])
263                   goto next;
264             }
265
266           filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
267
268           /* Only accept .so */
269           char *ext = strrchr ((const char *) filename, '.');
270           /* unreadable */
271           if (!ext || (strcmp (ext, ".so") != 0) ||
272               stat ((char *) filename, &statb) < 0)
273             {
274             ignore:
275               vec_free (filename);
276               continue;
277             }
278
279           /* a dir or other things which aren't plugins */
280           if (!S_ISREG (statb.st_mode))
281             goto ignore;
282
283           plugin_name = format (0, "%s%c", entry->d_name, 0);
284           /* Have we seen this plugin already? */
285           p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
286           if (p == 0)
287             {
288               /* No, add it to the plugin vector */
289               vec_add2 (pm->plugin_info, pi, 1);
290               pi->name = plugin_name;
291               pi->filename = filename;
292               pi->file_info = statb;
293               hash_set_mem (pm->plugin_by_name_hash, plugin_name,
294                             pi - pm->plugin_info);
295             }
296         next:
297           ;
298         }
299       closedir (dp);
300       vec_free (plugin_path[i]);
301     }
302   vec_free (plugin_path);
303
304
305   /*
306    * Sort the plugins by name. This is important.
307    * API traces contain absolute message numbers.
308    * Loading plugins in directory (vs. alphabetical) order
309    * makes trace replay incredibly fragile.
310    */
311   vec_sort_with_function (pm->plugin_info, plugin_name_sort_cmp);
312
313   /*
314    * Attempt to load the plugins
315    */
316   for (i = 0; i < vec_len (pm->plugin_info); i++)
317     {
318       pi = vec_elt_at_index (pm->plugin_info, i);
319
320       if (load_one_plugin (pm, pi, from_early_init))
321         {
322           /* Make a note of any which fail to load */
323           vec_add1 (load_fail_indices, i);
324           hash_unset_mem (pm->plugin_by_name_hash, pi->name);
325           vec_free (pi->name);
326           vec_free (pi->filename);
327         }
328     }
329
330   /* Remove plugin info vector elements corresponding to load failures */
331   if (vec_len (load_fail_indices) > 0)
332     {
333       for (i = vec_len (load_fail_indices) - 1; i >= 0; i--)
334         vec_delete (pm->plugin_info, 1, load_fail_indices[i]);
335       vec_free (load_fail_indices);
336     }
337
338   /* Recreate the plugin name hash */
339   for (i = 0; i < vec_len (pm->plugin_info); i++)
340     {
341       pi = vec_elt_at_index (pm->plugin_info, i);
342       hash_unset_mem (pm->plugin_by_name_hash, pi->name);
343       hash_set_mem (pm->plugin_by_name_hash, pi->name, pi - pm->plugin_info);
344     }
345
346   return 0;
347 }
348
349 int
350 vlib_plugin_early_init (vlib_main_t * vm)
351 {
352   plugin_main_t *pm = &vlib_plugin_main;
353
354   if (pm->plugin_path == 0)
355     pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
356
357   clib_warning ("plugin path %s", pm->plugin_path);
358
359   pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
360   pm->vlib_main = vm;
361
362   return vlib_load_new_plugins (pm, 1 /* from_early_init */ );
363 }
364
365 static clib_error_t *
366 vlib_plugins_show_cmd_fn (vlib_main_t * vm,
367                           unformat_input_t * input, vlib_cli_command_t * cmd)
368 {
369   plugin_main_t *pm = &vlib_plugin_main;
370   u8 *s = 0;
371   u8 *key = 0;
372   uword value = 0;
373   int index = 1;
374   plugin_info_t *pi;
375
376   s = format (s, " Plugin path is: %s\n\n", pm->plugin_path);
377   s = format (s, "     %-41s%s\n", "Plugin", "Version");
378
379   /* *INDENT-OFF* */
380   hash_foreach_mem (key, value, pm->plugin_by_name_hash,
381     {
382       if (key != 0)
383         {
384           pi = vec_elt_at_index (pm->plugin_info, value);
385           s = format (s, "%3d. %-40s %s\n", index, key, pi->version);
386           index++;
387         }
388     });
389   /* *INDENT-ON* */
390
391   vlib_cli_output (vm, "%v", s);
392   vec_free (s);
393   return 0;
394 }
395
396 /* *INDENT-OFF* */
397 VLIB_CLI_COMMAND (plugins_show_cmd, static) =
398 {
399   .path = "show plugins",
400   .short_help = "show loaded plugins",
401   .function = vlib_plugins_show_cmd_fn,
402 };
403 /* *INDENT-ON* */
404
405 static clib_error_t *
406 config_one_plugin (vlib_main_t * vm, char *name, unformat_input_t * input)
407 {
408   plugin_main_t *pm = &vlib_plugin_main;
409   plugin_config_t *pc;
410   clib_error_t *error = 0;
411   uword *p;
412   int is_enable = 0;
413   int is_disable = 0;
414   int skip_version_check = 0;
415
416   if (pm->config_index_by_name == 0)
417     pm->config_index_by_name = hash_create_string (0, sizeof (uword));
418
419   p = hash_get_mem (pm->config_index_by_name, name);
420
421   if (p)
422     {
423       error = clib_error_return (0, "plugin '%s' already configured", name);
424       goto done;
425     }
426
427   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
428     {
429       if (unformat (input, "enable"))
430         is_enable = 1;
431       else if (unformat (input, "disable"))
432         is_disable = 1;
433       else if (unformat (input, "skip-version-check"))
434         skip_version_check = 1;
435       else
436         {
437           error = clib_error_return (0, "unknown input '%U'",
438                                      format_unformat_error, input);
439           goto done;
440         }
441     }
442
443   if (is_enable && is_disable)
444     {
445       error = clib_error_return (0, "please specify either enable or disable"
446                                  " for plugin '%s'", name);
447       goto done;
448     }
449
450   vec_add2 (pm->configs, pc, 1);
451   hash_set_mem (pm->config_index_by_name, name, pc - pm->configs);
452   pc->is_enabled = is_enable;
453   pc->is_disabled = is_disable;
454   pc->skip_version_check = skip_version_check;
455   pc->name = name;
456
457 done:
458   return error;
459 }
460
461 clib_error_t *
462 vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input)
463 {
464   plugin_main_t *pm = &vlib_plugin_main;
465   clib_error_t *error = 0;
466   unformat_input_t in;
467
468   unformat_init (&in, 0, 0);
469
470   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
471     {
472       u8 *s, *v;
473       if (unformat (input, "%s %v", &s, &v))
474         {
475           if (strncmp ((const char *) s, "plugins", 8) == 0)
476             {
477               if (vec_len (in.buffer) > 0)
478                 vec_add1 (in.buffer, ' ');
479               vec_add (in.buffer, v, vec_len (v));
480             }
481         }
482       else
483         {
484           error = clib_error_return (0, "unknown input '%U'",
485                                      format_unformat_error, input);
486           goto done;
487         }
488
489       vec_free (v);
490       vec_free (s);
491     }
492 done:
493   input = &in;
494   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
495     {
496       unformat_input_t sub_input;
497       u8 *s = 0;
498       if (unformat (input, "path %s", &s))
499         pm->plugin_path = s;
500       else if (unformat (input, "plugin %s %U", &s,
501                          unformat_vlib_cli_sub_input, &sub_input))
502         {
503           error = config_one_plugin (vm, (char *) s, &sub_input);
504           unformat_free (&sub_input);
505           if (error)
506             goto done2;
507         }
508       else
509         {
510           error = clib_error_return (0, "unknown input '%U'",
511                                      format_unformat_error, input);
512           {
513             vec_free (s);
514             goto done2;
515           }
516         }
517     }
518
519 done2:
520   unformat_free (&in);
521   return error;
522 }
523
524 /* discard whole 'plugins' section, as it is already consumed prior to
525    plugin load */
526 static clib_error_t *
527 plugins_config (vlib_main_t * vm, unformat_input_t * input)
528 {
529   u8 *junk;
530
531   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
532     {
533       if (unformat (input, "%s", &junk))
534         {
535           vec_free (junk);
536           return 0;
537         }
538       else
539         return clib_error_return (0, "unknown input '%U'",
540                                   format_unformat_error, input);
541     }
542   return 0;
543 }
544
545 VLIB_CONFIG_FUNCTION (plugins_config, "plugins");
546
547 /*
548  * fd.io coding-style-patch-verification: ON
549  *
550  * Local Variables:
551  * eval: (c-set-style "gnu")
552  * End:
553  */