blob: 09a96fd2ca43d7ed980b529cf78030180fdea8b3 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * plugin.c: plugin handling
17 */
18
19#include <vat/vat.h>
20#include <vat/plugin.h>
21#include <dlfcn.h>
22#include <dirent.h>
23
24plugin_main_t vat_plugin_main;
25
Dave Barach72d72232016-08-04 10:15:08 -040026static int
27load_one_plugin (plugin_main_t * pm, plugin_info_t * pi)
Ed Warnickecb9cada2015-12-08 15:45:58 -070028{
29 void *handle, *register_handle;
Dave Barach72d72232016-08-04 10:15:08 -040030 clib_error_t *(*fp) (vat_main_t *);
31 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070032
Dave Barach72d72232016-08-04 10:15:08 -040033 handle = dlopen ((char *) pi->name, RTLD_LAZY);
34
35 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -070036 * Note: this can happen if the plugin has an undefined symbol reference,
37 * so print a warning. Otherwise, the poor slob won't know what happened.
38 * Ask me how I know that...
39 */
40 if (handle == 0)
41 {
Dave Barach72d72232016-08-04 10:15:08 -040042 clib_warning ("%s", dlerror ());
Dave Barach3464c862018-03-12 17:38:31 -040043 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 }
Dave Barach72d72232016-08-04 10:15:08 -040045
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 pi->handle = handle;
47
48 register_handle = dlsym (pi->handle, "vat_plugin_register");
49 if (register_handle == 0)
Dave Barach3464c862018-03-12 17:38:31 -040050 {
51 clib_warning ("%s: symbol vat_plugin_register not found", pi->name);
52 dlclose (handle);
53 return 0;
54 }
55
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
57 fp = register_handle;
58
Dave Barach72d72232016-08-04 10:15:08 -040059 error = (*fp) (pm->vat_main);
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
61 if (error)
62 {
63 clib_error_report (error);
64 dlclose (handle);
65 return 1;
66 }
67
68 clib_warning ("Loaded plugin: %s", pi->name);
69
70 return 0;
71}
72
Dave Barach72d72232016-08-04 10:15:08 -040073static u8 **
74split_plugin_path (plugin_main_t * pm)
Ed Warnickecb9cada2015-12-08 15:45:58 -070075{
76 int i;
77 u8 **rv = 0;
78 u8 *path = pm->plugin_path;
79 u8 *this = 0;
80
81 for (i = 0; i < vec_len (pm->plugin_path); i++)
82 {
83 if (path[i] != ':')
Dave Barach72d72232016-08-04 10:15:08 -040084 {
85 vec_add1 (this, path[i]);
86 continue;
87 }
88 vec_add1 (this, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 vec_add1 (rv, this);
90 this = 0;
91 }
92 if (this)
93 {
94 vec_add1 (this, 0);
95 vec_add1 (rv, this);
96 }
97 return rv;
98}
99
Dave Barach72d72232016-08-04 10:15:08 -0400100int
101vat_load_new_plugins (plugin_main_t * pm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102{
103 DIR *dp;
104 struct dirent *entry;
105 struct stat statb;
106 uword *p;
107 plugin_info_t *pi;
108 u8 **plugin_path;
109 int i;
110
111 plugin_path = split_plugin_path (pm);
Dave Barach72d72232016-08-04 10:15:08 -0400112
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 for (i = 0; i < vec_len (plugin_path); i++)
114 {
Dave Barach72d72232016-08-04 10:15:08 -0400115 dp = opendir ((char *) plugin_path[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Dave Barach72d72232016-08-04 10:15:08 -0400117 if (dp == 0)
118 continue;
119
120 while ((entry = readdir (dp)))
121 {
122 u8 *plugin_name;
123
124 if (pm->plugin_name_filter)
125 {
126 int j;
127 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
128 if (entry->d_name[j] != pm->plugin_name_filter[j])
129 goto next;
130 }
131
132 plugin_name = format (0, "%s/%s%c", plugin_path[i],
133 entry->d_name, 0);
134
135 /* unreadable */
136 if (stat ((char *) plugin_name, &statb) < 0)
137 {
138 ignore:
139 vec_free (plugin_name);
140 continue;
141 }
142
143 /* a dir or other things which aren't plugins */
144 if (!S_ISREG (statb.st_mode))
145 goto ignore;
146
147 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
148 if (p == 0)
149 {
150 vec_add2 (pm->plugin_info, pi, 1);
Filip Tehlar1fa06c92021-10-11 15:08:25 +0000151 clib_memset (pi, 0, sizeof (*pi));
Dave Barach72d72232016-08-04 10:15:08 -0400152 pi->name = plugin_name;
153 pi->file_info = statb;
154
155 if (load_one_plugin (pm, pi))
156 {
157 vec_free (plugin_name);
158 _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1;
159 continue;
160 }
Dave Barach72d72232016-08-04 10:15:08 -0400161 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
162 pi - pm->plugin_info);
163 }
164 next:
165 ;
166 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 closedir (dp);
168 vec_free (plugin_path[i]);
169 }
170 vec_free (plugin_path);
171 return 0;
172}
173
174#define QUOTE_(x) #x
175#define QUOTE(x) QUOTE_(x)
176
Dave Barach72d72232016-08-04 10:15:08 -0400177/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 * Load plugins from /usr/lib/vpp_api_test_plugins by default
179 */
180char *vat_plugin_path = "/usr/lib/vpp_api_test_plugins";
Dave Barach72d72232016-08-04 10:15:08 -0400181
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182char *vat_plugin_name_filter = 0;
183
Dave Barach72d72232016-08-04 10:15:08 -0400184int
185vat_plugin_init (vat_main_t * vam)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186{
187 plugin_main_t *pm = &vat_plugin_main;
Dave Barach72d72232016-08-04 10:15:08 -0400188
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 pm->plugin_path = format (0, "%s%c", vat_plugin_path, 0);
190 if (vat_plugin_name_filter)
191 pm->plugin_name_filter = format (0, "%s%c", vat_plugin_name_filter, 0);
192
193 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
194 pm->vat_main = vam;
Dave Barach72d72232016-08-04 10:15:08 -0400195
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196 return vat_load_new_plugins (pm);
197}
Dave Barach72d72232016-08-04 10:15:08 -0400198
199/*
200 * fd.io coding-style-patch-verification: ON
201 *
202 * Local Variables:
203 * eval: (c-set-style "gnu")
204 * End:
205 */