FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
plugin.c
Go to the documentation of this file.
1 /*
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 
25 
26 static int
28 {
29  void *handle, *register_handle;
30  clib_error_t *(*fp) (vat_main_t *);
31  clib_error_t *error;
32 
33  handle = dlopen ((char *) pi->filename, RTLD_LAZY);
34 
35  /*
36  * 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  {
42  clib_warning ("%s", dlerror ());
43  return 0;
44  }
45 
46  pi->handle = handle;
47 
48  register_handle = dlsym (pi->handle, "vat_plugin_register");
49  if (register_handle == 0)
50  {
51  clib_warning ("%s: symbol vat_plugin_register not found", pi->name);
52  dlclose (handle);
53  return 0;
54  }
55 
56  fp = register_handle;
57 
58  error = (*fp) (pm->vat_main);
59 
60  if (error)
61  {
62  clib_error_report (error);
63  dlclose (handle);
64  return 1;
65  }
66 
67  clib_warning ("Loaded plugin: %s", pi->name);
68 
69  return 0;
70 }
71 
72 static u8 **
74 {
75  int i;
76  u8 **rv = 0;
77  u8 *path = pm->plugin_path;
78  u8 *this = 0;
79 
80  for (i = 0; i < vec_len (pm->plugin_path); i++)
81  {
82  if (path[i] != ':')
83  {
84  vec_add1 (this, path[i]);
85  continue;
86  }
87  vec_add1 (this, 0);
88  vec_add1 (rv, this);
89  this = 0;
90  }
91  if (this)
92  {
93  vec_add1 (this, 0);
94  vec_add1 (rv, this);
95  }
96  return rv;
97 }
98 
99 int
101 {
102  DIR *dp;
103  struct dirent *entry;
104  struct stat statb;
105  uword *p;
106  plugin_info_t *pi;
107  u8 **plugin_path;
108  int i;
109 
110  plugin_path = split_plugin_path (pm);
111 
112  for (i = 0; i < vec_len (plugin_path); i++)
113  {
114  dp = opendir ((char *) plugin_path[i]);
115 
116  if (dp == 0)
117  continue;
118 
119  while ((entry = readdir (dp)))
120  {
121  u8 *plugin_name;
122  u8 *file_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  file_name = format (0, "%s/%s%c", plugin_path[i], entry->d_name, 0);
133  plugin_name = format (0, "%s%c", entry->d_name, 0);
134 
135  /* unreadable */
136  if (stat ((char *) file_name, &statb) < 0)
137  {
138  ignore:
139  vec_free (file_name);
140  vec_free (plugin_name);
141  continue;
142  }
143 
144  /* a dir or other things which aren't plugins */
145  if (!S_ISREG (statb.st_mode))
146  goto ignore;
147 
148  p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
149  if (p == 0)
150  {
151  vec_add2 (pm->plugin_info, pi, 1);
152  pi->name = plugin_name;
153  pi->filename = file_name;
154  pi->file_info = statb;
155 
156  if (load_one_vat_plugin (pm, pi))
157  {
158  vec_free (file_name);
159  vec_free (plugin_name);
160  _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1;
161  continue;
162  }
163  memset (pi, 0, sizeof (*pi));
164  hash_set_mem (pm->plugin_by_name_hash, plugin_name,
165  pi - pm->plugin_info);
166  }
167  next:
168  ;
169  }
170  closedir (dp);
171  vec_free (plugin_path[i]);
172  }
173  vec_free (plugin_path);
174  return 0;
175 }
176 
177 #define QUOTE_(x) #x
178 #define QUOTE(x) QUOTE_(x)
179 
180 /*
181  * Load plugins from /usr/lib/vpp_api_test_plugins by default
182  */
183 char *vat_plugin_path = "/usr/lib/vpp_api_test_plugins";
184 
186 
187 int
188 vat_plugin_init (vat_main_t * vam)
189 {
193  u8 *plugin_path;
194  u8 *plugin_name_filter;
195 
196  plugin_path = vlib_get_vat_plugin_path ();
197  plugin_name_filter = vlib_get_vat_plugin_name_filter ();
198 
199  if (plugin_path)
200  vat_plugin_path = (char *) plugin_path;
201 
202  if (plugin_name_filter)
203  vat_plugin_name_filter = (char *) plugin_name_filter;
204 
205  pm->plugin_path = format (0, "%s%c", vat_plugin_path, 0);
206 
208  pm->plugin_name_filter = format (0, "%s%c", vat_plugin_name_filter, 0);
209 
210  pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
211  pm->vat_main = vam;
212 
213  return vat_load_new_plugins (pm);
214 }
215 
216 /*
217  * fd.io coding-style-patch-verification: ON
218  *
219  * Local Variables:
220  * eval: (c-set-style "gnu")
221  * End:
222  */
u8 * vlib_get_vat_plugin_name_filter(void)
Definition: plugin.c:377
u8 * filename
Definition: plugin.h:72
u8 * plugin_path
Definition: plugin.h:96
u8 * plugin_name_filter
Definition: plugin.h:97
char * vat_plugin_path
Definition: plugin.c:183
u8 * vlib_get_vat_plugin_path(void)
Definition: plugin.c:370
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:562
int i
#define hash_set_mem(h, key, value)
Definition: hash.h:275
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
unsigned char u8
Definition: types.h:56
char * vat_plugin_name_filter
Definition: plugin.c:185
u8 * name
Definition: plugin.h:71
#define hash_create_string(elts, value_bytes)
Definition: hash.h:690
static int load_one_vat_plugin(plugin_main_t *pm, plugin_info_t *pi)
Definition: plugin.c:27
plugin_info_t * plugin_info
Definition: plugin.h:92
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
#define clib_warning(format, args...)
Definition: error.h:59
static u8 ** split_plugin_path(plugin_main_t *pm)
Definition: plugin.c:73
#define clib_error_report(e)
Definition: error.h:113
void * handle
Definition: plugin.h:74
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
int vat_plugin_init(vat_main_t *vam)
Definition: plugin.c:188
#define hash_get_mem(h, key)
Definition: hash.h:269
plugin_main_t vat_plugin_main
Definition: plugin.c:24
int vat_load_new_plugins(plugin_main_t *pm)
Definition: plugin.c:100
struct stat file_info
Definition: plugin.h:73
uword * plugin_by_name_hash
Definition: plugin.h:93