FD.io VPP  v16.06
Vector Packet Processing
plugin.c
Go to the documentation of this file.
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 
23 
25 {
27  pm->handoff_structure_get_cb = cb;
28 }
29 
30 static void * vnet_get_handoff_structure (void)
31 {
32  void * (*fp)(void);
33 
34  fp = vlib_plugin_main.handoff_structure_get_cb;
35  if (fp == 0)
36  return 0;
37  else
38  return (*fp)();
39 }
40 
41 static int
42 load_one_plugin (plugin_main_t *pm, plugin_info_t *pi, int from_early_init)
43 {
44  void *handle, *register_handle;
45  clib_error_t * (*fp)(vlib_main_t *, void *, int);
46  clib_error_t * error;
47  void *handoff_structure;
48 
49  handle = dlopen ((char *)pi->name, RTLD_LAZY);
50 
51  /*
52  * Note: this can happen if the plugin has an undefined symbol reference,
53  * so print a warning. Otherwise, the poor slob won't know what happened.
54  * Ask me how I know that...
55  */
56  if (handle == 0)
57  {
58  clib_warning ("%s", dlerror());
59  return -1;
60  }
61 
62  pi->handle = handle;
63 
64  register_handle = dlsym (pi->handle, "vlib_plugin_register");
65  if (register_handle == 0)
66  {
67  dlclose (handle);
68  return 0;
69  }
70 
71  fp = register_handle;
72 
73  handoff_structure = vnet_get_handoff_structure();
74 
75  if (handoff_structure == 0)
76  error = clib_error_return (0, "handoff structure callback returned 0");
77  else
78  error = (*fp)(pm->vlib_main, handoff_structure, from_early_init);
79 
80  if (error)
81  {
82  clib_error_report (error);
83  dlclose (handle);
84  return 1;
85  }
86 
87  clib_warning ("Loaded plugin: %s", pi->name);
88 
89  return 0;
90 }
91 
93 {
94  int i;
95  u8 **rv = 0;
96  u8 *path = pm->plugin_path;
97  u8 *this = 0;
98 
99  for (i = 0; i < vec_len (pm->plugin_path); i++)
100  {
101  if (path[i] != ':')
102  {
103  vec_add1(this, path[i]);
104  continue;
105  }
106  vec_add1(this, 0);
107  vec_add1 (rv, this);
108  this = 0;
109  }
110  if (this)
111  {
112  vec_add1 (this, 0);
113  vec_add1 (rv, this);
114  }
115  return rv;
116 }
117 
118 int vlib_load_new_plugins (plugin_main_t *pm, int from_early_init)
119 {
120  DIR *dp;
121  struct dirent *entry;
122  struct stat statb;
123  uword *p;
124  plugin_info_t *pi;
125  u8 **plugin_path;
126  int i;
127 
128  plugin_path = split_plugin_path (pm);
129 
130  for (i = 0; i < vec_len (plugin_path); i++)
131  {
132  dp = opendir ((char *)plugin_path[i]);
133 
134  if (dp == 0)
135  continue;
136 
137  while ((entry = readdir (dp)))
138  {
139  u8 *plugin_name;
140 
141  if (pm->plugin_name_filter)
142  {
143  int j;
144  for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
145  if (entry->d_name[j] != pm->plugin_name_filter[j])
146  goto next;
147  }
148 
149  plugin_name = format (0, "%s/%s%c", plugin_path[i],
150  entry->d_name, 0);
151 
152  /* unreadable */
153  if (stat ((char *)plugin_name, &statb) < 0)
154  {
155  ignore:
156  vec_free (plugin_name);
157  continue;
158  }
159 
160  /* a dir or other things which aren't plugins */
161  if (!S_ISREG(statb.st_mode))
162  goto ignore;
163 
164  p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
165  if (p == 0)
166  {
167  vec_add2 (pm->plugin_info, pi, 1);
168  pi->name = plugin_name;
169  pi->file_info = statb;
170 
171  if (load_one_plugin (pm, pi, from_early_init))
172  {
173  vec_free (plugin_name);
174  _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1;
175  continue;
176  }
177  memset (pi, 0, sizeof (*pi));
178  hash_set_mem (pm->plugin_by_name_hash, plugin_name,
179  pi - pm->plugin_info);
180  }
181  next:
182  ;
183  }
184  closedir (dp);
185  vec_free (plugin_path[i]);
186  }
187  vec_free (plugin_path);
188  return 0;
189 }
190 char *vlib_plugin_path __attribute__((weak));
191 char *vlib_plugin_path = "";
192 char *vlib_plugin_name_filter __attribute__((weak));
193 char *vlib_plugin_name_filter = 0;
194 
196 {
198 
199  pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
200 
201  clib_warning ("plugin path %s", pm->plugin_path);
202 
203  if (vlib_plugin_name_filter)
204  pm->plugin_name_filter = format (0, "%s%c", vlib_plugin_name_filter, 0);
205 
206  pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
207  pm->vlib_main = vm;
208 
209  return vlib_load_new_plugins (pm, 1 /* from_early_init */);
210 }
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
static void(BVT(clib_bihash)*h, BVT(clib_bihash_value)*v)
int vlib_load_new_plugins(plugin_main_t *pm, int from_early_init)
Definition: plugin.c:118
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
vlib_main_t * vlib_main
Definition: plugin.h:80
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:519
#define hash_set_mem(h, key, value)
Definition: hash.h:257
#define clib_error_report(e)
Definition: error.h:126
u8 * name
Definition: plugin.h:62
static void * vnet_get_handoff_structure(void)
Definition: plugin.c:30
u8 * plugin_path
Definition: plugin.h:73
#define clib_warning(format, args...)
Definition: error.h:59
void vlib_set_get_handoff_structure_cb(void *cb)
Definition: plugin.c:24
#define hash_create_string(elts, value_bytes)
Definition: hash.h:609
static u8 ** split_plugin_path(plugin_main_t *pm)
Definition: plugin.c:92
plugin_main_t vlib_plugin_main
Definition: plugin.c:22
int vlib_plugin_early_init(vlib_main_t *vm)
Definition: plugin.c:195
uword * plugin_by_name_hash
Definition: plugin.h:70
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
static int load_one_plugin(plugin_main_t *pm, plugin_info_t *pi, int from_early_init)
Definition: plugin.c:42
void * handoff_structure_get_cb
Definition: plugin.h:77
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
u64 uword
Definition: types.h:112
plugin_info_t * plugin_info
Definition: plugin.h:69
u8 * plugin_name_filter
Definition: plugin.h:74
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
#define hash_get_mem(h, key)
Definition: hash.h:251
void * handle
Definition: plugin.h:64
struct stat file_info
Definition: plugin.h:63
#define clib_error_return(e, args...)
Definition: error.h:112