FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
plugin.c
Go to the documentation of this file.
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 
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 {
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 (default): %s", pi->name);
109  goto error;
110  }
111  }
112  else if (reg->default_disabled)
113  {
114  clib_warning ("Plugin disabled (default): %s", 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  if (reg->description)
188  clib_warning ("Loaded plugin: %s (%s)", pi->name, reg->description);
189  else
190  clib_warning ("Loaded plugin: %s", pi->name);
191 
192  return 0;
193 error:
194  vec_free (data);
195  elf_main_free (&em);
196  return -1;
197 }
198 
199 static u8 **
201 {
202  int i;
203  u8 **rv = 0;
204  u8 *path = pm->plugin_path;
205  u8 *this = 0;
206 
207  for (i = 0; i < vec_len (pm->plugin_path); i++)
208  {
209  if (path[i] != ':')
210  {
211  vec_add1 (this, path[i]);
212  continue;
213  }
214  vec_add1 (this, 0);
215  vec_add1 (rv, this);
216  this = 0;
217  }
218  if (this)
219  {
220  vec_add1 (this, 0);
221  vec_add1 (rv, this);
222  }
223  return rv;
224 }
225 
226 static int
227 plugin_name_sort_cmp (void *a1, void *a2)
228 {
229  plugin_info_t *p1 = a1;
230  plugin_info_t *p2 = a2;
231 
232  return strcmp ((char *) p1->name, (char *) p2->name);
233 }
234 
235 int
236 vlib_load_new_plugins (plugin_main_t * pm, int from_early_init)
237 {
238  DIR *dp;
239  struct dirent *entry;
240  struct stat statb;
241  uword *p;
242  plugin_info_t *pi;
243  u8 **plugin_path;
244  u32 *load_fail_indices = 0;
245  int i;
246 
247  plugin_path = split_plugin_path (pm);
248 
249  for (i = 0; i < vec_len (plugin_path); i++)
250  {
251  dp = opendir ((char *) plugin_path[i]);
252 
253  if (dp == 0)
254  continue;
255 
256  while ((entry = readdir (dp)))
257  {
258  u8 *plugin_name;
259  u8 *filename;
260 
261  if (pm->plugin_name_filter)
262  {
263  int j;
264  for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
265  if (entry->d_name[j] != pm->plugin_name_filter[j])
266  goto next;
267  }
268 
269  filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
270 
271  /* Only accept .so */
272  char *ext = strrchr ((const char *) filename, '.');
273  /* unreadable */
274  if (!ext || (strcmp (ext, ".so") != 0) ||
275  stat ((char *) filename, &statb) < 0)
276  {
277  ignore:
278  vec_free (filename);
279  continue;
280  }
281 
282  /* a dir or other things which aren't plugins */
283  if (!S_ISREG (statb.st_mode))
284  goto ignore;
285 
286  plugin_name = format (0, "%s%c", entry->d_name, 0);
287  /* Have we seen this plugin already? */
288  p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
289  if (p == 0)
290  {
291  /* No, add it to the plugin vector */
292  vec_add2 (pm->plugin_info, pi, 1);
293  pi->name = plugin_name;
294  pi->filename = filename;
295  pi->file_info = statb;
296  hash_set_mem (pm->plugin_by_name_hash, plugin_name,
297  pi - pm->plugin_info);
298  }
299  next:
300  ;
301  }
302  closedir (dp);
303  vec_free (plugin_path[i]);
304  }
305  vec_free (plugin_path);
306 
307 
308  /*
309  * Sort the plugins by name. This is important.
310  * API traces contain absolute message numbers.
311  * Loading plugins in directory (vs. alphabetical) order
312  * makes trace replay incredibly fragile.
313  */
315 
316  /*
317  * Attempt to load the plugins
318  */
319  for (i = 0; i < vec_len (pm->plugin_info); i++)
320  {
321  pi = vec_elt_at_index (pm->plugin_info, i);
322 
323  if (load_one_plugin (pm, pi, from_early_init))
324  {
325  /* Make a note of any which fail to load */
326  vec_add1 (load_fail_indices, i);
328  vec_free (pi->name);
329  vec_free (pi->filename);
330  }
331  }
332 
333  /* Remove plugin info vector elements corresponding to load failures */
334  if (vec_len (load_fail_indices) > 0)
335  {
336  for (i = vec_len (load_fail_indices) - 1; i >= 0; i--)
337  vec_delete (pm->plugin_info, 1, load_fail_indices[i]);
338  vec_free (load_fail_indices);
339  }
340 
341  /* Recreate the plugin name hash */
342  for (i = 0; i < vec_len (pm->plugin_info); i++)
343  {
344  pi = vec_elt_at_index (pm->plugin_info, i);
346  hash_set_mem (pm->plugin_by_name_hash, pi->name, pi - pm->plugin_info);
347  }
348 
349  return 0;
350 }
351 
352 int
354 {
356 
357  if (pm->plugin_path == 0)
358  pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
359 
360  clib_warning ("plugin path %s", pm->plugin_path);
361 
362  pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
363  pm->vlib_main = vm;
364 
365  return vlib_load_new_plugins (pm, 1 /* from_early_init */ );
366 }
367 
368 static clib_error_t *
370  unformat_input_t * input, vlib_cli_command_t * cmd)
371 {
373  u8 *s = 0;
374  u8 *key = 0;
375  uword value = 0;
376  int index = 1;
377  plugin_info_t *pi;
378 
379  s = format (s, " Plugin path is: %s\n\n", pm->plugin_path);
380  s = format (s, " %-41s%-33s%s\n", "Plugin", "Version", "Description");
381 
382  /* *INDENT-OFF* */
383  hash_foreach_mem (key, value, pm->plugin_by_name_hash,
384  {
385  if (key != 0)
386  {
387  pi = vec_elt_at_index (pm->plugin_info, value);
388  s = format (s, "%3d. %-40s %-32s %s\n", index, key, pi->version,
389  pi->reg->description ? pi->reg->description : "");
390  index++;
391  }
392  });
393  /* *INDENT-ON* */
394 
395  vlib_cli_output (vm, "%v", s);
396  vec_free (s);
397  return 0;
398 }
399 
400 /* *INDENT-OFF* */
401 VLIB_CLI_COMMAND (plugins_show_cmd, static) =
402 {
403  .path = "show plugins",
404  .short_help = "show loaded plugins",
405  .function = vlib_plugins_show_cmd_fn,
406 };
407 /* *INDENT-ON* */
408 
409 static clib_error_t *
411 {
413  plugin_config_t *pc;
414  clib_error_t *error = 0;
415  uword *p;
416  int is_enable = 0;
417  int is_disable = 0;
418  int skip_version_check = 0;
419 
420  if (pm->config_index_by_name == 0)
421  pm->config_index_by_name = hash_create_string (0, sizeof (uword));
422 
423  p = hash_get_mem (pm->config_index_by_name, name);
424 
425  if (p)
426  {
427  error = clib_error_return (0, "plugin '%s' already configured", name);
428  goto done;
429  }
430 
432  {
433  if (unformat (input, "enable"))
434  is_enable = 1;
435  else if (unformat (input, "disable"))
436  is_disable = 1;
437  else if (unformat (input, "skip-version-check"))
438  skip_version_check = 1;
439  else
440  {
441  error = clib_error_return (0, "unknown input '%U'",
442  format_unformat_error, input);
443  goto done;
444  }
445  }
446 
447  if (is_enable && is_disable)
448  {
449  error = clib_error_return (0, "please specify either enable or disable"
450  " for plugin '%s'", name);
451  goto done;
452  }
453 
454  vec_add2 (pm->configs, pc, 1);
455  hash_set_mem (pm->config_index_by_name, name, pc - pm->configs);
456  pc->is_enabled = is_enable;
457  pc->is_disabled = is_disable;
458  pc->skip_version_check = skip_version_check;
459  pc->name = name;
460 
461 done:
462  return error;
463 }
464 
465 clib_error_t *
467 {
469  clib_error_t *error = 0;
470  unformat_input_t in;
471 
472  unformat_init (&in, 0, 0);
473 
475  {
476  u8 *s, *v;
477  if (unformat (input, "%s %v", &s, &v))
478  {
479  if (strncmp ((const char *) s, "plugins", 8) == 0)
480  {
481  if (vec_len (in.buffer) > 0)
482  vec_add1 (in.buffer, ' ');
483  vec_add (in.buffer, v, vec_len (v));
484  }
485  }
486  else
487  {
488  error = clib_error_return (0, "unknown input '%U'",
489  format_unformat_error, input);
490  goto done;
491  }
492 
493  vec_free (v);
494  vec_free (s);
495  }
496 done:
497  input = &in;
499  {
500  unformat_input_t sub_input;
501  u8 *s = 0;
502  if (unformat (input, "path %s", &s))
503  pm->plugin_path = s;
504  else if (unformat (input, "plugin %s %U", &s,
505  unformat_vlib_cli_sub_input, &sub_input))
506  {
507  error = config_one_plugin (vm, (char *) s, &sub_input);
508  unformat_free (&sub_input);
509  if (error)
510  goto done2;
511  }
512  else
513  {
514  error = clib_error_return (0, "unknown input '%U'",
515  format_unformat_error, input);
516  {
517  vec_free (s);
518  goto done2;
519  }
520  }
521  }
522 
523 done2:
524  unformat_free (&in);
525  return error;
526 }
527 
528 /* discard whole 'plugins' section, as it is already consumed prior to
529  plugin load */
530 static clib_error_t *
532 {
533  u8 *junk;
534 
536  {
537  if (unformat (input, "%s", &junk))
538  {
539  vec_free (junk);
540  return 0;
541  }
542  else
543  return clib_error_return (0, "unknown input '%U'",
544  format_unformat_error, input);
545  }
546  return 0;
547 }
548 
550 
551 /*
552  * fd.io coding-style-patch-verification: ON
553  *
554  * Local Variables:
555  * eval: (c-set-style "gnu")
556  * End:
557  */
plugin_config_t * configs
Definition: plugin.h:100
static void elf_main_free(elf_main_t *em)
Definition: elf.h:926
u8 * filename
Definition: plugin.h:72
u8 * plugin_path
Definition: plugin.h:96
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
u8 * plugin_name_filter
Definition: plugin.h:97
static clib_error_t * plugins_config(vlib_main_t *vm, unformat_input_t *input)
Definition: plugin.c:531
char * name
Definition: plugin.h:83
static int plugin_name_sort_cmp(void *a1, void *a2)
Definition: plugin.c:227
u32 index
Definition: elf.h:857
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
clib_error_t * vlib_plugin_config(vlib_main_t *vm, unformat_input_t *input)
Definition: plugin.c:466
vlib_main_t * vlib_main
Definition: plugin.h:104
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:561
#define hash_set_mem(h, key, value)
Definition: hash.h:274
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:599
static int load_one_plugin(plugin_main_t *pm, plugin_info_t *pi, int from_early_init)
Definition: plugin.c:64
vlib_plugin_registration_t * reg
Definition: plugin.h:77
u8 is_disabled
Definition: plugin.h:84
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:111
u8 * name
Definition: plugin.h:71
int vlib_plugin_early_init(vlib_main_t *vm)
Definition: plugin.c:353
#define hash_create_string(elts, value_bytes)
Definition: hash.h:652
uword * config_index_by_name
Definition: plugin.h:101
#define hash_unset_mem(h, key)
Definition: hash.h:280
#define v
Definition: acl.c:246
char * version
Definition: plugin.h:78
struct _unformat_input_t unformat_input_t
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:119
plugin_info_t * plugin_info
Definition: plugin.h:92
#define hash_foreach_mem(key_var, value_var, h, body)
Definition: hash.h:437
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
svmdb_client_t * c
vlib_main_t * vm
Definition: buffer.c:276
vec_header_t h
Definition: buffer.c:275
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
#define clib_warning(format, args...)
Definition: error.h:59
clib_error_t * elf_read_file(elf_main_t *em, char *file_name)
Definition: elf.c:1329
void os_exit(int code)
Definition: unix-misc.c:180
char * vlib_plugin_app_version
Definition: plugin.c:27
static clib_error_t * config_one_plugin(vlib_main_t *vm, char *name, unformat_input_t *input)
Definition: plugin.c:410
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
unsigned int u32
Definition: types.h:88
static void unformat_init(unformat_input_t *i, uword(*fill_buffer)(unformat_input_t *), void *fill_buffer_arg)
Definition: format.h:151
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:785
static void * elf_get_section_contents(elf_main_t *em, uword section_index, uword elt_size)
Definition: elf.h:958
#define clib_error_report(e)
Definition: error.h:125
int vlib_load_new_plugins(plugin_main_t *pm, int from_early_init)
Definition: plugin.c:236
void * vlib_get_plugin_symbol(char *plugin_name, char *symbol_name)
Definition: plugin.c:31
static u8 ** split_plugin_path(plugin_main_t *pm)
Definition: plugin.c:200
u64 uword
Definition: types.h:112
void * handle
Definition: plugin.h:74
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
uword unformat_vlib_cli_sub_input(unformat_input_t *i, va_list *args)
Definition: cli.c:151
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:960
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
#define hash_get_mem(h, key)
Definition: hash.h:268
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
struct stat file_info
Definition: plugin.h:73
clib_error_t * elf_get_section_by_name(elf_main_t *em, char *section_name, elf_section_t **result)
Definition: elf.c:47
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:577
static char * str_array_to_vec(char *array, int len)
Definition: plugin.c:45
u8 skip_version_check
Definition: plugin.h:86
plugin_main_t vlib_plugin_main
Definition: plugin.c:23
uword * plugin_by_name_hash
Definition: plugin.h:93
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971
static clib_error_t * vlib_plugins_show_cmd_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: plugin.c:369
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169