diff options
Diffstat (limited to 'lib/frrscript.c')
| -rw-r--r-- | lib/frrscript.c | 296 |
1 files changed, 232 insertions, 64 deletions
diff --git a/lib/frrscript.c b/lib/frrscript.c index 1a9f3639dd..ccb790dc4a 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -32,6 +32,92 @@ DEFINE_MTYPE_STATIC(LIB, SCRIPT, "Scripting"); +/* + * Script name hash utilities + */ + +struct frrscript_names_head frrscript_names_hash; + +/* + * Wrapper for frrscript_names_add + * Use this to register hook calls when a daemon starts up + */ +int frrscript_names_add_function_name(const char *function_name) +{ + struct frrscript_names_entry *insert = + XCALLOC(MTYPE_SCRIPT, sizeof(*insert)); + strlcpy(insert->function_name, function_name, + sizeof(insert->function_name)); + + if (frrscript_names_add(&frrscript_names_hash, insert)) { + zlog_warn( + "Failed to add hook call function name to script_names"); + return 1; + } + return 0; +} + +void frrscript_names_destroy(void) +{ + struct frrscript_names_entry *ne; + + while ((ne = frrscript_names_pop(&frrscript_names_hash))) + XFREE(MTYPE_SCRIPT, ne); +} + +/* + * Given a function_name, set its script_name. function_names and script_names + * are one-to-one. Each set will wipe the previous script_name. + * Return 0 if set was successful, else 1. + * + * script_name is the base name of the file, without .lua. + */ +int frrscript_names_set_script_name(const char *function_name, + const char *script_name) +{ + struct frrscript_names_entry lookup; + + strlcpy(lookup.function_name, function_name, + sizeof(lookup.function_name)); + struct frrscript_names_entry *snhe = + frrscript_names_find(&frrscript_names_hash, &lookup); + if (!snhe) + return 1; + strlcpy(snhe->script_name, script_name, sizeof(snhe->script_name)); + return 0; +} + +/* + * Given a function_name, get its script_name. + * Return NULL if function_name not found. + * + * script_name is the base name of the file, without .lua. + */ +char *frrscript_names_get_script_name(const char *function_name) +{ + struct frrscript_names_entry lookup; + + strlcpy(lookup.function_name, function_name, + sizeof(lookup.function_name)); + struct frrscript_names_entry *snhe = + frrscript_names_find(&frrscript_names_hash, &lookup); + if (!snhe) + return NULL; + return snhe->script_name; +} + +uint32_t frrscript_names_hash_key(const struct frrscript_names_entry *snhe) +{ + return string_hash_make(snhe->function_name); +} + +int frrscript_names_hash_cmp(const struct frrscript_names_entry *snhe1, + const struct frrscript_names_entry *snhe2) +{ + return strncmp(snhe1->function_name, snhe2->function_name, + sizeof(snhe1->function_name)); +} + /* Codecs */ struct frrscript_codec frrscript_codecs_lib[] = { @@ -102,67 +188,136 @@ static void codec_free(struct codec *c) } #endif -/* Generic script APIs */ +/* Lua function hash utils */ + +unsigned int lua_function_hash_key(const void *data) +{ + const struct lua_function_state *lfs = data; + + return string_hash_make(lfs->name); +} + +bool lua_function_hash_cmp(const void *d1, const void *d2) +{ + const struct lua_function_state *lfs1 = d1; + const struct lua_function_state *lfs2 = d2; + + return strmatch(lfs1->name, lfs2->name); +} + +void *lua_function_alloc(void *arg) +{ + struct lua_function_state *tmp = arg; + + struct lua_function_state *lfs = + XCALLOC(MTYPE_SCRIPT, sizeof(struct lua_function_state)); + lfs->name = tmp->name; + lfs->L = tmp->L; + return lfs; +} + +static void lua_function_free(struct hash_bucket *b, void *data) +{ + struct lua_function_state *lfs = (struct lua_function_state *)b->data; + lua_close(lfs->L); + XFREE(MTYPE_SCRIPT, lfs); +} + +/* internal frrscript APIs */ -int _frrscript_call(struct frrscript *fs) +int _frrscript_call_lua(struct lua_function_state *lfs, int nargs) { - int ret = lua_pcall(fs->L, 0, 0, 0); + int ret; + ret = lua_pcall(lfs->L, nargs, 1, 0); switch (ret) { case LUA_OK: break; case LUA_ERRRUN: - zlog_err("Script '%s' runtime error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : runtime error: %s", lfs->name, + lua_tostring(lfs->L, -1)); break; case LUA_ERRMEM: - zlog_err("Script '%s' memory error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : memory error: %s", lfs->name, + lua_tostring(lfs->L, -1)); break; case LUA_ERRERR: - zlog_err("Script '%s' error handler error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : error handler error: %s", + lfs->name, lua_tostring(lfs->L, -1)); break; case LUA_ERRGCMM: - zlog_err("Script '%s' garbage collector error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : garbage collector error: %s", + lfs->name, lua_tostring(lfs->L, -1)); break; default: - zlog_err("Script '%s' unknown error: %s", fs->name, - lua_tostring(fs->L, -1)); + zlog_err("Lua hook call '%s' : unknown error: %s", lfs->name, + lua_tostring(lfs->L, -1)); break; } if (ret != LUA_OK) { - lua_pop(fs->L, 1); + lua_pop(lfs->L, 1); goto done; } + if (lua_gettop(lfs->L) != 1) { + zlog_err( + "Lua hook call '%s': Lua function should return only 1 result", + lfs->name); + ret = 1; + goto done; + } + + if (lua_istable(lfs->L, 1) != 1) { + zlog_err( + "Lua hook call '%s': Lua function should return a Lua table", + lfs->name); + ret = 1; + } + done: /* LUA_OK is 0, so we can just return lua_pcall's result directly */ return ret; } -void *frrscript_get_result(struct frrscript *fs, - const struct frrscript_env *result) +void *frrscript_get_result(struct frrscript *fs, const char *function_name, + const char *name, + void *(*lua_to)(lua_State *L, int idx)) { - void *r; - struct frrscript_codec c = {.typename = result->typename}; + void *p; + struct lua_function_state *lfs; + struct lua_function_state lookup = {.name = function_name}; + + lfs = hash_lookup(fs->lua_function_hash, &lookup); - struct frrscript_codec *codec = hash_lookup(codec_hash, &c); - assert(codec && "No encoder for type"); + if (lfs == NULL) + return NULL; - if (!codec->decoder) { - zlog_err("No script decoder for type '%s'", result->typename); + /* At this point, the Lua state should have only the returned table. + * We will then search the table for the key/value we're interested in. + * Then if the value is present (i.e. non-nil), call the lua_to* + * decoder. + */ + assert(lua_gettop(lfs->L) == 1); + assert(lua_istable(lfs->L, -1) == 1); + lua_getfield(lfs->L, -1, name); + if (lua_isnil(lfs->L, -1)) { + lua_pop(lfs->L, 1); + zlog_warn( + "frrscript: '%s.lua': '%s': tried to decode '%s' as result but failed", + fs->name, function_name, name); return NULL; } + p = lua_to(lfs->L, 2); - lua_getglobal(fs->L, result->name); - r = codec->decoder(fs->L, -1); - lua_pop(fs->L, 1); + /* At the end, the Lua state should be same as it was at the start + * i.e. containing solely the returned table. + */ + assert(lua_gettop(lfs->L) == 1); + assert(lua_istable(lfs->L, -1) == 1); - return r; + return p; } void frrscript_register_type_codec(struct frrscript_codec *codec) @@ -183,61 +338,74 @@ void frrscript_register_type_codecs(struct frrscript_codec *codecs) frrscript_register_type_codec(&codecs[i]); } -struct frrscript *frrscript_load(const char *name, - int (*load_cb)(struct frrscript *)) +struct frrscript *frrscript_new(const char *name) { struct frrscript *fs = XCALLOC(MTYPE_SCRIPT, sizeof(struct frrscript)); fs->name = XSTRDUP(MTYPE_SCRIPT, name); - fs->L = luaL_newstate(); - frrlua_export_logging(fs->L); + fs->lua_function_hash = + hash_create(lua_function_hash_key, lua_function_hash_cmp, + "Lua function state hash"); + return fs; +} - char fname[MAXPATHLEN * 2]; - snprintf(fname, sizeof(fname), "%s/%s.lua", scriptdir, fs->name); +int frrscript_load(struct frrscript *fs, const char *function_name, + int (*load_cb)(struct frrscript *)) +{ - int ret = luaL_loadfile(fs->L, fname); + /* Set up the Lua script */ + lua_State *L = luaL_newstate(); - switch (ret) { - case LUA_OK: - break; - case LUA_ERRSYNTAX: - zlog_err("Failed loading script '%s': syntax error: %s", fname, - lua_tostring(fs->L, -1)); - break; - case LUA_ERRMEM: - zlog_err("Failed loading script '%s': out-of-memory error: %s", - fname, lua_tostring(fs->L, -1)); - break; - case LUA_ERRGCMM: - zlog_err( - "Failed loading script '%s': garbage collector error: %s", - fname, lua_tostring(fs->L, -1)); - break; - case LUA_ERRFILE: - zlog_err("Failed loading script '%s': file read error: %s", - fname, lua_tostring(fs->L, -1)); - break; - default: - zlog_err("Failed loading script '%s': unknown error: %s", fname, - lua_tostring(fs->L, -1)); - break; + frrlua_export_logging(L); + + char script_name[MAXPATHLEN]; + + if (snprintf(script_name, sizeof(script_name), "%s/%s.lua", scriptdir, + fs->name) + >= (int)sizeof(script_name)) { + zlog_err("frrscript: path to script %s/%s.lua is too long", + scriptdir, fs->name); + goto fail; } - if (ret != LUA_OK) + if (luaL_dofile(L, script_name) != 0) { + zlog_err("frrscript: failed loading script '%s.lua': error: %s", + script_name, lua_tostring(L, -1)); goto fail; + } - if (load_cb && (*load_cb)(fs) != 0) + /* To check the Lua function, we get it from the global table */ + lua_getglobal(L, function_name); + if (lua_isfunction(L, lua_gettop(L)) == 0) { + zlog_err("frrscript: loaded script '%s.lua' but %s not found", + script_name, function_name); goto fail; + } + /* Then pop the function (frrscript_call will push it when it needs it) + */ + lua_pop(L, 1); - return fs; + if (load_cb && (*load_cb)(fs) != 0) { + zlog_err( + "frrscript: '%s.lua': %s: loaded but callback returned non-zero exit code", + script_name, function_name); + goto fail; + } + + /* Add the Lua function state to frrscript */ + struct lua_function_state key = {.name = function_name, .L = L}; + + hash_get(fs->lua_function_hash, &key, lua_function_alloc); + + return 0; fail: - frrscript_unload(fs); - return NULL; + lua_close(L); + return 1; } -void frrscript_unload(struct frrscript *fs) +void frrscript_delete(struct frrscript *fs) { - lua_close(fs->L); + hash_iterate(fs->lua_function_hash, lua_function_free, NULL); XFREE(MTYPE_SCRIPT, fs->name); XFREE(MTYPE_SCRIPT, fs); } |
