FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
plugin.h
Go to the documentation of this file.
1 /*
2  * plugin.h: 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 #ifndef __included_plugin_h__
19 #define __included_plugin_h__
20 
21 #include <vlib/vlib.h>
22 #include <vlib/unix/unix.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 
27 /*
28  * vlib plugin scheme
29  *
30  * Almost anything which can be made to work in a vlib unix
31  * application will also work in a vlib plugin.
32  *
33  * The elf-section magic which registers static objects
34  * works so long as plugins are preset when the vlib unix process
35  * starts. But wait: there's more...
36  *
37  * If an application calls vlib_load_new_plugins() -- possibly after
38  * changing vlib_plugin_main.plugin_path / vlib_plugin_main.plugin_name_filter,
39  * -- new plugins will be loaded. That, in turn, allows considerable
40  * flexibility in terms of adding feature code or fixing bugs without
41  * requiring the data-plane process to restart.
42  *
43  * When the plugin mechanism loads a plugin, it uses dlsym to locate
44  * and call the plugin's function vlib_plugin_register() if it exists.
45  * A plugin which expects to be loaded after the vlib application
46  * starts uses this callback to modify the application. If vlib_plugin_register
47  * returns non-zero, the plugin mechanism dlclose()'s the plugin.
48  *
49  * Applications control the plugin search path and name filter by
50  * declaring the variables vlib_plugin_path and vlib_plugin_name_filter.
51  * libvlib.la supplies weak references for these symbols which
52  * effectively disable the scheme. In order for the elf-section magic to
53  * work, static plugins must be loaded at the earliest possible moment.
54  *
55  * An application can change these parameters at any time and call
56  * vlib_load_new_plugins().
57  */
58 
59 /* *INDENT-OFF* */
60 typedef CLIB_PACKED(struct {
61  u8 default_disabled;
62  const char version[32];
63  const char version_required[32];
64  const char *early_init;
65  const char *description;
66 }) vlib_plugin_registration_t;
67 /* *INDENT-ON* */
68 
69 typedef struct
70 {
71  u8 *name;
73  struct stat file_info;
74  void *handle;
75 
76  /* plugin registration */
77  vlib_plugin_registration_t *reg;
78  char *version;
80 
81 typedef struct
82 {
83  char *name;
88 
89 typedef struct
90 {
91  /* loaded plugin info */
94 
95  /* paths and name filters */
101 
102  /* plugin configs and hash by name */
105 
106  /* usual */
108 } plugin_main_t;
109 
111 
114 int vlib_load_new_plugins (plugin_main_t * pm, int from_early_init);
115 void *vlib_get_plugin_symbol (char *plugin_name, char *symbol_name);
117 
118 #define VLIB_PLUGIN_REGISTER() \
119  vlib_plugin_registration_t vlib_plugin_registration \
120  __attribute__((__section__(".vlib_plugin_registration")))
121 
122 /* Call a plugin init function: used for init function dependencies. */
123 #define vlib_call_plugin_init_function(vm,p,x) \
124 ({ \
125  clib_error_t *(*_f)(vlib_main_t *); \
126  uword *_fptr = 0; \
127  clib_error_t * _error = 0; \
128  _fptr= vlib_get_plugin_symbol \
129  (p, CLIB_STRING_MACRO(_vlib_init_function_##x)); \
130  if (_fptr == 0) \
131  { \
132  _error = clib_error_return \
133  (0, "Plugin %s and/or symbol %s not found.", \
134  p, CLIB_STRING_MACRO(_vlib_init_function_##x)); \
135  } \
136  else \
137  { \
138  _f = (void *)(_fptr[0]); \
139  } \
140  if (_fptr && ! hash_get (vm->init_functions_called, _f)) \
141  { \
142  hash_set1 (vm->init_functions_called, _f); \
143  _error = _f (vm); \
144  } \
145  _error; \
146  })
147 
148 #endif /* __included_plugin_h__ */
149 
150 /*
151  * fd.io coding-style-patch-verification: ON
152  *
153  * Local Variables:
154  * eval: (c-set-style "gnu")
155  * End:
156  */
vlib_main_t * vlib_main
Definition: plugin.h:107
u8 * filename
Definition: plugin.h:72
u8 * plugin_path
Definition: plugin.h:96
u8 * plugin_name_filter
Definition: plugin.h:97
char * name
Definition: plugin.h:83
u8 * vat_plugin_name_filter
Definition: plugin.h:99
plugin_main_t vlib_plugin_main
Definition: plugin.c:23
clib_error_t * vlib_plugin_config(vlib_main_t *vm, unformat_input_t *input)
Definition: plugin.c:481
unsigned char u8
Definition: types.h:56
uword * config_index_by_name
Definition: plugin.h:104
u8 * vlib_get_vat_plugin_path(void)
Definition: plugin.c:370
vlib_plugin_registration_t * reg
Definition: plugin.h:77
u8 is_disabled
Definition: plugin.h:84
int vlib_plugin_early_init(vlib_main_t *vm)
Definition: plugin.c:354
u8 * name
Definition: plugin.h:71
char * version
Definition: plugin.h:78
struct _unformat_input_t unformat_input_t
int vlib_load_new_plugins(plugin_main_t *pm, int from_early_init)
Definition: plugin.c:237
plugin_info_t * plugin_info
Definition: plugin.h:92
vlib_main_t * vm
Definition: buffer.c:323
typedef CLIB_PACKED(struct { u8 default_disabled;const char version[32];const char version_required[32];const char *early_init;const char *description;}) vlib_plugin_registration_t
u8 * vat_plugin_path
Definition: plugin.h:98
void * vlib_get_plugin_symbol(char *plugin_name, char *symbol_name)
Definition: plugin.c:31
option version
Definition: memclnt.api:17
void * handle
Definition: plugin.h:74
u64 uword
Definition: types.h:112
plugin_config_t * configs
Definition: plugin.h:103
u8 skip_version_check
Definition: plugin.h:86
uword * plugin_by_name_hash
Definition: plugin.h:93
u8 plugins_default_disable
Definition: plugin.h:100