blob: 77e4633e14a74edab40ec993f98f1c3ca7ee75d0 [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
Dave Barach8dc954a2020-02-05 17:31:09 -050025#define PLUGIN_LOG_DBG(...) \
26 do {vlib_log_debug (vlib_plugin_main.logger, __VA_ARGS__);} while(0)
27#define PLUGIN_LOG_ERR(...) \
28 do {vlib_log_err (vlib_plugin_main.logger, __VA_ARGS__);} while(0)
29#define PLUGIN_LOG_NOTICE(...) \
30 do {vlib_log_notice (vlib_plugin_main.logger, __VA_ARGS__);} while(0)
31
Damjan Marion3b46cba2017-01-23 21:13:45 +010032char *vlib_plugin_path __attribute__ ((weak));
33char *vlib_plugin_path = "";
34char *vlib_plugin_app_version __attribute__ ((weak));
35char *vlib_plugin_app_version = "";
Ed Warnickecb9cada2015-12-08 15:45:58 -070036
Damjan Marionf935e332017-01-06 14:33:05 +010037void *
Sergey Nikiforove917bf72023-01-14 00:15:13 +050038vlib_get_plugin_symbol (const char *plugin_name, const char *symbol_name)
Damjan Marionf935e332017-01-06 14:33:05 +010039{
40 plugin_main_t *pm = &vlib_plugin_main;
41 uword *p;
42 plugin_info_t *pi;
43
44 if ((p = hash_get_mem (pm->plugin_by_name_hash, plugin_name)) == 0)
45 return 0;
46
47 pi = vec_elt_at_index (pm->plugin_info, p[0]);
48 return dlsym (pi->handle, symbol_name);
49}
50
Damjan Marion3b46cba2017-01-23 21:13:45 +010051static char *
52str_array_to_vec (char *array, int len)
53{
54 char c, *r = 0;
55 int n = 0;
56
57 do
58 {
59 c = array[n];
60 vec_add1 (r, c);
61 }
62 while (c && ++n < len);
63
64 if (c)
65 vec_add1 (r, 0);
66
67 return r;
68}
69
Dave Barach8dc954a2020-02-05 17:31:09 -050070static u8 *
71extract (u8 * sp, u8 * ep)
72{
73 u8 *rv = 0;
74
75 while (sp <= ep)
76 {
77 vec_add1 (rv, *sp);
78 sp++;
79 }
80 vec_add1 (rv, 0);
81 return rv;
82}
83
Dave Barach500ba9f2020-10-15 17:07:03 -040084/*
85 * If a plugin .so contains a ".vlib_plugin_r2" section,
86 * this function converts the vlib_plugin_r2_t to
87 * a vlib_plugin_registration_t.
88 */
89
90static clib_error_t *
Dave Barach9d5be6a2024-06-01 08:44:28 -040091r2_to_reg (elf_main_t *em, vlib_plugin_r2_t *r2,
92 vlib_plugin_registration_t *reg, elf_section_t *data_section)
Dave Barach500ba9f2020-10-15 17:07:03 -040093{
Dave Barach500ba9f2020-10-15 17:07:03 -040094 uword data_segment_offset;
95 u8 *data;
96
97 /* It turns out that the strings land in the ".data" section */
Dave Barach9d5be6a2024-06-01 08:44:28 -040098 data = elf_get_section_contents (em, data_section->index, 1);
Dave Barach500ba9f2020-10-15 17:07:03 -040099
100 /*
101 * Offsets in the ".vlib_plugin_r2" section
102 * need to have the data section base subtracted from them.
103 * The offset is in the first 8 bytes of the ".data" section
104 */
105
106 data_segment_offset = *((uword *) data);
107
108 /* Relocate pointers, subtract data_segment_offset */
109#define _(a) r2->a.data_segment_offset -= data_segment_offset;
110 foreach_r2_string_field;
111#undef _
112
113 if (r2->version.length >= ARRAY_LEN (reg->version) - 1)
114 return clib_error_return (0, "Version string too long");
115
116 if (r2->version_required.length >= ARRAY_LEN (reg->version_required) - 1)
117 return clib_error_return (0, "Version-required string too long");
118
119 if (r2->overrides.length >= ARRAY_LEN (reg->overrides) - 1)
120 return clib_error_return (0, "Override string too long");
121
122 /* Compatibility with C-initializer */
123 memcpy ((void *) reg->version, data + r2->version.data_segment_offset,
124 r2->version.length);
125 memcpy ((void *) reg->version_required,
126 data + r2->version_required.data_segment_offset,
127 r2->version_required.length);
128 memcpy ((void *) reg->overrides, data + r2->overrides.data_segment_offset,
129 r2->overrides.length);
130
131 if (r2->early_init.length > 0)
132 {
133 u8 *ei = 0;
134 vec_validate (ei, r2->early_init.length + 1);
135 memcpy (ei, data + r2->early_init.data_segment_offset,
136 r2->early_init.length);
137 reg->early_init = (void *) ei;
138 }
139
140 if (r2->description.length > 0)
141 {
142 u8 *desc = 0;
143 vec_validate (desc, r2->description.length + 1);
144 memcpy (desc, data + r2->description.data_segment_offset,
145 r2->description.length);
146 reg->description = (void *) desc;
147 }
148 vec_free (data);
149 return 0;
150}
151
152
Dave Barach9b8ffd92016-07-08 08:13:45 -0400153static int
154load_one_plugin (plugin_main_t * pm, plugin_info_t * pi, int from_early_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155{
Damjan Marion3b46cba2017-01-23 21:13:45 +0100156 void *handle;
Dave Barach500ba9f2020-10-15 17:07:03 -0400157 int reread_reg = 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400158 clib_error_t *error;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100159 elf_main_t em = { 0 };
160 elf_section_t *section;
161 u8 *data;
162 char *version_required;
163 vlib_plugin_registration_t *reg;
Dave Barach500ba9f2020-10-15 17:07:03 -0400164 vlib_plugin_r2_t *r2;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100165 plugin_config_t *pc = 0;
166 uword *p;
167
168 if (elf_read_file (&em, (char *) pi->filename))
169 return -1;
170
Dave Barach500ba9f2020-10-15 17:07:03 -0400171 /* New / improved (well, not really) registration structure? */
172 error = elf_get_section_by_name (&em, ".vlib_plugin_r2", &section);
173 if (error == 0)
174 {
Dave Barach9d5be6a2024-06-01 08:44:28 -0400175 elf_section_t *data_section;
176 elf_relocation_table_t *rt;
177 elf_relocation_with_addend_t *r;
178 elf_symbol_table_t *st;
179 elf64_symbol_t *sym, *symok = 0;
180
Dave Barach500ba9f2020-10-15 17:07:03 -0400181 data = elf_get_section_contents (&em, section->index, 1);
182 r2 = (vlib_plugin_r2_t *) data;
Dave Barach9d5be6a2024-06-01 08:44:28 -0400183
184 elf_get_section_by_name (&em, ".data", &data_section);
185
186 // Find first symbol in .vlib_plugin_r2 section.
187 vec_foreach (st, em.symbol_tables)
188 {
189 vec_foreach (sym, st->symbols)
190 {
191 if (sym->section_index == section->index)
192 {
193 symok = sym;
194 break;
195 }
196 }
197 }
198
199 // Relocate section data as per relocation tables.
200 if (symok != 0)
201 {
202 vec_foreach (rt, em.relocation_tables)
203 {
204 vec_foreach (r, rt->relocations)
205 {
206 if (r->address >= symok->value &&
207 r->address < symok->value + symok->size)
208 {
209 *(uword *) ((void *) data + r->address - symok->value) +=
210 r->addend - data_section->header.exec_address;
211 }
212 }
213 }
214 }
215
Dave Barach500ba9f2020-10-15 17:07:03 -0400216 reg = clib_mem_alloc (sizeof (*reg));
217 memset (reg, 0, sizeof (*reg));
218
219 reg->default_disabled = r2->default_disabled != 0;
Dave Barach9d5be6a2024-06-01 08:44:28 -0400220 error = r2_to_reg (&em, r2, reg, data_section);
Dave Barach500ba9f2020-10-15 17:07:03 -0400221 if (error)
222 {
223 PLUGIN_LOG_ERR ("Bad r2 registration: %s\n", (char *) pi->name);
224 return -1;
225 }
226 if (pm->plugins_default_disable)
227 reg->default_disabled = 1;
228 reread_reg = 0;
229 goto process_reg;
230 }
Damjan Marionc7ef4f32022-04-04 18:04:28 +0200231 else
232 clib_error_free (error);
Dave Barach500ba9f2020-10-15 17:07:03 -0400233
Damjan Marion3b46cba2017-01-23 21:13:45 +0100234 error = elf_get_section_by_name (&em, ".vlib_plugin_registration",
235 &section);
236 if (error)
237 {
Dave Barach8dc954a2020-02-05 17:31:09 -0500238 PLUGIN_LOG_ERR ("Not a plugin: %s\n", (char *) pi->name);
Damjan Marion3b46cba2017-01-23 21:13:45 +0100239 return -1;
240 }
241
242 data = elf_get_section_contents (&em, section->index, 1);
243 reg = (vlib_plugin_registration_t *) data;
244
245 if (vec_len (data) != sizeof (*reg))
246 {
Dave Barach8dc954a2020-02-05 17:31:09 -0500247 PLUGIN_LOG_ERR ("vlib_plugin_registration size mismatch in plugin %s\n",
248 (char *) pi->name);
Damjan Marion3b46cba2017-01-23 21:13:45 +0100249 goto error;
250 }
251
Mohsin Kazmi06bc2602018-03-22 23:45:23 +0100252 if (pm->plugins_default_disable)
253 reg->default_disabled = 1;
254
Dave Barach500ba9f2020-10-15 17:07:03 -0400255process_reg:
Damjan Marion3b46cba2017-01-23 21:13:45 +0100256 p = hash_get_mem (pm->config_index_by_name, pi->name);
257 if (p)
258 {
259 pc = vec_elt_at_index (pm->configs, p[0]);
260 if (pc->is_disabled)
261 {
Dave Barach8dc954a2020-02-05 17:31:09 -0500262 PLUGIN_LOG_NOTICE ("Plugin disabled: %s", pi->name);
Damjan Marion3b46cba2017-01-23 21:13:45 +0100263 goto error;
264 }
265 if (reg->default_disabled && pc->is_enabled == 0)
266 {
Dave Barach8dc954a2020-02-05 17:31:09 -0500267 PLUGIN_LOG_NOTICE ("Plugin disabled (default): %s", pi->name);
Damjan Marion3b46cba2017-01-23 21:13:45 +0100268 goto error;
269 }
270 }
271 else if (reg->default_disabled)
272 {
Dave Barach8dc954a2020-02-05 17:31:09 -0500273 PLUGIN_LOG_NOTICE ("Plugin disabled (default): %s", pi->name);
Damjan Marion3b46cba2017-01-23 21:13:45 +0100274 goto error;
275 }
276
277 version_required = str_array_to_vec ((char *) &reg->version_required,
278 sizeof (reg->version_required));
279
280 if ((strlen (version_required) > 0) &&
281 (strncmp (vlib_plugin_app_version, version_required,
282 strlen (version_required))))
283 {
Dave Barach8dc954a2020-02-05 17:31:09 -0500284 PLUGIN_LOG_ERR ("Plugin %s version mismatch: %s != %s",
285 pi->name, vlib_plugin_app_version,
286 reg->version_required);
Damjan Marion3b46cba2017-01-23 21:13:45 +0100287 if (!(pc && pc->skip_version_check == 1))
288 {
289 vec_free (version_required);
290 goto error;
291 }
292 }
293
Dave Barach8dc954a2020-02-05 17:31:09 -0500294 /*
295 * Collect names of plugins overridden (disabled) by the
296 * current plugin.
297 */
298 if (reg->overrides[0])
299 {
300 const char *overrides = reg->overrides;
301 u8 *override_name_copy, *overridden_by_name_copy;
302 u8 *sp, *ep;
303 uword *p;
304
305 sp = ep = (u8 *) overrides;
306
307 while (1)
308 {
309 if (*sp == 0
310 || (sp >= (u8 *) overrides + ARRAY_LEN (reg->overrides)))
311 break;
312 if (*sp == ' ' || *sp == ',')
313 {
314 sp++;
315 continue;
316 }
317 ep = sp;
318 while (*ep && *ep != ' ' && *ep != ',' &&
319 ep < (u8 *) overrides + ARRAY_LEN (reg->overrides))
320 ep++;
321 if (*ep == ' ' || *ep == ',')
322 ep--;
323
324 override_name_copy = extract (sp, ep);
325
326
327 p = hash_get_mem (pm->plugin_overrides_by_name_hash,
328 override_name_copy);
329 /* Already overridden... */
330 if (p)
331 vec_free (override_name_copy);
332 else
333 {
334 overridden_by_name_copy = format (0, "%s%c", pi->name, 0);
335 hash_set_mem (pm->plugin_overrides_by_name_hash,
336 override_name_copy, overridden_by_name_copy);
337 }
338 sp = *ep ? ep + 1 : ep;
339 }
340 }
Damjan Marion3b46cba2017-01-23 21:13:45 +0100341 vec_free (version_required);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Eric Sunee98c9d2022-02-02 10:53:34 -0800343#if defined(RTLD_DEEPBIND)
Damjan Marionef4ddfa2021-09-15 15:08:28 +0200344 handle = dlopen ((char *) pi->filename,
345 RTLD_LAZY | (reg->deep_bind ? RTLD_DEEPBIND : 0));
Eric Sunee98c9d2022-02-02 10:53:34 -0800346#else
347 handle = dlopen ((char *) pi->filename, RTLD_LAZY);
348#endif
Dave Barach9b8ffd92016-07-08 08:13:45 -0400349
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350 if (handle == 0)
351 {
Dave Barach8dc954a2020-02-05 17:31:09 -0500352 PLUGIN_LOG_ERR ("%s", dlerror ());
353 PLUGIN_LOG_ERR ("Failed to load plugin '%s'", pi->name);
Dave Barach3464c862018-03-12 17:38:31 -0400354 goto error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 }
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357 pi->handle = handle;
358
Dave Barach500ba9f2020-10-15 17:07:03 -0400359 if (reread_reg)
360 reg = dlsym (pi->handle, "vlib_plugin_registration");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
Damjan Marion3b46cba2017-01-23 21:13:45 +0100362 pi->reg = reg;
363 pi->version = str_array_to_vec ((char *) &reg->version,
364 sizeof (reg->version));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
Ole Troane4ad8cc2017-02-07 11:22:33 +0100366 if (reg->early_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 {
Damjan Marion3b46cba2017-01-23 21:13:45 +0100368 clib_error_t *(*ei) (vlib_main_t *);
369 void *h;
370
371 h = dlsym (pi->handle, reg->early_init);
372 if (h)
373 {
374 ei = h;
375 error = (*ei) (pm->vlib_main);
376 if (error)
377 {
Dave Barach8dc954a2020-02-05 17:31:09 -0500378 u8 *err = format (0, "%s: %U%c", pi->name,
379 format_clib_error, error, 0);
380 PLUGIN_LOG_ERR ((char *) err);
381 clib_error_free (error);
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200382 dlclose (pi->handle);
Dave Barach8dc954a2020-02-05 17:31:09 -0500383 pi->handle = 0;
Damjan Marion72d2c4f2018-04-05 21:32:29 +0200384 goto error;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100385 }
386 }
387 else
Dave Barach8dc954a2020-02-05 17:31:09 -0500388 PLUGIN_LOG_ERR ("Plugin %s: early init function %s set but not found",
389 (char *) pi->name, reg->early_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390 }
391
Damjan Marion1bfb0dd2017-03-22 11:08:39 +0100392 if (reg->description)
Dave Barach8dc954a2020-02-05 17:31:09 -0500393 PLUGIN_LOG_NOTICE ("Loaded plugin: %s (%s)", pi->name, reg->description);
Damjan Marion1bfb0dd2017-03-22 11:08:39 +0100394 else
Dave Barach8dc954a2020-02-05 17:31:09 -0500395 PLUGIN_LOG_NOTICE ("Loaded plugin: %s", pi->name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
Dave Barach8dc954a2020-02-05 17:31:09 -0500397 vec_free (data);
398 elf_main_free (&em);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399 return 0;
Dave Barach8dc954a2020-02-05 17:31:09 -0500400
Damjan Marion3b46cba2017-01-23 21:13:45 +0100401error:
402 vec_free (data);
403 elf_main_free (&em);
404 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405}
406
Dave Barach9b8ffd92016-07-08 08:13:45 -0400407static u8 **
408split_plugin_path (plugin_main_t * pm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409{
410 int i;
411 u8 **rv = 0;
412 u8 *path = pm->plugin_path;
413 u8 *this = 0;
414
415 for (i = 0; i < vec_len (pm->plugin_path); i++)
416 {
417 if (path[i] != ':')
Dave Barach9b8ffd92016-07-08 08:13:45 -0400418 {
419 vec_add1 (this, path[i]);
420 continue;
421 }
422 vec_add1 (this, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 vec_add1 (rv, this);
424 this = 0;
425 }
426 if (this)
427 {
428 vec_add1 (this, 0);
429 vec_add1 (rv, this);
430 }
431 return rv;
432}
433
Dave Baracha83bc302017-02-25 16:38:12 -0500434static int
435plugin_name_sort_cmp (void *a1, void *a2)
436{
437 plugin_info_t *p1 = a1;
438 plugin_info_t *p2 = a2;
439
440 return strcmp ((char *) p1->name, (char *) p2->name);
441}
442
Dave Barach8dc954a2020-02-05 17:31:09 -0500443static int
444index_cmp (void *a1, void *a2)
445{
446 uword *i1 = (uword *) a1, *i2 = (uword *) a2;
447
448 if (*i1 < *i2)
449 return -1;
450 else if (*i1 > *i2)
451 return 1;
452 else
453 return 0;
454}
455
Dave Barach9b8ffd92016-07-08 08:13:45 -0400456int
457vlib_load_new_plugins (plugin_main_t * pm, int from_early_init)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458{
459 DIR *dp;
460 struct dirent *entry;
461 struct stat statb;
462 uword *p;
463 plugin_info_t *pi;
464 u8 **plugin_path;
Dave Barach8dc954a2020-02-05 17:31:09 -0500465 uword *not_loaded_indices = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 int i;
467
468 plugin_path = split_plugin_path (pm);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400469
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470 for (i = 0; i < vec_len (plugin_path); i++)
471 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400472 dp = opendir ((char *) plugin_path[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474 if (dp == 0)
475 continue;
476
477 while ((entry = readdir (dp)))
478 {
479 u8 *plugin_name;
Damjan Marionf935e332017-01-06 14:33:05 +0100480 u8 *filename;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400481
482 if (pm->plugin_name_filter)
483 {
484 int j;
485 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
486 if (entry->d_name[j] != pm->plugin_name_filter[j])
487 goto next;
488 }
489
Damjan Marionf935e332017-01-06 14:33:05 +0100490 filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
Ole Troan3b3688f2016-06-15 14:29:08 +0200491
492 /* Only accept .so */
Damjan Marionf935e332017-01-06 14:33:05 +0100493 char *ext = strrchr ((const char *) filename, '.');
Dave Barach9b8ffd92016-07-08 08:13:45 -0400494 /* unreadable */
495 if (!ext || (strcmp (ext, ".so") != 0) ||
Damjan Marionf935e332017-01-06 14:33:05 +0100496 stat ((char *) filename, &statb) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400497 {
498 ignore:
Damjan Marionf935e332017-01-06 14:33:05 +0100499 vec_free (filename);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400500 continue;
501 }
502
503 /* a dir or other things which aren't plugins */
504 if (!S_ISREG (statb.st_mode))
505 goto ignore;
506
Damjan Marionf935e332017-01-06 14:33:05 +0100507 plugin_name = format (0, "%s%c", entry->d_name, 0);
Dave Baracha83bc302017-02-25 16:38:12 -0500508 /* Have we seen this plugin already? */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400509 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
510 if (p == 0)
511 {
Dave Baracha83bc302017-02-25 16:38:12 -0500512 /* No, add it to the plugin vector */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400513 vec_add2 (pm->plugin_info, pi, 1);
514 pi->name = plugin_name;
Damjan Marionf935e332017-01-06 14:33:05 +0100515 pi->filename = filename;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400516 pi->file_info = statb;
Dave Barach8dc954a2020-02-05 17:31:09 -0500517 pi->handle = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400518 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
519 pi - pm->plugin_info);
520 }
521 next:
522 ;
523 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 closedir (dp);
525 vec_free (plugin_path[i]);
526 }
527 vec_free (plugin_path);
Dave Baracha83bc302017-02-25 16:38:12 -0500528
529
530 /*
531 * Sort the plugins by name. This is important.
532 * API traces contain absolute message numbers.
533 * Loading plugins in directory (vs. alphabetical) order
534 * makes trace replay incredibly fragile.
535 */
536 vec_sort_with_function (pm->plugin_info, plugin_name_sort_cmp);
537
538 /*
539 * Attempt to load the plugins
540 */
541 for (i = 0; i < vec_len (pm->plugin_info); i++)
542 {
543 pi = vec_elt_at_index (pm->plugin_info, i);
544
545 if (load_one_plugin (pm, pi, from_early_init))
546 {
547 /* Make a note of any which fail to load */
Dave Barach8dc954a2020-02-05 17:31:09 -0500548 vec_add1 (not_loaded_indices, i);
549 }
550 }
551
552 /*
553 * Honor override list
554 */
555 for (i = 0; i < vec_len (pm->plugin_info); i++)
556 {
557 uword *p;
558
559 pi = vec_elt_at_index (pm->plugin_info, i);
560
561 p = hash_get_mem (pm->plugin_overrides_by_name_hash, pi->name);
562
563 /* Plugin overridden? */
564 if (p)
565 {
566 PLUGIN_LOG_NOTICE ("Plugin '%s' overridden by '%s'", pi->name,
567 p[0]);
568 vec_add1 (not_loaded_indices, i);
569 }
570 }
571
572 /*
573 * Sort the vector of indices to delete to avoid screwing up
574 * the indices as we delete them.
575 */
576 vec_sort_with_function (not_loaded_indices, index_cmp);
577
578 /*
579 * Remove duplicates, which can happen if a plugin is
580 * disabled from the command line and disabled by
581 * a plugin which is loaded.
582 */
583 for (i = 0; i < vec_len (not_loaded_indices); i++)
584 {
585 if (i < vec_len (not_loaded_indices) - 1)
586 {
587 if (not_loaded_indices[i + 1] == not_loaded_indices[i])
588 {
589 vec_delete (not_loaded_indices, 1, i);
590 i--;
591 }
Dave Baracha83bc302017-02-25 16:38:12 -0500592 }
593 }
594
595 /* Remove plugin info vector elements corresponding to load failures */
Dave Barach8dc954a2020-02-05 17:31:09 -0500596 if (vec_len (not_loaded_indices) > 0)
Dave Baracha83bc302017-02-25 16:38:12 -0500597 {
Dave Barach8dc954a2020-02-05 17:31:09 -0500598 for (i = vec_len (not_loaded_indices) - 1; i >= 0; i--)
599 {
600 pi = vec_elt_at_index (pm->plugin_info, not_loaded_indices[i]);
601 hash_unset_mem (pm->plugin_by_name_hash, pi->name);
602 if (pi->handle)
603 {
604 dlclose (pi->handle);
605 PLUGIN_LOG_NOTICE ("Unloaded plugin: %s", pi->name);
606 }
607 vec_free (pi->name);
608 vec_free (pi->filename);
609 vec_delete (pm->plugin_info, 1, not_loaded_indices[i]);
610 }
611 vec_free (not_loaded_indices);
Dave Baracha83bc302017-02-25 16:38:12 -0500612 }
613
614 /* Recreate the plugin name hash */
Dave Barach8dc954a2020-02-05 17:31:09 -0500615 hash_free (pm->plugin_by_name_hash);
616 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
617
Dave Baracha83bc302017-02-25 16:38:12 -0500618 for (i = 0; i < vec_len (pm->plugin_info); i++)
619 {
620 pi = vec_elt_at_index (pm->plugin_info, i);
Dave Baracha83bc302017-02-25 16:38:12 -0500621 hash_set_mem (pm->plugin_by_name_hash, pi->name, pi - pm->plugin_info);
622 }
623
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624 return 0;
625}
Dave Barach9b8ffd92016-07-08 08:13:45 -0400626
Dave Barach9b8ffd92016-07-08 08:13:45 -0400627int
628vlib_plugin_early_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629{
630 plugin_main_t *pm = &vlib_plugin_main;
631
Dave Barach8dc954a2020-02-05 17:31:09 -0500632 pm->logger =
633 vlib_log_register_class_rate_limit ("plugin", "load",
634 0x7FFFFFFF /* aka no rate limit */ );
635
Damjan Marion3b46cba2017-01-23 21:13:45 +0100636 if (pm->plugin_path == 0)
Damjan Marion5546e432021-09-30 20:04:14 +0200637 pm->plugin_path = format (0, "%s", vlib_plugin_path);
638
639 if (pm->plugin_path_add)
640 pm->plugin_path = format (pm->plugin_path, ":%s", pm->plugin_path_add);
641
642 pm->plugin_path = format (pm->plugin_path, "%c", 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
Dave Barach8dc954a2020-02-05 17:31:09 -0500644 PLUGIN_LOG_DBG ("plugin path %s", pm->plugin_path);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
Dave Barach8dc954a2020-02-05 17:31:09 -0500647 pm->plugin_overrides_by_name_hash = hash_create_string (0, sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648 pm->vlib_main = vm;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400649
650 return vlib_load_new_plugins (pm, 1 /* from_early_init */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651}
Dave Barach9b8ffd92016-07-08 08:13:45 -0400652
Dave Barach0a32b602017-11-28 18:55:09 -0500653u8 *
654vlib_get_vat_plugin_path (void)
655{
656 plugin_main_t *pm = &vlib_plugin_main;
657 return (pm->vat_plugin_path);
658}
659
660u8 *
661vlib_get_vat_plugin_name_filter (void)
662{
663 plugin_main_t *pm = &vlib_plugin_main;
664 return (pm->vat_plugin_name_filter);
665}
666
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100667static clib_error_t *
668vlib_plugins_show_cmd_fn (vlib_main_t * vm,
Ed Warnicke853e7202016-08-12 11:42:26 -0700669 unformat_input_t * input, vlib_cli_command_t * cmd)
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100670{
671 plugin_main_t *pm = &vlib_plugin_main;
672 u8 *s = 0;
673 u8 *key = 0;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100674 uword value = 0;
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100675 int index = 1;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100676 plugin_info_t *pi;
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100677
Damjan Marion3b46cba2017-01-23 21:13:45 +0100678 s = format (s, " Plugin path is: %s\n\n", pm->plugin_path);
Damjan Marion1bfb0dd2017-03-22 11:08:39 +0100679 s = format (s, " %-41s%-33s%s\n", "Plugin", "Version", "Description");
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100680
Ed Warnicke853e7202016-08-12 11:42:26 -0700681 hash_foreach_mem (key, value, pm->plugin_by_name_hash,
Damjan Marion3b46cba2017-01-23 21:13:45 +0100682 {
683 if (key != 0)
684 {
685 pi = vec_elt_at_index (pm->plugin_info, value);
Damjan Marion1bfb0dd2017-03-22 11:08:39 +0100686 s = format (s, "%3d. %-40s %-32s %s\n", index, key, pi->version,
Dave Barach8dc954a2020-02-05 17:31:09 -0500687 (pi->reg && pi->reg->description) ?
688 pi->reg->description : "");
Damjan Marion3b46cba2017-01-23 21:13:45 +0100689 index++;
690 }
691 });
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100692
Ed Warnicke853e7202016-08-12 11:42:26 -0700693 vlib_cli_output (vm, "%v", s);
694 vec_free (s);
Shwetha Bhandarif24e5d72016-07-11 23:08:54 +0100695 return 0;
696}
697
Ed Warnicke853e7202016-08-12 11:42:26 -0700698VLIB_CLI_COMMAND (plugins_show_cmd, static) =
699{
Dave Barachfe6bdfd2017-01-20 19:50:09 -0500700 .path = "show plugins",
701 .short_help = "show loaded plugins",
702 .function = vlib_plugins_show_cmd_fn,
703};
Dave Barachfe6bdfd2017-01-20 19:50:09 -0500704
Damjan Marion3b46cba2017-01-23 21:13:45 +0100705static clib_error_t *
706config_one_plugin (vlib_main_t * vm, char *name, unformat_input_t * input)
707{
708 plugin_main_t *pm = &vlib_plugin_main;
709 plugin_config_t *pc;
710 clib_error_t *error = 0;
711 uword *p;
712 int is_enable = 0;
713 int is_disable = 0;
714 int skip_version_check = 0;
715
716 if (pm->config_index_by_name == 0)
717 pm->config_index_by_name = hash_create_string (0, sizeof (uword));
718
719 p = hash_get_mem (pm->config_index_by_name, name);
720
721 if (p)
722 {
723 error = clib_error_return (0, "plugin '%s' already configured", name);
724 goto done;
725 }
726
727 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
728 {
729 if (unformat (input, "enable"))
730 is_enable = 1;
731 else if (unformat (input, "disable"))
732 is_disable = 1;
733 else if (unformat (input, "skip-version-check"))
734 skip_version_check = 1;
735 else
736 {
737 error = clib_error_return (0, "unknown input '%U'",
738 format_unformat_error, input);
739 goto done;
740 }
741 }
742
743 if (is_enable && is_disable)
744 {
745 error = clib_error_return (0, "please specify either enable or disable"
746 " for plugin '%s'", name);
747 goto done;
748 }
749
750 vec_add2 (pm->configs, pc, 1);
751 hash_set_mem (pm->config_index_by_name, name, pc - pm->configs);
752 pc->is_enabled = is_enable;
753 pc->is_disabled = is_disable;
754 pc->skip_version_check = skip_version_check;
755 pc->name = name;
756
757done:
758 return error;
759}
760
761clib_error_t *
762vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input)
763{
764 plugin_main_t *pm = &vlib_plugin_main;
765 clib_error_t *error = 0;
766 unformat_input_t in;
767
768 unformat_init (&in, 0, 0);
769
770 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
771 {
772 u8 *s, *v;
773 if (unformat (input, "%s %v", &s, &v))
774 {
775 if (strncmp ((const char *) s, "plugins", 8) == 0)
776 {
777 if (vec_len (in.buffer) > 0)
778 vec_add1 (in.buffer, ' ');
779 vec_add (in.buffer, v, vec_len (v));
780 }
781 }
782 else
783 {
784 error = clib_error_return (0, "unknown input '%U'",
785 format_unformat_error, input);
786 goto done;
787 }
788
789 vec_free (v);
790 vec_free (s);
791 }
792done:
793 input = &in;
794 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
795 {
796 unformat_input_t sub_input;
797 u8 *s = 0;
798 if (unformat (input, "path %s", &s))
799 pm->plugin_path = s;
Damjan Marion5546e432021-09-30 20:04:14 +0200800 else if (unformat (input, "add-path %s", &s))
801 pm->plugin_path_add = s;
Dave Barach0a32b602017-11-28 18:55:09 -0500802 else if (unformat (input, "name-filter %s", &s))
803 pm->plugin_name_filter = s;
804 else if (unformat (input, "vat-path %s", &s))
805 pm->vat_plugin_path = s;
806 else if (unformat (input, "vat-name-filter %s", &s))
807 pm->vat_plugin_name_filter = s;
Mohsin Kazmi06bc2602018-03-22 23:45:23 +0100808 else if (unformat (input, "plugin default %U",
809 unformat_vlib_cli_sub_input, &sub_input))
810 {
811 pm->plugins_default_disable =
812 unformat (&sub_input, "disable") ? 1 : 0;
813 unformat_free (&sub_input);
814 }
Damjan Marion3b46cba2017-01-23 21:13:45 +0100815 else if (unformat (input, "plugin %s %U", &s,
816 unformat_vlib_cli_sub_input, &sub_input))
817 {
818 error = config_one_plugin (vm, (char *) s, &sub_input);
819 unformat_free (&sub_input);
820 if (error)
821 goto done2;
822 }
823 else
824 {
825 error = clib_error_return (0, "unknown input '%U'",
826 format_unformat_error, input);
827 {
828 vec_free (s);
829 goto done2;
830 }
831 }
832 }
833
834done2:
835 unformat_free (&in);
836 return error;
837}
838
839/* discard whole 'plugins' section, as it is already consumed prior to
840 plugin load */
841static clib_error_t *
842plugins_config (vlib_main_t * vm, unformat_input_t * input)
843{
844 u8 *junk;
845
846 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
847 {
848 if (unformat (input, "%s", &junk))
849 {
850 vec_free (junk);
851 return 0;
852 }
853 else
854 return clib_error_return (0, "unknown input '%U'",
855 format_unformat_error, input);
856 }
857 return 0;
858}
859
860VLIB_CONFIG_FUNCTION (plugins_config, "plugins");
861
Dave Barach9b8ffd92016-07-08 08:13:45 -0400862/*
863 * fd.io coding-style-patch-verification: ON
864 *
865 * Local Variables:
866 * eval: (c-set-style "gnu")
867 * End:
868 */