vat2: add plugin-path parameter

Add plugin-path parameter to aid external plugin development.
Multiple directories are supported as a colon separated list.

Type: improvement
Signed-off-by: Ole Troan <ot@cisco.com>
Change-Id: Ida35dedceccd0019ea68e56f7a3672c530258447
This commit is contained in:
Ole Troan
2022-12-06 14:41:41 +01:00
committed by Andrew Yourtchenko
parent bcdde1a0a1
commit fe1fb3c31a
3 changed files with 101 additions and 68 deletions

View File

@ -30,6 +30,8 @@
#include <limits.h> #include <limits.h>
#include "vat2.h" #include "vat2.h"
bool vat2_debug;
/* /*
* Filter these messages as they are used to manage the API connection to VPP * Filter these messages as they are used to manage the API connection to VPP
*/ */
@ -60,9 +62,8 @@ filter_message (char *msgname)
uword *function_by_name; uword *function_by_name;
bool debug = false; bool debug = false;
char *vat2_plugin_path; static u8 *
static void vat2_find_plugin_path (void)
vat2_find_plugin_path ()
{ {
char *p, path[PATH_MAX]; char *p, path[PATH_MAX];
int rv; int rv;
@ -70,44 +71,56 @@ vat2_find_plugin_path ()
/* find executable path */ /* find executable path */
if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1) if ((rv = readlink ("/proc/self/exe", path, PATH_MAX - 1)) == -1)
return; return 0;
/* readlink doesn't provide null termination */ /* readlink doesn't provide null termination */
path[rv] = 0; path[rv] = 0;
/* strip filename */ /* strip filename */
if ((p = strrchr (path, '/')) == 0) if ((p = strrchr (path, '/')) == 0)
return; return 0;
*p = 0; *p = 0;
/* strip bin/ */ /* strip bin/ */
if ((p = strrchr (path, '/')) == 0) if ((p = strrchr (path, '/')) == 0)
return; return 0;
*p = 0; *p = 0;
s = format (0, "%s/" CLIB_LIB_DIR "/vat2_plugins", path, path); s = format (0, "%s/" CLIB_LIB_DIR "/vat2_plugins", path, path);
vec_add1 (s, 0); vec_add1 (s, 0);
vat2_plugin_path = (char *) s; return s;
} }
void void
vac_callback (unsigned char *data, int len) vac_callback (unsigned char *data, int len)
{ {
u16 result_msg_id = ntohs(*((u16 *)data)); u16 result_msg_id = ntohs (*((u16 *) data));
DBG("Received something async: %d\n", result_msg_id); DBG ("Received something async: %d\n", result_msg_id);
} }
int vat2_load_plugins (char *path, char *filter, int *loaded); int vat2_load_plugins (u8 *path, char *filter, int *loaded);
static int static int
register_function (void) register_function (char *pluginpath)
{ {
int loaded; int loaded;
u8 *vat2_plugin_path = 0;
if (pluginpath == 0)
{
vat2_plugin_path = vat2_find_plugin_path ();
}
else
{
vat2_plugin_path = format (0, "%s", pluginpath);
vec_add1 (vat2_plugin_path, 0);
}
DBG ("Plugin Path %s\n", vat2_plugin_path);
int rv = vat2_load_plugins (vat2_plugin_path, 0, &loaded);
DBG ("Loaded %u plugins\n", loaded);
vec_free (vat2_plugin_path);
vat2_find_plugin_path();
DBG("Plugin Path %s\n", vat2_plugin_path);
int rv = vat2_load_plugins(vat2_plugin_path, 0, &loaded);
DBG("Loaded %u plugins\n", loaded);
return rv; return rv;
} }
@ -248,29 +261,31 @@ print_help (void)
" message\n" " message\n"
"--dump-apis List all APIs available in VAT2 (might " "--dump-apis List all APIs available in VAT2 (might "
"not reflect running VPP)\n" "not reflect running VPP)\n"
"--plugin-path Pluing path"
"\n"; "\n";
printf ("%s", help_string); printf ("%s", help_string);
} }
int main (int argc, char **argv) int
main (int argc, char **argv)
{ {
/* Create a heap of 64MB */ /* Create a heap of 64MB */
clib_mem_init (0, 64 << 20); clib_mem_init (0, 64 << 20);
char *filename = 0, *prefix = 0, *template = 0; char *filename = 0, *prefix = 0, *template = 0, *pluginpath = 0;
int index; int index;
int c; int c;
opterr = 0; opterr = 0;
cJSON *o = 0; cJSON *o = 0;
int option_index = 0; int option_index = 0;
bool dump_api = false; bool dump_api = false;
bool debug = false;
char *msgname = 0; char *msgname = 0;
static struct option long_options[] = { static struct option long_options[] = {
{ "debug", no_argument, 0, 'd' }, { "debug", no_argument, 0, 'd' },
{ "prefix", required_argument, 0, 'p' }, { "prefix", required_argument, 0, 's' },
{ "file", required_argument, 0, 'f' }, { "file", required_argument, 0, 'f' },
{ "dump-apis", no_argument, 0, 0 }, { "dump-apis", no_argument, 0, 0 },
{ "template", required_argument, 0, 't' }, { "template", required_argument, 0, 't' },
{ "plugin-path", required_argument, 0, 'p' },
{ 0, 0, 0, 0 } { 0, 0, 0, 0 }
}; };
@ -284,17 +299,20 @@ int main (int argc, char **argv)
dump_api = true; dump_api = true;
break; break;
case 'd': case 'd':
debug = true; vat2_debug = true;
break; break;
case 't': case 't':
template = optarg; template = optarg;
break; break;
case 'p': case 's':
prefix = optarg; prefix = optarg;
break; break;
case 'f': case 'f':
filename = optarg; filename = optarg;
break; break;
case 'p':
pluginpath = optarg;
break;
case '?': case '?':
print_help (); print_help ();
return 1; return 1;
@ -303,7 +321,7 @@ int main (int argc, char **argv)
} }
} }
DBG ("debug = %d, filename = %s, template = %s, shared memory prefix: %s\n", DBG ("debug = %d, filename = %s, template = %s, shared memory prefix: %s\n",
debug, filename, template, prefix); vat2_debug, filename, template, prefix);
for (index = optind; index < argc; index++) for (index = optind; index < argc; index++)
DBG ("Non-option argument %s\n", argv[index]); DBG ("Non-option argument %s\n", argv[index]);
@ -318,11 +336,12 @@ int main (int argc, char **argv)
/* Load plugins */ /* Load plugins */
function_by_name = hash_create_string (0, sizeof (uword)); function_by_name = hash_create_string (0, sizeof (uword));
int res = register_function(); int res = register_function (pluginpath);
if (res < 0) { if (res < 0)
fprintf(stderr, "%s: loading plugins failed\n", argv[0]); {
exit(-1); fprintf (stderr, "%s: loading plugins failed\n", argv[0]);
} exit (-1);
}
if (template) if (template)
{ {
@ -341,13 +360,16 @@ int main (int argc, char **argv)
{ {
msgname = argv[index]; msgname = argv[index];
} }
if (argc == (index + 2)) { if (argc == (index + 2))
o = cJSON_Parse(argv[index+1]); {
if (!o) { o = cJSON_Parse (argv[index + 1]);
fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr()); if (!o)
exit(-1); {
fprintf (stderr, "%s: Failed parsing JSON input: %s\n", argv[0],
cJSON_GetErrorPtr ());
exit (-1);
}
} }
}
if (!msgname && !filename) if (!msgname && !filename)
{ {
@ -356,7 +378,8 @@ int main (int argc, char **argv)
} }
/* Read message from file */ /* Read message from file */
if (filename) { if (filename)
{
if (argc > index) if (argc > index)
{ {
fprintf (stderr, "%s: Superfluous arguments when filename given\n", fprintf (stderr, "%s: Superfluous arguments when filename given\n",
@ -364,37 +387,41 @@ int main (int argc, char **argv)
exit (-1); exit (-1);
} }
FILE *f = fopen(filename, "r"); FILE *f = fopen (filename, "r");
size_t chunksize, bufsize; size_t chunksize, bufsize;
size_t n_read = 0; size_t n_read = 0;
size_t n; size_t n;
if (!f) { if (!f)
fprintf(stderr, "%s: can't open file: %s\n", argv[0], filename); {
exit(-1); fprintf (stderr, "%s: can't open file: %s\n", argv[0], filename);
} exit (-1);
}
chunksize = bufsize = 1024; chunksize = bufsize = 1024;
char *buf = malloc(bufsize); char *buf = malloc (bufsize);
while ((n = fread (buf + n_read, 1, chunksize, f))) while ((n = fread (buf + n_read, 1, chunksize, f)))
{ {
n_read += n; n_read += n;
if (n == chunksize) if (n == chunksize)
{ {
bufsize += chunksize; bufsize += chunksize;
buf = realloc (buf, bufsize); buf = realloc (buf, bufsize);
} }
} }
fclose(f); fclose (f);
if (n_read) { if (n_read)
o = cJSON_Parse(buf); {
if (!o) { o = cJSON_Parse (buf);
fprintf(stderr, "%s: Failed parsing JSON input: %s\n", argv[0], cJSON_GetErrorPtr()); if (!o)
exit(-1); {
} fprintf (stderr, "%s: Failed parsing JSON input: %s\n", argv[0],
cJSON_GetErrorPtr ());
exit (-1);
}
}
free (buf);
} }
free (buf);
}
if (!o) if (!o)
{ {
@ -422,7 +449,6 @@ int main (int argc, char **argv)
} }
} }
cJSON_Delete (o); cJSON_Delete (o);
vac_disconnect(); vac_disconnect ();
exit (0); exit (0);
} }

