blob: a7d9b9449a5fbb266f9b2a322ada2dbe7d72a5b3 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * plugin.h: 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#ifndef __included_plugin_h__
19#define __included_plugin_h__
20
21#include <vlib/vlib.h>
22#include <vlib/unix/unix.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <unistd.h>
26
27/*
28 * vlib plugin scheme
29 *
30 * Almost anything which can be made to work in a vlib unix
Dave Barach9b8ffd92016-07-08 08:13:45 -040031 * application will also work in a vlib plugin.
32 *
33 * The elf-section magic which registers static objects
Ed Warnickecb9cada2015-12-08 15:45:58 -070034 * works so long as plugins are preset when the vlib unix process
Dave Barach9b8ffd92016-07-08 08:13:45 -040035 * starts. But wait: there's more...
36 *
Ed Warnickecb9cada2015-12-08 15:45:58 -070037 * If an application calls vlib_load_new_plugins() -- possibly after
38 * changing vlib_plugin_main.plugin_path / vlib_plugin_main.plugin_name_filter,
39 * -- new plugins will be loaded. That, in turn, allows considerable
40 * flexibility in terms of adding feature code or fixing bugs without
41 * requiring the data-plane process to restart.
42 *
43 * When the plugin mechanism loads a plugin, it uses dlsym to locate
44 * and call the plugin's function vlib_plugin_register() if it exists.
45 * A plugin which expects to be loaded after the vlib application
46 * starts uses this callback to modify the application. If vlib_plugin_register
47 * returns non-zero, the plugin mechanism dlclose()'s the plugin.
48 *
49 * Applications control the plugin search path and name filter by
50 * declaring the variables vlib_plugin_path and vlib_plugin_name_filter.
Damjan Marion0eca04d2017-01-03 20:11:35 +010051 * libvlib.la supplies weak references for these symbols which
Ed Warnickecb9cada2015-12-08 15:45:58 -070052 * effectively disable the scheme. In order for the elf-section magic to
53 * work, static plugins must be loaded at the earliest possible moment.
54 *
55 * An application can change these parameters at any time and call
56 * vlib_load_new_plugins().
57 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070058
Damjan Marionef4ddfa2021-09-15 15:08:28 +020059typedef struct
60{
Damjan Marion79934e82022-04-05 12:40:31 +020061 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Damjan Marionef4ddfa2021-09-15 15:08:28 +020062 u8 default_disabled : 1;
63 u8 deep_bind : 1;
Matthew Smithd3456b82022-08-23 15:46:25 +000064 const char version[64];
65 const char version_required[64];
Dave Barach8dc954a2020-02-05 17:31:09 -050066 const char overrides[256];
Damjan Marion3b46cba2017-01-23 21:13:45 +010067 const char *early_init;
Damjan Marion1bfb0dd2017-03-22 11:08:39 +010068 const char *description;
Damjan Marion79934e82022-04-05 12:40:31 +020069} vlib_plugin_registration_t;
Dave Barach9b8ffd92016-07-08 08:13:45 -040070
Dave Barach500ba9f2020-10-15 17:07:03 -040071/*
72 * Plugins may also use this registration format, which is
73 * easy enough to emit from e.g. a golang compiler.
74 */
75typedef struct
76{
77 uword data_segment_offset;
78 uword length;
79} vlib_r2_string_t;
80
81typedef struct
82{
83 int default_disabled;
84 vlib_r2_string_t version;
85 vlib_r2_string_t version_required;
86 vlib_r2_string_t overrides;
87 vlib_r2_string_t early_init;
88 vlib_r2_string_t description;
89} vlib_plugin_r2_t;
90
91#define foreach_r2_string_field \
92_(version) \
93_(version_required) \
94_(overrides) \
95_(early_init) \
96_(description)
97
Dave Barach9b8ffd92016-07-08 08:13:45 -040098typedef struct
99{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 u8 *name;
Damjan Marionf935e332017-01-06 14:33:05 +0100101 u8 *filename;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 struct stat file_info;
103 void *handle;
Damjan Marion3b46cba2017-01-23 21:13:45 +0100104
105 /* plugin registration */
106 vlib_plugin_registration_t *reg;
107 char *version;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108} plugin_info_t;
109
Dave Barach9b8ffd92016-07-08 08:13:45 -0400110typedef struct
111{
Damjan Marion3b46cba2017-01-23 21:13:45 +0100112 char *name;
113 u8 is_disabled;
114 u8 is_enabled;
115 u8 skip_version_check;
116} plugin_config_t;
117
118typedef struct
119{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 /* loaded plugin info */
121 plugin_info_t *plugin_info;
122 uword *plugin_by_name_hash;
Dave Barach8dc954a2020-02-05 17:31:09 -0500123 uword *plugin_overrides_by_name_hash;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
Dave Barach0a32b602017-11-28 18:55:09 -0500125 /* paths and name filters */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 u8 *plugin_path;
Damjan Marion5546e432021-09-30 20:04:14 +0200127 u8 *plugin_path_add;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 u8 *plugin_name_filter;
Dave Barach0a32b602017-11-28 18:55:09 -0500129 u8 *vat_plugin_path;
130 u8 *vat_plugin_name_filter;
Mohsin Kazmi06bc2602018-03-22 23:45:23 +0100131 u8 plugins_default_disable;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
Damjan Marion3b46cba2017-01-23 21:13:45 +0100133 /* plugin configs and hash by name */
134 plugin_config_t *configs;
135 uword *config_index_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136
Dave Barach8dc954a2020-02-05 17:31:09 -0500137 /* Plugin log, avoid filling syslog w/ junk */
138 vlib_log_class_t logger;
139
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 /* usual */
141 vlib_main_t *vlib_main;
142} plugin_main_t;
143
Damjan Marion6a7acc22016-12-19 16:28:36 +0100144extern plugin_main_t vlib_plugin_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145
Damjan Marion3b46cba2017-01-23 21:13:45 +0100146clib_error_t *vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400147int vlib_plugin_early_init (vlib_main_t * vm);
148int vlib_load_new_plugins (plugin_main_t * pm, int from_early_init);
Sergey Nikiforove917bf72023-01-14 00:15:13 +0500149void *vlib_get_plugin_symbol (const char *plugin_name,
150 const char *symbol_name);
Dave Barach0a32b602017-11-28 18:55:09 -0500151u8 *vlib_get_vat_plugin_path (void);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Damjan Marion3b46cba2017-01-23 21:13:45 +0100153#define VLIB_PLUGIN_REGISTER() \
154 vlib_plugin_registration_t vlib_plugin_registration \
Damjan Marion931c6f52020-10-17 13:33:32 +0200155 __clib_export __clib_section(".vlib_plugin_registration")
Damjan Marion3b46cba2017-01-23 21:13:45 +0100156
Dave Barachcabbee72017-11-18 08:43:06 -0500157/* Call a plugin init function: used for init function dependencies. */
158#define vlib_call_plugin_init_function(vm,p,x) \
159({ \
160 clib_error_t *(*_f)(vlib_main_t *); \
161 uword *_fptr = 0; \
162 clib_error_t * _error = 0; \
163 _fptr= vlib_get_plugin_symbol \
164 (p, CLIB_STRING_MACRO(_vlib_init_function_##x)); \
165 if (_fptr == 0) \
166 { \
167 _error = clib_error_return \
168 (0, "Plugin %s and/or symbol %s not found.", \
169 p, CLIB_STRING_MACRO(_vlib_init_function_##x)); \
170 } \
171 else \
172 { \
173 _f = (void *)(_fptr[0]); \
174 } \
175 if (_fptr && ! hash_get (vm->init_functions_called, _f)) \
176 { \
177 hash_set1 (vm->init_functions_called, _f); \
178 _error = _f (vm); \
179 } \
180 _error; \
181 })
182
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183#endif /* __included_plugin_h__ */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400184
185/*
186 * fd.io coding-style-patch-verification: ON
187 *
188 * Local Variables:
189 * eval: (c-set-style "gnu")
190 * End:
191 */