Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 <dlfcn.h> |
| 20 | #include <dirent.h> |
| 21 | |
| 22 | plugin_main_t vlib_plugin_main; |
| 23 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 24 | void |
| 25 | vlib_set_get_handoff_structure_cb (void *cb) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 27 | plugin_main_t *pm = &vlib_plugin_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | pm->handoff_structure_get_cb = cb; |
| 29 | } |
| 30 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 31 | static void * |
| 32 | vnet_get_handoff_structure (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 34 | void *(*fp) (void); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | |
| 36 | fp = vlib_plugin_main.handoff_structure_get_cb; |
| 37 | if (fp == 0) |
| 38 | return 0; |
| 39 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 40 | return (*fp) (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 43 | void * |
| 44 | vlib_get_plugin_symbol (char *plugin_name, char *symbol_name) |
| 45 | { |
| 46 | plugin_main_t *pm = &vlib_plugin_main; |
| 47 | uword *p; |
| 48 | plugin_info_t *pi; |
| 49 | |
| 50 | if ((p = hash_get_mem (pm->plugin_by_name_hash, plugin_name)) == 0) |
| 51 | return 0; |
| 52 | |
| 53 | pi = vec_elt_at_index (pm->plugin_info, p[0]); |
| 54 | return dlsym (pi->handle, symbol_name); |
| 55 | } |
| 56 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 57 | static int |
| 58 | load_one_plugin (plugin_main_t * pm, plugin_info_t * pi, int from_early_init) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | { |
| 60 | void *handle, *register_handle; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 61 | clib_error_t *(*fp) (vlib_main_t *, void *, int); |
| 62 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | void *handoff_structure; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 65 | handle = dlopen ((char *) pi->filename, RTLD_LAZY); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 66 | |
| 67 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | * Note: this can happen if the plugin has an undefined symbol reference, |
| 69 | * so print a warning. Otherwise, the poor slob won't know what happened. |
| 70 | * Ask me how I know that... |
| 71 | */ |
| 72 | if (handle == 0) |
| 73 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 74 | clib_warning ("%s", dlerror ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | return -1; |
| 76 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 77 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | pi->handle = handle; |
| 79 | |
Ole Troan | 3b3688f | 2016-06-15 14:29:08 +0200 | [diff] [blame] | 80 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | register_handle = dlsym (pi->handle, "vlib_plugin_register"); |
| 82 | if (register_handle == 0) |
| 83 | { |
| 84 | dlclose (handle); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 85 | clib_warning ("Plugin missing vlib_plugin_register: %s\n", |
| 86 | (char *) pi->name); |
Ole Troan | 3b3688f | 2016-06-15 14:29:08 +0200 | [diff] [blame] | 87 | return 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | fp = register_handle; |
| 91 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 92 | handoff_structure = vnet_get_handoff_structure (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
| 94 | if (handoff_structure == 0) |
| 95 | error = clib_error_return (0, "handoff structure callback returned 0"); |
| 96 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 97 | error = (*fp) (pm->vlib_main, handoff_structure, from_early_init); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
| 99 | if (error) |
| 100 | { |
| 101 | clib_error_report (error); |
| 102 | dlclose (handle); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | clib_warning ("Loaded plugin: %s", pi->name); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 111 | static u8 ** |
| 112 | split_plugin_path (plugin_main_t * pm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | { |
| 114 | int i; |
| 115 | u8 **rv = 0; |
| 116 | u8 *path = pm->plugin_path; |
| 117 | u8 *this = 0; |
| 118 | |
| 119 | for (i = 0; i < vec_len (pm->plugin_path); i++) |
| 120 | { |
| 121 | if (path[i] != ':') |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 122 | { |
| 123 | vec_add1 (this, path[i]); |
| 124 | continue; |
| 125 | } |
| 126 | vec_add1 (this, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | vec_add1 (rv, this); |
| 128 | this = 0; |
| 129 | } |
| 130 | if (this) |
| 131 | { |
| 132 | vec_add1 (this, 0); |
| 133 | vec_add1 (rv, this); |
| 134 | } |
| 135 | return rv; |
| 136 | } |
| 137 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 138 | int |
| 139 | vlib_load_new_plugins (plugin_main_t * pm, int from_early_init) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | { |
| 141 | DIR *dp; |
| 142 | struct dirent *entry; |
| 143 | struct stat statb; |
| 144 | uword *p; |
| 145 | plugin_info_t *pi; |
| 146 | u8 **plugin_path; |
| 147 | int i; |
| 148 | |
| 149 | plugin_path = split_plugin_path (pm); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 150 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | for (i = 0; i < vec_len (plugin_path); i++) |
| 152 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 153 | dp = opendir ((char *) plugin_path[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 155 | if (dp == 0) |
| 156 | continue; |
| 157 | |
| 158 | while ((entry = readdir (dp))) |
| 159 | { |
| 160 | u8 *plugin_name; |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 161 | u8 *filename; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 162 | |
| 163 | if (pm->plugin_name_filter) |
| 164 | { |
| 165 | int j; |
| 166 | for (j = 0; j < vec_len (pm->plugin_name_filter); j++) |
| 167 | if (entry->d_name[j] != pm->plugin_name_filter[j]) |
| 168 | goto next; |
| 169 | } |
| 170 | |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 171 | filename = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0); |
Ole Troan | 3b3688f | 2016-06-15 14:29:08 +0200 | [diff] [blame] | 172 | |
| 173 | /* Only accept .so */ |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 174 | char *ext = strrchr ((const char *) filename, '.'); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 175 | /* unreadable */ |
| 176 | if (!ext || (strcmp (ext, ".so") != 0) || |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 177 | stat ((char *) filename, &statb) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 178 | { |
| 179 | ignore: |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 180 | vec_free (filename); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 181 | continue; |
| 182 | } |
| 183 | |
| 184 | /* a dir or other things which aren't plugins */ |
| 185 | if (!S_ISREG (statb.st_mode)) |
| 186 | goto ignore; |
| 187 | |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 188 | plugin_name = format (0, "%s%c", entry->d_name, 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 189 | p = hash_get_mem (pm->plugin_by_name_hash, plugin_name); |
| 190 | if (p == 0) |
| 191 | { |
| 192 | vec_add2 (pm->plugin_info, pi, 1); |
| 193 | pi->name = plugin_name; |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 194 | pi->filename = filename; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 195 | pi->file_info = statb; |
| 196 | |
| 197 | if (load_one_plugin (pm, pi, from_early_init)) |
| 198 | { |
| 199 | vec_free (plugin_name); |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 200 | vec_free (filename); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 201 | _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1; |
Damjan Marion | f935e33 | 2017-01-06 14:33:05 +0100 | [diff] [blame] | 202 | memset (pi, 0, sizeof (*pi)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 203 | continue; |
| 204 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 205 | hash_set_mem (pm->plugin_by_name_hash, plugin_name, |
| 206 | pi - pm->plugin_info); |
| 207 | } |
| 208 | next: |
| 209 | ; |
| 210 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | closedir (dp); |
| 212 | vec_free (plugin_path[i]); |
| 213 | } |
| 214 | vec_free (plugin_path); |
| 215 | return 0; |
| 216 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 217 | |
| 218 | char *vlib_plugin_path __attribute__ ((weak)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | char *vlib_plugin_path = ""; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 220 | char *vlib_plugin_name_filter __attribute__ ((weak)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | char *vlib_plugin_name_filter = 0; |
| 222 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 223 | int |
| 224 | vlib_plugin_early_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | { |
| 226 | plugin_main_t *pm = &vlib_plugin_main; |
| 227 | |
| 228 | pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0); |
| 229 | |
| 230 | clib_warning ("plugin path %s", pm->plugin_path); |
| 231 | |
| 232 | if (vlib_plugin_name_filter) |
| 233 | pm->plugin_name_filter = format (0, "%s%c", vlib_plugin_name_filter, 0); |
| 234 | |
| 235 | pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword)); |
| 236 | pm->vlib_main = vm; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 237 | |
| 238 | return vlib_load_new_plugins (pm, 1 /* from_early_init */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 240 | |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 241 | static clib_error_t * |
| 242 | vlib_plugins_show_cmd_fn (vlib_main_t * vm, |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 243 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 244 | { |
| 245 | plugin_main_t *pm = &vlib_plugin_main; |
| 246 | u8 *s = 0; |
| 247 | u8 *key = 0; |
| 248 | uword *value = 0; |
| 249 | int index = 1; |
| 250 | |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 251 | s = format (s, " Plugin path is: %s\n", pm->plugin_path); |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 252 | if (vlib_plugin_name_filter) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 253 | s = format (s, " Plugin filter: %s\n", vlib_plugin_name_filter); |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 254 | |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 255 | s = format (s, " Plugins loaded: \n"); |
| 256 | hash_foreach_mem (key, value, pm->plugin_by_name_hash, |
| 257 | { |
| 258 | if (key != 0) |
| 259 | s = format (s, " %d.%s\n", index, key); index++;} |
| 260 | ); |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 261 | |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 262 | vlib_cli_output (vm, "%v", s); |
| 263 | vec_free (s); |
Shwetha Bhandari | f24e5d7 | 2016-07-11 23:08:54 +0100 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 267 | VLIB_CLI_COMMAND (plugins_show_cmd, static) = |
| 268 | { |
| 269 | .path = "show plugins",.short_help = "show loaded plugins",.function = |
| 270 | vlib_plugins_show_cmd_fn,}; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 271 | /* |
| 272 | * fd.io coding-style-patch-verification: ON |
| 273 | * |
| 274 | * Local Variables: |
| 275 | * eval: (c-set-style "gnu") |
| 276 | * End: |
| 277 | */ |