View File

@ -76,8 +76,9 @@ load_one_plugin (plugin_info_t * pi)
return 0; return 0;
} }
/* Takes a vector as argument */
static u8 ** static u8 **
split_plugin_path (char *plugin_path) split_plugin_path (u8 *plugin_path)
{ {
int i; int i;
u8 **rv = 0; u8 **rv = 0;
@ -104,7 +105,7 @@ split_plugin_path (char *plugin_path)
} }
int int
vat2_load_plugins (char *path, char *filter, int *loaded) vat2_load_plugins (u8 *path, char *filter, int *loaded)
{ {
DIR *dp; DIR *dp;
struct dirent *entry; struct dirent *entry;

View File

@ -3,9 +3,15 @@
#include <stdbool.h> #include <stdbool.h>
extern bool debug; extern bool vat2_debug;
#define DBG(fmt, args...) do {if (debug) fprintf(stderr, fmt, ## args); } while(0) #define DBG(fmt, args...) \
do \
{ \
if (vat2_debug) \
fprintf (stderr, fmt, ##args); \
} \
while (0)
#define ERR(fmt, args...) fprintf(stderr, "VAT2: %s:%d:%s(): " fmt, \ #define ERR(fmt, args...) fprintf(stderr, "VAT2: %s:%d:%s(): " fmt, \
__FILE__, __LINE__, __func__, ##args) __FILE__, __LINE__, __func__, ##args)