Call a plugin init function by name 80/9480/2
authorDave Barach <dave@barachs.net>
Sat, 18 Nov 2017 13:43:06 +0000 (08:43 -0500)
committerFlorin Coras <florin.coras@gmail.com>
Sat, 18 Nov 2017 17:04:34 +0000 (17:04 +0000)
Use this macro to arrange init function ordering between friend
plugins.  Fails in the usual manner if the plugin doesn't exist, or if
the init function symbol is AWOL.

clib_error_t *
thisplug_init (vlib_main_t *vm)
{
  clib_error_t *error = 0;

  if ((error = vlib_plugin_init_function ("otherplug.so", otherplug_init)))
     return error;

  <etc>
  return error;
}
VLIB_INIT_FUNCTION(thisplug_init);

Change-Id: Ideecaf46bc0b1546e85096e54be8ddef87946565
Signed-off-by: Dave Barach <dave@barachs.net>
src/vlib/unix/plugin.h
src/vppinfra/clib.h

index d9801ec..52da743 100644 (file)
@@ -115,6 +115,32 @@ void *vlib_get_plugin_symbol (char *plugin_name, char *symbol_name);
   vlib_plugin_registration_t vlib_plugin_registration \
   __attribute__((__section__(".vlib_plugin_registration")))
 
+/* Call a plugin init function: used for init function dependencies. */
+#define vlib_call_plugin_init_function(vm,p,x)                  \
+({                                                              \
+  clib_error_t *(*_f)(vlib_main_t *);                           \
+  uword *_fptr = 0;                                             \
+  clib_error_t * _error = 0;                                    \
+  _fptr= vlib_get_plugin_symbol                                 \
+    (p, CLIB_STRING_MACRO(_vlib_init_function_##x));            \
+  if (_fptr == 0)                                               \
+    {                                                           \
+      _error = clib_error_return                                \
+        (0, "Plugin %s and/or symbol %s not found.",            \
+         p, CLIB_STRING_MACRO(_vlib_init_function_##x));        \
+    }                                                           \
+  else                                                          \
+    {                                                           \
+      _f = (void *)(_fptr[0]);                                  \
+    }                                                           \
+  if (_fptr && ! hash_get (vm->init_functions_called, _f))      \
+    {                                                           \
+      hash_set1 (vm->init_functions_called, _f);                \
+      _error = _f (vm);                                         \
+    }                                                           \
+  _error;                                                       \
+ })
+
 #endif /* __included_plugin_h__ */
 
 /*
index 33db3b2..0d059a0 100644 (file)
@@ -78,6 +78,9 @@
 #define CLIB_PACKED(x) x __attribute__ ((packed))
 #define CLIB_UNUSED(x) x __attribute__ ((unused))
 
+/* Make a string from the macro's argument */
+#define CLIB_STRING_MACRO(x) #x
+
 #define __clib_unused __attribute__ ((unused))
 #define __clib_weak __attribute__ ((weak))
 #define __clib_packed __attribute__ ((packed))