6rd: Move to plugin
[vpp.git] / vlib / vlib / unix / plugin.c
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
24 void vlib_set_get_handoff_structure_cb (void *cb)
25 {
26   plugin_main_t * pm = &vlib_plugin_main;
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
65   register_handle = dlsym (pi->handle, "vlib_plugin_register");
66   if (register_handle == 0)
67     {
68       dlclose (handle);
69       clib_warning("Plugin missing vlib_plugin_register: %s\n", (char *)pi->name);
70       return 1;
71     }
72
73   fp = register_handle;
74
75   handoff_structure = vnet_get_handoff_structure();
76
77   if (handoff_structure == 0)
78     error = clib_error_return (0, "handoff structure callback returned 0");
79   else
80     error = (*fp)(pm->vlib_main, handoff_structure, from_early_init);
81
82   if (error)
83     {
84       clib_error_report (error);
85       dlclose (handle);
86       return 1;
87     }
88
89   clib_warning ("Loaded plugin: %s", pi->name);
90
91   return 0;
92 }
93
94 static u8 **split_plugin_path (plugin_main_t *pm)
95 {
96   int i;
97   u8 **rv = 0;
98   u8 *path = pm->plugin_path;
99   u8 *this = 0;
100
101   for (i = 0; i < vec_len (pm->plugin_path); i++)
102     {
103       if (path[i] != ':')
104         {
105           vec_add1(this, path[i]);
106           continue;
107         }
108       vec_add1(this, 0);
109       vec_add1 (rv, this);
110       this = 0;
111     }
112   if (this)
113     {
114       vec_add1 (this, 0);
115       vec_add1 (rv, this);
116     }
117   return rv;
118 }
119
120 int vlib_load_new_plugins (plugin_main_t *pm, int from_early_init)
121 {
122   DIR *dp;
123   struct dirent *entry;
124   struct stat statb;
125   uword *p;
126   plugin_info_t *pi;
127   u8 **plugin_path;
128   int i;
129
130   plugin_path = split_plugin_path (pm);
131   
132   for (i = 0; i < vec_len (plugin_path); i++)
133     {
134       dp = opendir ((char *)plugin_path[i]);
135   
136       if (dp == 0)
137         continue;
138       
139       while ((entry = readdir (dp)))
140         {
141           u8 *plugin_name;
142           
143           if (pm->plugin_name_filter)
144             {
145               int j;
146               for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
147                 if (entry->d_name[j] != pm->plugin_name_filter[j])
148                   goto next;
149             }
150
151           plugin_name = format (0, "%s/%s%c", plugin_path[i],
152                                 entry->d_name, 0);
153
154           /* Only accept .so */
155           char * ext = strrchr((const char *)plugin_name, '.');
156           /* unreadable */
157           if(!ext || (strcmp(ext, ".so") != 0) ||
158              stat ((char *)plugin_name, &statb) < 0)
159             {
160             ignore:
161               vec_free (plugin_name);
162               continue;
163             }
164           
165           /* a dir or other things which aren't plugins */
166           if (!S_ISREG(statb.st_mode))
167             goto ignore;
168           
169           p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
170           if (p == 0) 
171             {
172               vec_add2 (pm->plugin_info, pi, 1);
173               pi->name = plugin_name;
174               pi->file_info = statb;
175               
176               if (load_one_plugin (pm, pi, from_early_init))
177                 {
178                   vec_free (plugin_name);
179                   _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1;
180                   continue;
181                 }
182               memset (pi, 0, sizeof (*pi));
183               hash_set_mem (pm->plugin_by_name_hash, plugin_name, 
184                             pi - pm->plugin_info);
185             }
186         next:
187           ;
188         }
189       closedir (dp);
190       vec_free (plugin_path[i]);
191     }
192   vec_free (plugin_path);
193   return 0;
194 }
195 char *vlib_plugin_path __attribute__((weak));
196 char *vlib_plugin_path = "";
197 char *vlib_plugin_name_filter __attribute__((weak));
198 char *vlib_plugin_name_filter = 0;
199
200 int vlib_plugin_early_init (vlib_main_t *vm)
201 {
202   plugin_main_t *pm = &vlib_plugin_main;
203
204   pm->plugin_path = format (0, "%s%c", vlib_plugin_path, 0);
205
206   clib_warning ("plugin path %s", pm->plugin_path);
207
208   if (vlib_plugin_name_filter)
209     pm->plugin_name_filter = format (0, "%s%c", vlib_plugin_name_filter, 0);
210
211   pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
212   pm->vlib_main = vm;
213   
214   return vlib_load_new_plugins (pm, 1 /* from_early_init */);
215 }