2 * plugin.h: plugin handling
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef __included_plugin_h__
19 #define __included_plugin_h__
21 #include <vlib/vlib.h>
22 #include <vlib/unix/unix.h>
23 #include <sys/types.h>
30 * Almost anything which can be made to work in a vlib unix
31 * application will also work in a vlib plugin.
33 * The elf-section magic which registers static objects
34 * works so long as plugins are preset when the vlib unix process
35 * starts. But wait: there's more...
37 * 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.
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.
49 * Applications control the plugin search path and name filter by
50 * declaring the variables vlib_plugin_path and vlib_plugin_name_filter.
51 * libvlib.la supplies weak references for these symbols which
52 * effectively disable the scheme. In order for the elf-section magic to
53 * work, static plugins must be loaded at the earliest possible moment.
55 * An application can change these parameters at any time and call
56 * vlib_load_new_plugins().
60 typedef CLIB_PACKED(struct {
62 const char version[32];
63 const char version_required[32];
64 const char overrides[256];
65 const char *early_init;
66 const char *description;
67 }) vlib_plugin_registration_t;
71 * Plugins may also use this registration format, which is
72 * easy enough to emit from e.g. a golang compiler.
76 uword data_segment_offset;
83 vlib_r2_string_t version;
84 vlib_r2_string_t version_required;
85 vlib_r2_string_t overrides;
86 vlib_r2_string_t early_init;
87 vlib_r2_string_t description;
90 #define foreach_r2_string_field \
101 struct stat file_info;
104 /* plugin registration */
105 vlib_plugin_registration_t *reg;
114 u8 skip_version_check;
119 /* loaded plugin info */
120 plugin_info_t *plugin_info;
121 uword *plugin_by_name_hash;
122 uword *plugin_overrides_by_name_hash;
124 /* paths and name filters */
126 u8 *plugin_name_filter;
128 u8 *vat_plugin_name_filter;
129 u8 plugins_default_disable;
131 /* plugin configs and hash by name */
132 plugin_config_t *configs;
133 uword *config_index_by_name;
135 /* Plugin log, avoid filling syslog w/ junk */
136 vlib_log_class_t logger;
139 vlib_main_t *vlib_main;
142 extern plugin_main_t vlib_plugin_main;
144 clib_error_t *vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input);
145 int vlib_plugin_early_init (vlib_main_t * vm);
146 int vlib_load_new_plugins (plugin_main_t * pm, int from_early_init);
147 void *vlib_get_plugin_symbol (char *plugin_name, char *symbol_name);
148 u8 *vlib_get_vat_plugin_path (void);
150 #define VLIB_PLUGIN_REGISTER() \
151 vlib_plugin_registration_t vlib_plugin_registration \
152 CLIB_NOSANITIZE_PLUGIN_REG_SECTION \
153 __clib_export __clib_section(".vlib_plugin_registration")
155 /* Call a plugin init function: used for init function dependencies. */
156 #define vlib_call_plugin_init_function(vm,p,x) \
158 clib_error_t *(*_f)(vlib_main_t *); \
160 clib_error_t * _error = 0; \
161 _fptr= vlib_get_plugin_symbol \
162 (p, CLIB_STRING_MACRO(_vlib_init_function_##x)); \
165 _error = clib_error_return \
166 (0, "Plugin %s and/or symbol %s not found.", \
167 p, CLIB_STRING_MACRO(_vlib_init_function_##x)); \
171 _f = (void *)(_fptr[0]); \
173 if (_fptr && ! hash_get (vm->init_functions_called, _f)) \
175 hash_set1 (vm->init_functions_called, _f); \
181 #endif /* __included_plugin_h__ */
184 * fd.io coding-style-patch-verification: ON
187 * eval: (c-set-style "gnu")