Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 31 | * application will also work in a vlib plugin. |
| 32 | * |
| 33 | * The elf-section magic which registers static objects |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | * works so long as plugins are preset when the vlib unix process |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 35 | * starts. But wait: there's more... |
| 36 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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. |
| 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 Marion | 0eca04d | 2017-01-03 20:11:35 +0100 | [diff] [blame] | 51 | * libvlib.la supplies weak references for these symbols which |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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. |
| 54 | * |
| 55 | * An application can change these parameters at any time and call |
| 56 | * vlib_load_new_plugins(). |
| 57 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 59 | /* *INDENT-OFF* */ |
| 60 | typedef CLIB_PACKED(struct { |
| 61 | u8 default_disabled; |
| 62 | const char version[32]; |
| 63 | const char version_required[32]; |
Dave Barach | 8dc954a | 2020-02-05 17:31:09 -0500 | [diff] [blame^] | 64 | const char overrides[256]; |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 65 | const char *early_init; |
Damjan Marion | 1bfb0dd | 2017-03-22 11:08:39 +0100 | [diff] [blame] | 66 | const char *description; |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 67 | }) vlib_plugin_registration_t; |
| 68 | /* *INDENT-ON* */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 69 | |
| 70 | typedef struct |
| 71 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | u8 *name; |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 73 | u8 *filename; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | struct stat file_info; |
| 75 | void *handle; |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 76 | |
| 77 | /* plugin registration */ |
| 78 | vlib_plugin_registration_t *reg; |
| 79 | char *version; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | } plugin_info_t; |
| 81 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 82 | typedef struct |
| 83 | { |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 84 | char *name; |
| 85 | u8 is_disabled; |
| 86 | u8 is_enabled; |
| 87 | u8 skip_version_check; |
| 88 | } plugin_config_t; |
| 89 | |
| 90 | typedef struct |
| 91 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | /* loaded plugin info */ |
| 93 | plugin_info_t *plugin_info; |
| 94 | uword *plugin_by_name_hash; |
Dave Barach | 8dc954a | 2020-02-05 17:31:09 -0500 | [diff] [blame^] | 95 | uword *plugin_overrides_by_name_hash; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | |
Dave Barach | 0a32b60 | 2017-11-28 18:55:09 -0500 | [diff] [blame] | 97 | /* paths and name filters */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | u8 *plugin_path; |
| 99 | u8 *plugin_name_filter; |
Dave Barach | 0a32b60 | 2017-11-28 18:55:09 -0500 | [diff] [blame] | 100 | u8 *vat_plugin_path; |
| 101 | u8 *vat_plugin_name_filter; |
Mohsin Kazmi | 06bc260 | 2018-03-22 23:45:23 +0100 | [diff] [blame] | 102 | u8 plugins_default_disable; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 104 | /* plugin configs and hash by name */ |
| 105 | plugin_config_t *configs; |
| 106 | uword *config_index_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Dave Barach | 8dc954a | 2020-02-05 17:31:09 -0500 | [diff] [blame^] | 108 | /* Plugin log, avoid filling syslog w/ junk */ |
| 109 | vlib_log_class_t logger; |
| 110 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | /* usual */ |
| 112 | vlib_main_t *vlib_main; |
| 113 | } plugin_main_t; |
| 114 | |
Damjan Marion | 6a7acc2 | 2016-12-19 16:28:36 +0100 | [diff] [blame] | 115 | extern plugin_main_t vlib_plugin_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 117 | clib_error_t *vlib_plugin_config (vlib_main_t * vm, unformat_input_t * input); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 118 | int vlib_plugin_early_init (vlib_main_t * vm); |
| 119 | int vlib_load_new_plugins (plugin_main_t * pm, int from_early_init); |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 120 | void *vlib_get_plugin_symbol (char *plugin_name, char *symbol_name); |
Dave Barach | 0a32b60 | 2017-11-28 18:55:09 -0500 | [diff] [blame] | 121 | u8 *vlib_get_vat_plugin_path (void); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 123 | #define VLIB_PLUGIN_REGISTER() \ |
| 124 | vlib_plugin_registration_t vlib_plugin_registration \ |
| 125 | __attribute__((__section__(".vlib_plugin_registration"))) |
| 126 | |
Dave Barach | cabbee7 | 2017-11-18 08:43:06 -0500 | [diff] [blame] | 127 | /* Call a plugin init function: used for init function dependencies. */ |
| 128 | #define vlib_call_plugin_init_function(vm,p,x) \ |
| 129 | ({ \ |
| 130 | clib_error_t *(*_f)(vlib_main_t *); \ |
| 131 | uword *_fptr = 0; \ |
| 132 | clib_error_t * _error = 0; \ |
| 133 | _fptr= vlib_get_plugin_symbol \ |
| 134 | (p, CLIB_STRING_MACRO(_vlib_init_function_##x)); \ |
| 135 | if (_fptr == 0) \ |
| 136 | { \ |
| 137 | _error = clib_error_return \ |
| 138 | (0, "Plugin %s and/or symbol %s not found.", \ |
| 139 | p, CLIB_STRING_MACRO(_vlib_init_function_##x)); \ |
| 140 | } \ |
| 141 | else \ |
| 142 | { \ |
| 143 | _f = (void *)(_fptr[0]); \ |
| 144 | } \ |
| 145 | if (_fptr && ! hash_get (vm->init_functions_called, _f)) \ |
| 146 | { \ |
| 147 | hash_set1 (vm->init_functions_called, _f); \ |
| 148 | _error = _f (vm); \ |
| 149 | } \ |
| 150 | _error; \ |
| 151 | }) |
| 152 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | #endif /* __included_plugin_h__ */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * fd.io coding-style-patch-verification: ON |
| 157 | * |
| 158 | * Local Variables: |
| 159 | * eval: (c-set-style "gnu") |
| 160 | * End: |
| 161 | */ |