From 6021965926654eb96c22391725ae0133ea77e784 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sat, 11 May 2019 00:55:50 +0000 Subject: [PATCH 01/35] lib: clean up frrlua.[ch] * Use frrlua_* prefix to differentiate from Lua builtins * Allow frrlua_initialize to pass an empty script * Fixup naming of table accessors * Fixup naming of prefix -> table encoder * Fixup BGP routemap code to new function names * Fix includes for frrlua.h * Clean up doc comments Signed-off-by: Quentin Young --- bgpd/bgp_routemap.c | 14 ++--- lib/frrlua.c | 135 +++++++++++++++++++++++++++----------------- lib/frrlua.h | 89 ++++++++++++++++------------- 3 files changed, 141 insertions(+), 97 deletions(-) diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 637eaca397..d9457ea616 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -344,9 +344,9 @@ route_match_command(void *rule, const struct prefix *prefix, void *object) int status = RMAP_NOMATCH; u_int32_t locpref = 0; u_int32_t newlocpref = 0; - enum lua_rm_status lrm_status; + enum frrlua_rm_status lrm_status; struct bgp_path_info *path = (struct bgp_path_info *)object; - lua_State *L = lua_initialize("/etc/frr/lua.scr"); + lua_State *L = frrlua_initialize("/etc/frr/lua.scr"); if (L == NULL) return status; @@ -354,9 +354,7 @@ route_match_command(void *rule, const struct prefix *prefix, void *object) /* * Setup the prefix information to pass in */ - lua_setup_prefix_table(L, prefix); - - zlog_debug("Set up prefix table"); + frrlua_newtable_prefix(L, prefix); /* * Setup the bgp_path_info information */ @@ -376,7 +374,7 @@ route_match_command(void *rule, const struct prefix *prefix, void *object) /* * Run the rule */ - lrm_status = lua_run_rm_rule(L, rule); + lrm_status = frrlua_run_rm_rule(L, rule); switch (lrm_status) { case LUA_RM_FAILURE: zlog_debug("RM_FAILURE"); @@ -387,13 +385,13 @@ route_match_command(void *rule, const struct prefix *prefix, void *object) case LUA_RM_MATCH_AND_CHANGE: zlog_debug("MATCH AND CHANGE"); lua_getglobal(L, "nexthop"); - path->attr->med = get_integer(L, "metric"); + path->attr->med = frrlua_table_get_integer(L, "metric"); /* * This needs to be abstraced with the set function */ if (path->attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) locpref = path->attr->local_pref; - newlocpref = get_integer(L, "localpref"); + newlocpref = frrlua_table_get_integer(L, "localpref"); if (newlocpref != locpref) { path->attr->flag |= ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF); path->attr->local_pref = newlocpref; diff --git a/lib/frrlua.c b/lib/frrlua.c index 9f9cf8c1f6..0c6d607b7a 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -2,25 +2,23 @@ * This file defines the lua interface into * FRRouting. * - * Copyright (C) 2016 Cumulus Networks, Inc. - * Donald Sharp + * Copyright (C) 2016-2019 Cumulus Networks, Inc. + * Donald Sharp, Quentin Young * - * This file is part of FRRouting (FRR). + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. * - * FRR is free software; you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation; either version 2, or (at your option) any later version. - * - * FRR is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. * * You should have received a copy of the GNU General Public License along - * with FRR; see the file COPYING. If not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * with this program; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ - #include #if defined(HAVE_LUA) @@ -28,6 +26,12 @@ #include "frrlua.h" #include "log.h" +/* + * Lua -> FRR function bindings. + * + * This section defines functions exportable into Lua environments. + */ + static int lua_zlog_debug(lua_State *L) { int debug_lua = 1; @@ -39,7 +43,14 @@ static int lua_zlog_debug(lua_State *L) return 0; } -const char *get_string(lua_State *L, const char *key) +/* + * FRR convenience functions. + * + * This section has convenience functions used to make interacting with the Lua + * stack easier. + */ + +const char *frrlua_table_get_string(lua_State *L, const char *key) { const char *str; @@ -52,7 +63,7 @@ const char *get_string(lua_State *L, const char *key) return str; } -int get_integer(lua_State *L, const char *key) +int frrlua_table_get_integer(lua_State *L, const char *key) { int result; @@ -65,44 +76,19 @@ int get_integer(lua_State *L, const char *key) return result; } -static void *lua_alloc(void *ud, void *ptr, size_t osize, - size_t nsize) -{ - (void)ud; (void)osize; /* not used */ - if (nsize == 0) { - free(ptr); - return NULL; - } else - return realloc(ptr, nsize); -} +/* + * Encoders. + * + * This section has functions that convert internal FRR datatypes into Lua + * datatypes. + */ -lua_State *lua_initialize(const char *file) -{ - int status; - lua_State *L = lua_newstate(lua_alloc, NULL); - - zlog_debug("Newstate: %p", L); - luaL_openlibs(L); - zlog_debug("Opened lib"); - status = luaL_loadfile(L, file); - if (status) { - zlog_debug("Failure to open %s %d", file, status); - lua_close(L); - return NULL; - } - - lua_pcall(L, 0, LUA_MULTRET, 0); - zlog_debug("Setting global function"); - lua_pushcfunction(L, lua_zlog_debug); - lua_setglobal(L, "zlog_debug"); - - return L; -} - -void lua_setup_prefix_table(lua_State *L, const struct prefix *prefix) +void frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix) { char buffer[100]; + zlog_debug("frrlua: pushing prefix table"); + lua_newtable(L); lua_pushstring(L, prefix2str(prefix, buffer, 100)); lua_setfield(L, -2, "route"); @@ -111,7 +97,14 @@ void lua_setup_prefix_table(lua_State *L, const struct prefix *prefix) lua_setglobal(L, "prefix"); } -enum lua_rm_status lua_run_rm_rule(lua_State *L, const char *rule) +/* + * Experimental. + * + * This section has experimental Lua functionality that doesn't belong + * elsewhere. + */ + +enum frrlua_rm_status frrlua_run_rm_rule(lua_State *L, const char *rule) { int status; @@ -126,4 +119,44 @@ enum lua_rm_status lua_run_rm_rule(lua_State *L, const char *rule) status = lua_tonumber(L, -1); return status; } + +/* Initialization */ + +static void *frrlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) +{ + (void)ud; + (void)osize; /* not used */ + + if (nsize == 0) { + free(ptr); + return NULL; + } else + return realloc(ptr, nsize); +} + +lua_State *frrlua_initialize(const char *file) +{ + int status; + lua_State *L = lua_newstate(frrlua_alloc, NULL); + + zlog_debug("Newstate: %p", L); + luaL_openlibs(L); + zlog_debug("Opened lib"); + if (file) { + status = luaL_loadfile(L, file); + if (status) { + zlog_debug("Failure to open %s %d", file, status); + lua_close(L); + return NULL; + } + lua_pcall(L, 0, LUA_MULTRET, 0); + } + + zlog_debug("Setting global function"); + lua_pushcfunction(L, lua_zlog_debug); + lua_setglobal(L, "zlog_debug"); + + return L; +} + #endif diff --git a/lib/frrlua.h b/lib/frrlua.h index 40c7a67b89..5d5e63da69 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -1,27 +1,23 @@ /* - * This file defines the lua interface into - * FRRouting. + * Copyright (C) 2016-2019 Cumulus Networks, Inc. + * Donald Sharp, Quentin Young * - * Copyright (C) 2016 Cumulus Networks, Inc. - * Donald Sharp + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. * - * This file is part of FRRouting (FRR). - * - * FRR is free software; you can redistribute it and/or modify it under the - * terms of the GNU General Public License as published by the Free Software - * Foundation; either version 2, or (at your option) any later version. - * - * FRR is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. * * You should have received a copy of the GNU General Public License along - * with FRR; see the file COPYING. If not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * with this program; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef __LUA_H__ -#define __LUA_H__ +#ifndef __FRRLUA_H__ +#define __FRRLUA_H__ #if defined(HAVE_LUA) @@ -29,18 +25,16 @@ #include "lualib.h" #include "lauxlib.h" +#include "prefix.h" + #ifdef __cplusplus extern "C" { #endif /* - * These functions are helper functions that - * try to glom some of the lua_XXX functionality - * into what we actually need, instead of having - * to make multiple calls to set up what - * we want + * Status enum for Lua routemap processing results */ -enum lua_rm_status { +enum frrlua_rm_status { /* * Script function run failure. This will translate into a * deny @@ -63,26 +57,45 @@ enum lua_rm_status { }; /* - * Open up the lua.scr file and parse - * initial global values, if any. + * Creates a new Lua state, loads all libraries, and if a script is provided, + * runs it. + * + * Returns: + * The new Lua state. */ -lua_State *lua_initialize(const char *file); - -void lua_setup_prefix_table(lua_State *L, const struct prefix *prefix); - -enum lua_rm_status lua_run_rm_rule(lua_State *L, const char *rule); +lua_State *frrlua_initialize(const char *file); /* - * Get particular string/integer information - * from a table. It is *assumed* that - * the table has already been selected + * Pushes a new table containing relevant fields from a prefix structure. + * + * Additionally sets the global variable "prefix" to point at this table. */ -const char *get_string(lua_State *L, const char *key); -int get_integer(lua_State *L, const char *key); +void frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix); + +/* + * Runs a routemap rule or something + */ +enum frrlua_rm_status frrlua_run_rm_rule(lua_State *L, const char *rule); + +/* + * Retrieve a string from table on the top of the stack. + * + * key + * Key of string value in table + */ +const char *frrlua_table_get_string(lua_State *L, const char *key); + +/* + * Retrieve an integer from table on the top of the stack. + * + * key + * Key of string value in table + */ +int frrlua_table_get_integer(lua_State *L, const char *key); #ifdef __cplusplus } #endif -#endif -#endif +#endif /* HAVE_LUA */ +#endif /* __FRRLUA_H__ */ From 3174525d03d0ba74455a683851ac6487cc811c6a Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Thu, 23 May 2019 21:01:49 +0000 Subject: [PATCH 02/35] *: add Lua 5.3 as a dependency Signed-off-by: Quentin Young --- debian/control | 1 + 1 file changed, 1 insertion(+) diff --git a/debian/control b/debian/control index 4aaa9f21bf..a5e22466cb 100644 --- a/debian/control +++ b/debian/control @@ -29,6 +29,7 @@ Build-Depends: bison, python3-dev, python3-pytest , python3-sphinx, + liblua5.3-dev, texinfo (>= 4.7) Standards-Version: 4.5.0.3 Homepage: https://www.frrouting.org/ From cd6ca660c5e96f14f50b6a2f4f6f5cd83cb70d13 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sat, 11 May 2019 01:51:19 +0000 Subject: [PATCH 03/35] lib: add interface -> table encoder Signed-off-by: Quentin Young --- lib/frrlua.c | 30 ++++++++++++++++++++++++++++++ lib/frrlua.h | 5 +++++ 2 files changed, 35 insertions(+) diff --git a/lib/frrlua.c b/lib/frrlua.c index 0c6d607b7a..8107102eed 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -97,6 +97,36 @@ void frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix) lua_setglobal(L, "prefix"); } +void frrlua_newtable_interface(lua_State *L, const struct interface *ifp) +{ + zlog_debug("frrlua: pushing interface table"); + + lua_newtable(L); + lua_pushstring(L, ifp->name); + lua_setfield(L, -2, "name"); + lua_pushinteger(L, ifp->ifindex); + lua_setfield(L, -2, "ifindex"); + lua_pushinteger(L, ifp->status); + lua_setfield(L, -2, "status"); + lua_pushinteger(L, ifp->flags); + lua_setfield(L, -2, "flags"); + lua_pushinteger(L, ifp->metric); + lua_setfield(L, -2, "metric"); + lua_pushinteger(L, ifp->speed); + lua_setfield(L, -2, "speed"); + lua_pushinteger(L, ifp->mtu); + lua_setfield(L, -2, "mtu"); + lua_pushinteger(L, ifp->mtu6); + lua_setfield(L, -2, "mtu6"); + lua_pushinteger(L, ifp->bandwidth); + lua_setfield(L, -2, "bandwidth"); + lua_pushinteger(L, ifp->link_ifindex); + lua_setfield(L, -2, "link_ifindex"); + lua_pushinteger(L, ifp->ll_type); + lua_setfield(L, -2, "linklayer_type"); +} + + /* * Experimental. * diff --git a/lib/frrlua.h b/lib/frrlua.h index 5d5e63da69..14cd3f0bdb 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -72,6 +72,11 @@ lua_State *frrlua_initialize(const char *file); */ void frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix); +/* + * Pushes a new table containing relevant fields from an interface structure. + */ +void frrlua_newtable_interface(lua_State *L, const struct interface *ifp); + /* * Runs a routemap rule or something */ From d473bdc7f1e65a37384acb29af568c4b12a5a207 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Fri, 9 Aug 2019 20:13:07 +0000 Subject: [PATCH 04/35] lib: allow exporting all logging functions to Lua Add a function that will export FRR's logging functions into a Lua table, and add that table to the table of your choice (usually _ENV). For instance, to add logging to the global environment: lua_gettable(L, LUA_REGISTRYINDEX); lua_gettable(L, LUA_RIDX_GLOBALS); frrlua_export_logging(L); Then the following functions are globally accessible to any Lua scripts running with state L: - log.debug() - log.info() - log.notice() - log.warn() - log.error() These are bound to zlog_debug, zlog_info, etc. They only take one string argument for now but this shouldn't be an issue given Lua's builtin facilities for formatting strings. Signed-off-by: Quentin Young --- lib/frrlua.c | 85 ++++++++++++++++++++++++++++++++++++++-------------- lib/frrlua.h | 15 ++++++++++ 2 files changed, 77 insertions(+), 23 deletions(-) diff --git a/lib/frrlua.c b/lib/frrlua.c index 8107102eed..4c38e08a07 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -26,23 +26,6 @@ #include "frrlua.h" #include "log.h" -/* - * Lua -> FRR function bindings. - * - * This section defines functions exportable into Lua environments. - */ - -static int lua_zlog_debug(lua_State *L) -{ - int debug_lua = 1; - const char *string = lua_tostring(L, 1); - - if (debug_lua) - zlog_debug("%s", string); - - return 0; -} - /* * FRR convenience functions. * @@ -126,6 +109,67 @@ void frrlua_newtable_interface(lua_State *L, const struct interface *ifp) lua_setfield(L, -2, "linklayer_type"); } +/* + * Logging. + * + * Lua-compatible wrappers for FRR logging functions. + */ +static const char *frrlua_log_thunk(lua_State *L) +{ + int nargs; + + nargs = lua_gettop(L); + assert(nargs == 1); + + return lua_tostring(L, 1); +} + +static int frrlua_log_debug(lua_State *L) +{ + zlog_debug("%s", frrlua_log_thunk(L)); + return 0; +} + +static int frrlua_log_info(lua_State *L) +{ + zlog_info("%s", frrlua_log_thunk(L)); + return 0; +} + +static int frrlua_log_notice(lua_State *L) +{ + zlog_notice("%s", frrlua_log_thunk(L)); + return 0; +} + +static int frrlua_log_warn(lua_State *L) +{ + zlog_warn("%s", frrlua_log_thunk(L)); + return 0; +} + +static int frrlua_log_error(lua_State *L) +{ + zlog_err("%s", frrlua_log_thunk(L)); + return 0; +} + +static const luaL_Reg log_funcs[] = { + {"debug", frrlua_log_debug}, + {"info", frrlua_log_info}, + {"notice", frrlua_log_notice}, + {"warn", frrlua_log_warn}, + {"error", frrlua_log_error}, + {}, +}; + +void frrlua_export_logging(lua_State *L) +{ + lua_newtable(L); + luaL_setfuncs(L, log_funcs, 0); + lua_setfield(L, -2, "log"); +} + /* * Experimental. @@ -169,9 +213,7 @@ lua_State *frrlua_initialize(const char *file) int status; lua_State *L = lua_newstate(frrlua_alloc, NULL); - zlog_debug("Newstate: %p", L); luaL_openlibs(L); - zlog_debug("Opened lib"); if (file) { status = luaL_loadfile(L, file); if (status) { @@ -182,11 +224,8 @@ lua_State *frrlua_initialize(const char *file) lua_pcall(L, 0, LUA_MULTRET, 0); } - zlog_debug("Setting global function"); - lua_pushcfunction(L, lua_zlog_debug); - lua_setglobal(L, "zlog_debug"); - return L; } + #endif diff --git a/lib/frrlua.h b/lib/frrlua.h index 14cd3f0bdb..d6ee2347a9 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -98,6 +98,21 @@ const char *frrlua_table_get_string(lua_State *L, const char *key); */ int frrlua_table_get_integer(lua_State *L, const char *key); +/* + * Exports a new table containing bindings to FRR zlog functions into the + * global namespace. + * + * From Lua, these functions may be accessed as: + * + * - log.debug() + * - log.info() + * - log.warn() + * - log.error() + * + * They take a single string argument. + */ +void frrlua_export_logging(lua_State *L); + #ifdef __cplusplus } #endif From f93716445925f81c5e257e276a5bcc5cd7d74afd Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 11 Aug 2019 18:35:16 +0000 Subject: [PATCH 05/35] lib: remove frrlua_initialize This was toy code used for testing purposes. Code calling Lua should be very explicit about what is loaded into the Lua state. Also, the allocator used is exactly the same allocator used by default w/ luaL_newstate(). Signed-off-by: Quentin Young --- bgpd/bgp_routemap.c | 3 ++- lib/frrlua.c | 34 ---------------------------------- lib/frrlua.h | 9 --------- 3 files changed, 2 insertions(+), 44 deletions(-) diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index d9457ea616..5274c4fae0 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -346,7 +346,8 @@ route_match_command(void *rule, const struct prefix *prefix, void *object) u_int32_t newlocpref = 0; enum frrlua_rm_status lrm_status; struct bgp_path_info *path = (struct bgp_path_info *)object; - lua_State *L = frrlua_initialize("/etc/frr/lua.scr"); + lua_State *L = luaL_newstate();; + luaL_openlibs(L); if (L == NULL) return status; diff --git a/lib/frrlua.c b/lib/frrlua.c index 4c38e08a07..7dc8786901 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -194,38 +194,4 @@ enum frrlua_rm_status frrlua_run_rm_rule(lua_State *L, const char *rule) return status; } -/* Initialization */ - -static void *frrlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) -{ - (void)ud; - (void)osize; /* not used */ - - if (nsize == 0) { - free(ptr); - return NULL; - } else - return realloc(ptr, nsize); -} - -lua_State *frrlua_initialize(const char *file) -{ - int status; - lua_State *L = lua_newstate(frrlua_alloc, NULL); - - luaL_openlibs(L); - if (file) { - status = luaL_loadfile(L, file); - if (status) { - zlog_debug("Failure to open %s %d", file, status); - lua_close(L); - return NULL; - } - lua_pcall(L, 0, LUA_MULTRET, 0); - } - - return L; -} - - #endif diff --git a/lib/frrlua.h b/lib/frrlua.h index d6ee2347a9..56c43b7755 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -56,15 +56,6 @@ enum frrlua_rm_status { LUA_RM_MATCH_AND_CHANGE, }; -/* - * Creates a new Lua state, loads all libraries, and if a script is provided, - * runs it. - * - * Returns: - * The new Lua state. - */ -lua_State *frrlua_initialize(const char *file); - /* * Pushes a new table containing relevant fields from a prefix structure. * From 0fe6a43d9040524f6715adaaf8f9d7d7aa28a53e Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 11 Aug 2019 19:23:35 +0000 Subject: [PATCH 06/35] lib: move bgp routemap stuff out of frrlua.[ch] Signed-off-by: Quentin Young --- bgpd/bgp_routemap.c | 39 +++++++++++++++++++++++++++++++++++++++ lib/frrlua.c | 24 ------------------------ lib/frrlua.h | 30 ------------------------------ 3 files changed, 39 insertions(+), 54 deletions(-) diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 5274c4fae0..247391adef 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -338,6 +338,45 @@ static const struct route_map_rule_cmd route_match_peer_cmd = { }; #if defined(HAVE_LUA) + +enum frrlua_rm_status { + /* + * Script function run failure. This will translate into a + * deny + */ + LUA_RM_FAILURE = 0, + /* + * No Match was found for the route map function + */ + LUA_RM_NOMATCH, + /* + * Match was found but no changes were made to the + * incoming data. + */ + LUA_RM_MATCH, + /* + * Match was found and data was modified, so + * figure out what changed + */ + LUA_RM_MATCH_AND_CHANGE, +}; + +static enum frrlua_rm_status frrlua_run_rm_rule(lua_State *L, const char *rule) +{ + int status; + + lua_getglobal(L, rule); + status = lua_pcall(L, 0, 1, 0); + if (status) { + zlog_debug("Executing Failure with function: %s: %d", + rule, status); + return LUA_RM_FAILURE; + } + + status = lua_tonumber(L, -1); + return status; +} + static enum route_map_cmd_result_t route_match_command(void *rule, const struct prefix *prefix, void *object) { diff --git a/lib/frrlua.c b/lib/frrlua.c index 7dc8786901..252b667961 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -170,28 +170,4 @@ void frrlua_export_logging(lua_State *L) lua_setfield(L, -2, "log"); } - -/* - * Experimental. - * - * This section has experimental Lua functionality that doesn't belong - * elsewhere. - */ - -enum frrlua_rm_status frrlua_run_rm_rule(lua_State *L, const char *rule) -{ - int status; - - lua_getglobal(L, rule); - status = lua_pcall(L, 0, 1, 0); - if (status) { - zlog_debug("Executing Failure with function: %s: %d", - rule, status); - return LUA_RM_FAILURE; - } - - status = lua_tonumber(L, -1); - return status; -} - #endif diff --git a/lib/frrlua.h b/lib/frrlua.h index 56c43b7755..f77842b445 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -31,31 +31,6 @@ extern "C" { #endif -/* - * Status enum for Lua routemap processing results - */ -enum frrlua_rm_status { - /* - * Script function run failure. This will translate into a - * deny - */ - LUA_RM_FAILURE = 0, - /* - * No Match was found for the route map function - */ - LUA_RM_NOMATCH, - /* - * Match was found but no changes were made to the - * incoming data. - */ - LUA_RM_MATCH, - /* - * Match was found and data was modified, so - * figure out what changed - */ - LUA_RM_MATCH_AND_CHANGE, -}; - /* * Pushes a new table containing relevant fields from a prefix structure. * @@ -68,11 +43,6 @@ void frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix); */ void frrlua_newtable_interface(lua_State *L, const struct interface *ifp); -/* - * Runs a routemap rule or something - */ -enum frrlua_rm_status frrlua_run_rm_rule(lua_State *L, const char *rule); - /* * Retrieve a string from table on the top of the stack. * From e93f19fb66c491f36e9579ccb83517c1e77a9a29 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 11 Aug 2019 19:44:04 +0000 Subject: [PATCH 07/35] lib: add Lua stack dumper Signed-off-by: Quentin Young --- lib/frrlua.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ lib/frrlua.h | 7 +++++++ 2 files changed, 53 insertions(+) diff --git a/lib/frrlua.c b/lib/frrlua.c index 252b667961..15fda79201 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -25,6 +25,7 @@ #include "prefix.h" #include "frrlua.h" #include "log.h" +#include "buffer.h" /* * FRR convenience functions. @@ -170,4 +171,49 @@ void frrlua_export_logging(lua_State *L) lua_setfield(L, -2, "log"); } +/* + * Debugging. + */ + +char *frrlua_stackdump(lua_State *L) +{ + int top = lua_gettop(L); + + char tmpbuf[64]; + struct buffer *buf = buffer_new(4098); + + for (int i = 1; i <= top; i++) { + int t = lua_type(L, i); + + switch (t) { + case LUA_TSTRING: /* strings */ + snprintf(tmpbuf, sizeof(tmpbuf), "\"%s\"\n", + lua_tostring(L, i)); + buffer_putstr(buf, tmpbuf); + break; + case LUA_TBOOLEAN: /* booleans */ + snprintf(tmpbuf, sizeof(tmpbuf), "%s\n", + lua_toboolean(L, i) ? "true" : "false"); + buffer_putstr(buf, tmpbuf); + break; + case LUA_TNUMBER: /* numbers */ + snprintf(tmpbuf, sizeof(tmpbuf), "%g\n", + lua_tonumber(L, i)); + buffer_putstr(buf, tmpbuf); + break; + default: /* other values */ + snprintf(tmpbuf, sizeof(tmpbuf), "%s\n", + lua_typename(L, t)); + buffer_putstr(buf, tmpbuf); + break; + } + } + + char *result = XSTRDUP(MTYPE_TMP, buffer_getstr(buf)); + + buffer_free(buf); + + return result; +} + #endif diff --git a/lib/frrlua.h b/lib/frrlua.h index f77842b445..bc017b6441 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -74,6 +74,13 @@ int frrlua_table_get_integer(lua_State *L, const char *key); */ void frrlua_export_logging(lua_State *L); +/* + * Dump Lua stack to a string. + * + * Return value must be freed with XFREE(MTYPE_TMP, ...); + */ +char *frrlua_stackdump(lua_State *L); + #ifdef __cplusplus } #endif From 5f98c815b6c75115963955a75d1dd2c047d589c9 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sat, 28 Nov 2020 19:02:39 -0500 Subject: [PATCH 08/35] lib: start adding generic scripting stuff Rather than let Luaisms propagate from the start, this is some generic wrapper stuff that defines some semantics for interacting with scripts that aren't specific to the underlying language. The concept I have in mind for FRR's idea of a script is: - has a name - has some inputs, which have types - has some outputs, which have types I don't want to even say they have to be files; maybe we can embed scripts in frr.conf, for example. Similarly the types of inputs and outputs are probably going to end up being some language-specific setup. For now, we will stick to this simple model, but the plan is to add full object support (ie calling back into C). This shouldn't be misconstrued as prepping for multilingual scripting support, which is a bad idea for the following reasons: - Each language would require different FFI methods, and specifically different object encoders; a lot of code - Languages have different capabilities that would have to be brought to parity with each other; a lot of work - Languages have *vastly* different performance characteristics; bad impressions, lots of issues we can't do anything about - Each language would need a dedicated maintainer for the above reasons; pragmatically difficult - Supporting multiple languages fractures the community and limits the audience with which a given script can be shared The only pro for multilingual support would be ease of use for users not familiar with Lua but familiar with one of the other supported languages. This is not enough to outweigh the cons. In order to get rich scripting capabilities, we need to be able to pass representations of internal objects to the scripts. For example, a script that performs some computation based on information about a peer needs access to some equivalent of `struct peer` for the peer in question. To transfer these objects from C-space into Lua-space we need to encode them onto the Lua stack. This patch adds a mapping from arbitrary type names to the functions that encode objects of that type. For example, the function that encodes `struct peer` into a Lua table could be registered with: bgp_peer_encoder_func(struct frrscript *fs, struct peer *peer) { // encode peer to Lua table, push to stack in fs->scriptinfo->L } frrscript_register_type_encoder("peer", bgp_peer_encoder_func); Later on when calling a script that wants a peer, the plan is to be able to specify the type name like so: frrscript_call(script, "peer", peer); Using C-style types for the type names would have been nice, it might be possible to do this with preprocessor magic or possibly python preprocessing later on. Signed-off-by: Quentin Young mergeme no stdlib Signed-off-by: Quentin Young --- lib/command.c | 16 ++++++ lib/frrlua.c | 3 ++ lib/frrlua.h | 1 + lib/frrscript.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/frrscript.h | 84 ++++++++++++++++++++++++++++++ lib/subdir.am | 2 + 6 files changed, 239 insertions(+) create mode 100644 lib/frrscript.c create mode 100644 lib/frrscript.h diff --git a/lib/command.c b/lib/command.c index 87110157f6..99f86ec680 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2279,6 +2279,22 @@ done: return CMD_SUCCESS; } +#ifdef DEV_BUILD +DEFUN(script, + script_cmd, + "script SCRIPT", + "Test command - execute a script\n" + "Script name (same as filename in /etc/frr/scripts/\n") +{ + struct frrscript *fs = frrscript_load(argv[2]->arg, NULL); + int ret = frrscript_call(fs, 42); + + vty_out(vty, "Script result: %d\n", ret); + + return CMD_SUCCESS; +} +#endif + /* Set config filename. Called from vty.c */ void host_config_set(const char *filename) { diff --git a/lib/frrlua.c b/lib/frrlua.c index 15fda79201..57288e6676 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -26,6 +26,9 @@ #include "frrlua.h" #include "log.h" #include "buffer.h" +#include "frrscript.h" + +/* Lua stuff */ /* * FRR convenience functions. diff --git a/lib/frrlua.h b/lib/frrlua.h index bc017b6441..37e8e8635b 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -26,6 +26,7 @@ #include "lauxlib.h" #include "prefix.h" +#include "frrscript.h" #ifdef __cplusplus extern "C" { diff --git a/lib/frrscript.c b/lib/frrscript.c new file mode 100644 index 0000000000..9b27ca7459 --- /dev/null +++ b/lib/frrscript.c @@ -0,0 +1,133 @@ +/* Scripting foo + * Copyright (C) 2020 NVIDIA Corporation + * Quentin Young + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "frrscript.h" +#include "memory.h" +#include "hash.h" +#include "log.h" + +#include "frrlua.h" + +/* Type encoders */ + +struct encoder { + char *typename; + int (*encoder)(struct lua_State *, const void *); +}; + +struct hash *encoder_hash; + +static unsigned int encoder_hash_key(const void *data) +{ + const struct encoder *e = data; + + return string_hash_make(e->typename); +} + +static bool encoder_hash_cmp(const void *d1, const void *d2) +{ + return !strcmp(d1, d2); +} + +static void *encoder_alloc(void *arg) +{ + struct encoder *tmp = arg; + + struct encoder *e = XCALLOC(MTYPE_TMP, sizeof(struct encoder)); + e->typename = XSTRDUP(MTYPE_TMP, tmp->typename); + + return e; +} + +#if 0 +static void encoder_free(struct encoder *e) +{ + XFREE(MTYPE_TMP, e->typename); + XFREE(MTYPE_TMP, e); +} +#endif + +/* Generic script APIs */ + +int frrscript_lua_call(struct frrscript *fs, ...) +{ + /* Process arguments according to argspec in fs */ + /* ... */ + + int ret = lua_pcall(fs->L, 0, 0, 0); + + /* Process stack result according to argspec in fs */ + /* ... */ + + /* LUA_OK is 0, so we can just return lua_pcall's result directly */ + return ret; +} + +void frrscript_register_type_encoder(const char *typename, + int (*encoder)(lua_State *L, void *)) +{ + struct encoder e = { + .typename = (char *) typename, + .encoder = NULL + }; + + if (hash_lookup(encoder_hash, &e)) { + zlog_backtrace(LOG_ERR); + assert(!"Type encoder double-registered."); + } + + hash_get(encoder_hash, &e, encoder_alloc); +} + + +struct frrscript *frrscript_load(const char *name, + int (*load_cb)(struct frrscript *)) +{ + struct frrscript *fs = XCALLOC(MTYPE_TMP, sizeof(struct frrscript)); + + fs->name = XSTRDUP(MTYPE_TMP, name); + fs->L = luaL_newstate(); + + char fname[MAXPATHLEN]; + snprintf(fname, sizeof(fname), FRRSCRIPT_PATH "/%s.lua", fs->name); + + if (luaL_loadfile(fs->L, fname) != LUA_OK) + goto fail; + + if (load_cb && (*load_cb)(fs) != 0) + goto fail; + + return fs; +fail: + frrscript_unload(fs); + return NULL; +} + +void frrscript_unload(struct frrscript *fs) +{ + XFREE(MTYPE_TMP, fs->name); + XFREE(MTYPE_TMP, fs); +} + +void frrscript_init() +{ + encoder_hash = hash_create(encoder_hash_key, encoder_hash_cmp, "Lua type encoders"); +} diff --git a/lib/frrscript.h b/lib/frrscript.h new file mode 100644 index 0000000000..2988d90aa8 --- /dev/null +++ b/lib/frrscript.h @@ -0,0 +1,84 @@ +/* Scripting foo + * Copyright (C) 2020 NVIDIA Corporation + * Quentin Young + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef __FRRSCRIPT_H__ +#define __FRRSCRIPT_H__ + +#include "frrlua.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define FRRSCRIPT_PATH "/etc/frr/scripts" + +struct frrscript { + /* Script name */ + char *name; + + /* Lua state */ + struct lua_State *L; +}; + + +/* + * Create new FRR script. + */ +struct frrscript *frrscript_load(const char *name, + int (*load_cb)(struct frrscript *)); + +/* + * Destroy FRR script. + */ +void frrscript_unload(struct frrscript *fs); + +/* + * Register a Lua encoder for a type. + * + * tname + * Name of type; e.g., "peer", "ospf_interface", etc. Chosen at will. + * + * encoder + * Function pointer to encoder function. Encoder function should push a Lua + * table representing the passed argument - which will have the C type + * associated with the chosen 'tname' to the provided stack. + * + */ +void frrscript_register_type_encoder(const char *tname, + int (*encoder)(lua_State *, void *)); + +/* + * Initialize scripting subsystem. Call this before anything else. + */ +void frrscript_init(void); + +/* + * Forward decl for frrscript_lua_call + */ +int frrscript_lua_call(struct frrscript *fs, ...); + +/* + * Call FRR script. + */ +#define frrscript_call(fs, ...) frrscript_lua_call((fs), __VA_ARGS__) + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __FRRSCRIPT_H__ */ diff --git a/lib/subdir.am b/lib/subdir.am index 038282a99b..c5e32a56d9 100644 --- a/lib/subdir.am +++ b/lib/subdir.am @@ -26,6 +26,7 @@ lib_libfrr_la_SOURCES = \ lib/filter_nb.c \ lib/frrcu.c \ lib/frrlua.c \ + lib/frrscript.c \ lib/frr_pthread.c \ lib/frrstr.c \ lib/getopt.c \ @@ -184,6 +185,7 @@ pkginclude_HEADERS += \ lib/filter.h \ lib/freebsd-queue.h \ lib/frrlua.h \ + lib/frrscript.h \ lib/frr_pthread.h \ lib/frratomic.h \ lib/frrcu.h \ From 3d19ffc5ef88022bc6be8daf052c4720b81b1bc7 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sat, 28 Nov 2020 21:51:42 -0500 Subject: [PATCH 09/35] lib: add 'script foo' test command Signed-off-by: Quentin Young --- lib/command.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/command.c b/lib/command.c index 99f86ec680..56af946997 100644 --- a/lib/command.c +++ b/lib/command.c @@ -49,6 +49,8 @@ #include "northbound_cli.h" #include "network.h" +#include "frrscript.h" + DEFINE_MTYPE_STATIC(LIB, HOST, "Host config") DEFINE_MTYPE(LIB, COMPLETION, "Completion item") @@ -2286,10 +2288,18 @@ DEFUN(script, "Test command - execute a script\n" "Script name (same as filename in /etc/frr/scripts/\n") { - struct frrscript *fs = frrscript_load(argv[2]->arg, NULL); - int ret = frrscript_call(fs, 42); + struct prefix p; + str2prefix("1.2.3.4/24", &p); - vty_out(vty, "Script result: %d\n", ret); + struct frrscript *fs = frrscript_load(argv[1]->arg, NULL); + + if (fs == NULL) { + vty_out(vty, "Script '/etc/frr/scripts/%s.lua' not found\n", + argv[1]->arg); + } else { + int ret = frrscript_call(fs, FRRSCRIPT_ARGS("cool", "prefix", &p), FRRSCRIPT_RESULTS()); + vty_out(vty, "Script result: %d\n", ret); + } return CMD_SUCCESS; } @@ -2389,6 +2399,10 @@ void cmd_init(int terminal) install_element(VIEW_NODE, &echo_cmd); install_element(VIEW_NODE, &autocomplete_cmd); install_element(VIEW_NODE, &find_cmd); +#ifdef DEV_BUILD + install_element(VIEW_NODE, &script_cmd); +#endif + install_element(ENABLE_NODE, &config_end_cmd); install_element(ENABLE_NODE, &config_disable_cmd); From f944ec671c3a3e56bf2315d26f7840419bf0a074 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sat, 28 Nov 2020 23:33:27 -0500 Subject: [PATCH 10/35] lib: make encoder type a typedef Need to use it for casts regularly. Signed-off-by: Quentin Young --- lib/frrscript.c | 4 ++-- lib/frrscript.h | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/frrscript.c b/lib/frrscript.c index 9b27ca7459..246ae25fd7 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -30,7 +30,7 @@ struct encoder { char *typename; - int (*encoder)(struct lua_State *, const void *); + encoder_func encoder; }; struct hash *encoder_hash; @@ -82,7 +82,7 @@ int frrscript_lua_call(struct frrscript *fs, ...) } void frrscript_register_type_encoder(const char *typename, - int (*encoder)(lua_State *L, void *)) + encoder_func encoder) { struct encoder e = { .typename = (char *) typename, diff --git a/lib/frrscript.h b/lib/frrscript.h index 2988d90aa8..6891200def 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -27,6 +27,8 @@ extern "C" { #define FRRSCRIPT_PATH "/etc/frr/scripts" +typedef int (*encoder_func)(struct lua_State *, const void *); + struct frrscript { /* Script name */ char *name; @@ -59,8 +61,7 @@ void frrscript_unload(struct frrscript *fs); * associated with the chosen 'tname' to the provided stack. * */ -void frrscript_register_type_encoder(const char *tname, - int (*encoder)(lua_State *, void *)); +void frrscript_register_type_encoder(const char *tname, encoder_func encoder); /* * Initialize scripting subsystem. Call this before anything else. From 9f79b3758dc305264623c779a677415572bc4d68 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 01:49:06 -0500 Subject: [PATCH 11/35] lib: add macros to count variadic args Magical macro used to compute the number of arguments passed to a variadic macro. Signed-off-by: Quentin Young --- lib/compiler.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/compiler.h b/lib/compiler.h index 217a60d888..70ef8e9bc8 100644 --- a/lib/compiler.h +++ b/lib/compiler.h @@ -279,6 +279,29 @@ extern "C" { #define array_size(ar) (sizeof(ar) / sizeof(ar[0])) +/* Some insane macros to count number of varargs to a functionlike macro */ +#define PP_ARG_N( \ + _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \ + _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ + _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \ + _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \ + _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \ + _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \ + _61, _62, _63, N, ...) N + +#define PP_RSEQ_N() \ + 62, 61, 60, \ + 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \ + 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \ + 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \ + 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \ + 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \ + 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 + +#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__) +#define PP_NARG(...) PP_NARG_(_, ##__VA_ARGS__, PP_RSEQ_N()) + + /* sigh. this is so ugly, it overflows and wraps to being nice again. * * printfrr() supports "%Ld" for , whatever that is typedef'd to. From a3ce06c40b0fc8f7ec695393d099f2ed35a5c8fd Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 01:50:03 -0500 Subject: [PATCH 12/35] lib: update script encoder signatures Update the two test functions that encode a prefix and an interface to match the encoder_func signature expected by the scripting infra. Signed-off-by: Quentin Young --- lib/frrlua.c | 11 +++++++---- lib/frrlua.h | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/frrlua.c b/lib/frrlua.c index 57288e6676..4d576829c5 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -70,7 +70,7 @@ int frrlua_table_get_integer(lua_State *L, const char *key) * datatypes. */ -void frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix) +int frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix) { char buffer[100]; @@ -81,10 +81,11 @@ void frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix) lua_setfield(L, -2, "route"); lua_pushinteger(L, prefix->family); lua_setfield(L, -2, "family"); - lua_setglobal(L, "prefix"); + + return 0; } -void frrlua_newtable_interface(lua_State *L, const struct interface *ifp) +int frrlua_newtable_interface(lua_State *L, const struct interface *ifp) { zlog_debug("frrlua: pushing interface table"); @@ -111,6 +112,8 @@ void frrlua_newtable_interface(lua_State *L, const struct interface *ifp) lua_setfield(L, -2, "link_ifindex"); lua_pushinteger(L, ifp->ll_type); lua_setfield(L, -2, "linklayer_type"); + + return 0; } /* @@ -171,7 +174,7 @@ void frrlua_export_logging(lua_State *L) { lua_newtable(L); luaL_setfuncs(L, log_funcs, 0); - lua_setfield(L, -2, "log"); + lua_setglobal(L, "log"); } /* diff --git a/lib/frrlua.h b/lib/frrlua.h index 37e8e8635b..029e6ecee8 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -37,12 +37,12 @@ extern "C" { * * Additionally sets the global variable "prefix" to point at this table. */ -void frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix); +int frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix); /* * Pushes a new table containing relevant fields from an interface structure. */ -void frrlua_newtable_interface(lua_State *L, const struct interface *ifp); +int frrlua_newtable_interface(lua_State *L, const struct interface *ifp); /* * Retrieve a string from table on the top of the stack. From 923431ef80fc92b313d812605764b005397fa9bb Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 01:59:02 -0500 Subject: [PATCH 13/35] lib: fix hash issues in scripting foo Signed-off-by: Quentin Young --- lib/frrscript.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/frrscript.c b/lib/frrscript.c index 246ae25fd7..e7390a8d94 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -44,7 +44,10 @@ static unsigned int encoder_hash_key(const void *data) static bool encoder_hash_cmp(const void *d1, const void *d2) { - return !strcmp(d1, d2); + const struct encoder *e1 = d1; + const struct encoder *e2 = d2; + + return strmatch(e1->typename, e2->typename); } static void *encoder_alloc(void *arg) @@ -53,6 +56,7 @@ static void *encoder_alloc(void *arg) struct encoder *e = XCALLOC(MTYPE_TMP, sizeof(struct encoder)); e->typename = XSTRDUP(MTYPE_TMP, tmp->typename); + e->encoder = tmp->encoder; return e; } @@ -81,20 +85,16 @@ int frrscript_lua_call(struct frrscript *fs, ...) return ret; } -void frrscript_register_type_encoder(const char *typename, - encoder_func encoder) +void frrscript_register_type_encoder(const char *typename, encoder_func encoder) { - struct encoder e = { - .typename = (char *) typename, - .encoder = NULL - }; + struct encoder e = {.typename = (char *)typename, .encoder = encoder}; if (hash_lookup(encoder_hash, &e)) { zlog_backtrace(LOG_ERR); assert(!"Type encoder double-registered."); } - hash_get(encoder_hash, &e, encoder_alloc); + assert(hash_get(encoder_hash, &e, encoder_alloc)); } From 3b002f19166ca8933c42ebf8f5a7b7794acd53bf Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 02:00:26 -0500 Subject: [PATCH 14/35] lib: allow passing arguments to scripts - Add ability to pass arguments when calling a script - Add macros to define arguments and results Signed-off-by: Quentin Young --- lib/frrscript.c | 71 ++++++++++++++++++++++++++++++++++++++++++++----- lib/frrscript.h | 31 +++++++++++++++++++++ 2 files changed, 96 insertions(+), 6 deletions(-) diff --git a/lib/frrscript.c b/lib/frrscript.c index e7390a8d94..e523fcb475 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -18,6 +18,7 @@ */ #include +#include #include "frrscript.h" #include "memory.h" @@ -73,13 +74,64 @@ static void encoder_free(struct encoder *e) int frrscript_lua_call(struct frrscript *fs, ...) { - /* Process arguments according to argspec in fs */ - /* ... */ + va_list vl; + va_start(vl, fs); - int ret = lua_pcall(fs->L, 0, 0, 0); + int nargs = va_arg(vl, int); + assert(nargs % 3 == 0); - /* Process stack result according to argspec in fs */ - /* ... */ + zlog_debug("%s: Script '%s' called with # args: %d", __func__, fs->name, + nargs); + + struct encoder e = {}; + void *arg; + const char *bindname; + + /* Encode script arguments */ + for (int i = 0; i < nargs; i += 3) { + bindname = va_arg(vl, const char *); + e.typename = va_arg(vl, char *); + arg = va_arg(vl, void *); + + zlog_debug("Script argument | Bind name: %s | Type: %s", + bindname, e.typename); + + struct encoder *enc = hash_lookup(encoder_hash, &e); + assert(enc + && "No encoder for type; rerun with debug logs to see more"); + enc->encoder(fs->L, arg); + + lua_setglobal(fs->L, bindname); + } + + int nresults = va_arg(vl, int); + zlog_debug("Expected script results: %d", nresults); + + int ret = lua_pcall(fs->L, 0, nresults, 0); + + switch (ret) { + case LUA_ERRRUN: + zlog_err("Script '%s' runtime error", fs->name); + break; + case LUA_ERRMEM: + zlog_err("Script '%s' memory error", fs->name); + break; + case LUA_ERRERR: + zlog_err("Script '%s' error handler error", fs->name); + break; + case LUA_ERRGCMM: + zlog_err("Script '%s' garbage collector error", fs->name); + break; + default: + zlog_err("Script '%s' unknown error", fs->name); + break; + } + + /* After script returns, decode results */ + for (int i = 0; i < nresults; i++) { + const char *resultname = va_arg(vl, const char *); + fprintf(stderr, "result: %s\n", resultname); + } /* LUA_OK is 0, so we can just return lua_pcall's result directly */ return ret; @@ -129,5 +181,12 @@ void frrscript_unload(struct frrscript *fs) void frrscript_init() { - encoder_hash = hash_create(encoder_hash_key, encoder_hash_cmp, "Lua type encoders"); + encoder_hash = hash_create(encoder_hash_key, encoder_hash_cmp, + "Lua type encoders"); + + /* Register core library types */ + frrscript_register_type_encoder("prefix", + (encoder_func)frrlua_newtable_prefix); + frrscript_register_type_encoder( + "interface", (encoder_func)frrlua_newtable_interface); } diff --git a/lib/frrscript.h b/lib/frrscript.h index 6891200def..39eebe6e46 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -75,9 +75,40 @@ int frrscript_lua_call(struct frrscript *fs, ...); /* * Call FRR script. + * + * Call it like this: + * + * frrscript_call(fs, FRRSCRIPT_ARGS("cool_prefix", "prefix", p), + * FRRSCRIPT_RESULTS("result1", "result2")) */ #define frrscript_call(fs, ...) frrscript_lua_call((fs), __VA_ARGS__) +/* + * Macro that defines the arguments to a script. + * + * For each argument you want to pass to a script, pass *three* arguments to + * this function. The first should be name of the variable to bind the argument + * to in the script's environment. The second should be the type, as registered + * by frrscript_register_type_encoder(). The third should be the argument + * itself. + * + * This macro itself should be used as the second argument to frrscript_call(). + */ +#define FRRSCRIPT_ARGS(...) PP_NARG(__VA_ARGS__), ##__VA_ARGS__ + +/* + * Macro that defines the results from a script. + * + * Similar to FRRSCRIPT_ARGS, except this defines the results from a script. + * + * The first argument should be the name to bind the first result to and will + * be used after the script finishes to get that particular result value. + * + * This macro itself should be used as the third argument to frrscript_call(). + * It may not be omitted. + */ +#define FRRSCRIPT_RESULTS(...) PP_NARG(__VA_ARGS__), ##__VA_ARGS__ + #ifdef __cplusplus } #endif /* __cplusplus */ From 12bf07a5dc868014f7ceac82c422eae8d69dddbd Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 02:01:33 -0500 Subject: [PATCH 15/35] lib: export zlog functions to scripts Add: - log.warn() - log.error() - log.notice() - log.info() - log.debug() to the global namespace for each script Signed-off-by: Quentin Young --- lib/frrscript.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/frrscript.c b/lib/frrscript.c index e523fcb475..861819b25f 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -157,6 +157,7 @@ struct frrscript *frrscript_load(const char *name, fs->name = XSTRDUP(MTYPE_TMP, name); fs->L = luaL_newstate(); + frrlua_export_logging(fs->L); char fname[MAXPATHLEN]; snprintf(fname, sizeof(fname), FRRSCRIPT_PATH "/%s.lua", fs->name); From e613a6f73c03b338196f71db615047af7862837e Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 02:02:26 -0500 Subject: [PATCH 16/35] lib: initialize scripting system in libfrr Signed-off-by: Quentin Young --- lib/libfrr.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/libfrr.c b/lib/libfrr.c index 8e7777a1a9..8e21ac11b2 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -43,6 +43,7 @@ #include "frrcu.h" #include "frr_pthread.h" #include "defaults.h" +#include "frrscript.h" DEFINE_HOOK(frr_late_init, (struct thread_master * tm), (tm)) DEFINE_HOOK(frr_very_late_init, (struct thread_master * tm), (tm)) @@ -717,6 +718,7 @@ struct thread_master *frr_init(void) lib_cmd_init(); frr_pthread_init(); + frrscript_init(); log_ref_init(); log_ref_vty_init(); From 646b0cce88a4fa6512f434d508a627518d28e55c Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 02:33:07 -0500 Subject: [PATCH 17/35] lib: add better script error handling Signed-off-by: Quentin Young --- lib/frrscript.c | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/frrscript.c b/lib/frrscript.c index 861819b25f..5465aa36e2 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -110,23 +110,28 @@ int frrscript_lua_call(struct frrscript *fs, ...) int ret = lua_pcall(fs->L, 0, nresults, 0); switch (ret) { + case LUA_OK: + break; case LUA_ERRRUN: - zlog_err("Script '%s' runtime error", fs->name); + zlog_err("Script '%s' runtime error: %s", fs->name, lua_tostring(fs->L, -1)); break; case LUA_ERRMEM: - zlog_err("Script '%s' memory error", fs->name); + zlog_err("Script '%s' memory error: %s", fs->name, lua_tostring(fs->L, -1)); break; case LUA_ERRERR: - zlog_err("Script '%s' error handler error", fs->name); + zlog_err("Script '%s' error handler error: %s", fs->name, lua_tostring(fs->L, -1)); break; case LUA_ERRGCMM: - zlog_err("Script '%s' garbage collector error", fs->name); + zlog_err("Script '%s' garbage collector error: %s", fs->name, lua_tostring(fs->L, -1)); break; default: - zlog_err("Script '%s' unknown error", fs->name); + zlog_err("Script '%s' unknown error: %s", fs->name, lua_tostring(fs->L, -1)); break; } + if (ret != LUA_OK) + lua_pop(fs->L, 1); + /* After script returns, decode results */ for (int i = 0; i < nresults; i++) { const char *resultname = va_arg(vl, const char *); @@ -162,7 +167,31 @@ struct frrscript *frrscript_load(const char *name, char fname[MAXPATHLEN]; snprintf(fname, sizeof(fname), FRRSCRIPT_PATH "/%s.lua", fs->name); - if (luaL_loadfile(fs->L, fname) != LUA_OK) + int ret = luaL_loadfile(fs->L, fname); + + switch (ret) { + case LUA_OK: + break; + case LUA_ERRSYNTAX: + zlog_err("Failed loading script '%s': syntax error", fname); + break; + case LUA_ERRMEM: + zlog_err("Failed loading script '%s': out-of-memory error", + fname); + break; + case LUA_ERRGCMM: + zlog_err("Failed loading script '%s': garbage collector error", + fname); + break; + case LUA_ERRFILE: + zlog_err("Failed loading script '%s': file read error", fname); + break; + default: + zlog_err("Failed loading script '%s': unknown error", fname); + break; + } + + if (ret != LUA_OK) goto fail; if (load_cb && (*load_cb)(fs) != 0) From 479d37cce04147d50c3b8a2dcd2b4583b9e9232a Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 02:33:18 -0500 Subject: [PATCH 18/35] lib: close lua state when destroying script Signed-off-by: Quentin Young --- lib/frrscript.c | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/frrscript.c b/lib/frrscript.c index 5465aa36e2..8623415ef8 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -205,6 +205,7 @@ fail: void frrscript_unload(struct frrscript *fs) { + lua_close(fs->L); XFREE(MTYPE_TMP, fs->name); XFREE(MTYPE_TMP, fs); } From 00d9e83a3ff388f53cbe252632ff94dc4dcc1fbd Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 13:21:14 -0500 Subject: [PATCH 19/35] lib: encode plen when passing prefixes to scripts Signed-off-by: Quentin Young --- lib/frrlua.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/frrlua.c b/lib/frrlua.c index 4d576829c5..eeac98ccd8 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -78,7 +78,9 @@ int frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix) lua_newtable(L); lua_pushstring(L, prefix2str(prefix, buffer, 100)); - lua_setfield(L, -2, "route"); + lua_setfield(L, -2, "network"); + lua_pushinteger(L, prefix->prefixlen); + lua_setfield(L, -2, "length"); lua_pushinteger(L, prefix->family); lua_setfield(L, -2, "family"); From 224782816d52af6926122c7a036c66ac23e759a7 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 13:21:50 -0500 Subject: [PATCH 20/35] lib: better load-time error handling for scripts Signed-off-by: Quentin Young --- lib/frrscript.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/frrscript.c b/lib/frrscript.c index 8623415ef8..bc6d075810 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -173,21 +173,25 @@ struct frrscript *frrscript_load(const char *name, case LUA_OK: break; case LUA_ERRSYNTAX: - zlog_err("Failed loading script '%s': syntax error", fname); + 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", - fname); + 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", - fname); + 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", fname); + 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", fname); + zlog_err("Failed loading script '%s': unknown error: %s", fname, + lua_tostring(fs->L, -1)); break; } From 9e47ee98a3de9c7c3f6ee4eb527f59015a5515b5 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 14:51:52 -0500 Subject: [PATCH 21/35] lib: cleanup / refactor scripting foo - fix 'struct lua_State' - change includes to library style - rename encoder funcs to look like lua_push* funcs - fix erroneous doc comment on prefix encoder - remove unused (and broken) convenience func Signed-off-by: Quentin Young --- bgpd/bgp_routemap.c | 2 +- lib/frrlua.c | 17 ++--------------- lib/frrlua.h | 12 +++++------- lib/frrscript.c | 7 +++---- lib/frrscript.h | 3 ++- 5 files changed, 13 insertions(+), 28 deletions(-) diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 247391adef..b296c86a1e 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -394,7 +394,7 @@ route_match_command(void *rule, const struct prefix *prefix, void *object) /* * Setup the prefix information to pass in */ - frrlua_newtable_prefix(L, prefix); + lua_pushprefix(L, prefix); /* * Setup the bgp_path_info information */ diff --git a/lib/frrlua.c b/lib/frrlua.c index eeac98ccd8..f768575b66 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -37,19 +37,6 @@ * stack easier. */ -const char *frrlua_table_get_string(lua_State *L, const char *key) -{ - const char *str; - - lua_pushstring(L, key); - lua_gettable(L, -2); - - str = (const char *)lua_tostring(L, -1); - lua_pop(L, 1); - - return str; -} - int frrlua_table_get_integer(lua_State *L, const char *key) { int result; @@ -70,7 +57,7 @@ int frrlua_table_get_integer(lua_State *L, const char *key) * datatypes. */ -int frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix) +int lua_pushprefix(lua_State *L, const struct prefix *prefix) { char buffer[100]; @@ -87,7 +74,7 @@ int frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix) return 0; } -int frrlua_newtable_interface(lua_State *L, const struct interface *ifp) +int lua_pushinterface(lua_State *L, const struct interface *ifp) { zlog_debug("frrlua: pushing interface table"); diff --git a/lib/frrlua.h b/lib/frrlua.h index 029e6ecee8..6e646e337d 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -21,9 +21,9 @@ #if defined(HAVE_LUA) -#include "lua.h" -#include "lualib.h" -#include "lauxlib.h" +#include +#include +#include #include "prefix.h" #include "frrscript.h" @@ -34,15 +34,13 @@ extern "C" { /* * Pushes a new table containing relevant fields from a prefix structure. - * - * Additionally sets the global variable "prefix" to point at this table. */ -int frrlua_newtable_prefix(lua_State *L, const struct prefix *prefix); +int lua_pushprefix(lua_State *L, const struct prefix *prefix); /* * Pushes a new table containing relevant fields from an interface structure. */ -int frrlua_newtable_interface(lua_State *L, const struct interface *ifp); +int lua_pushinterface(lua_State *L, const struct interface *ifp); /* * Retrieve a string from table on the top of the stack. diff --git a/lib/frrscript.c b/lib/frrscript.c index bc6d075810..f4b696709a 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -220,8 +220,7 @@ void frrscript_init() "Lua type encoders"); /* Register core library types */ - frrscript_register_type_encoder("prefix", - (encoder_func)frrlua_newtable_prefix); - frrscript_register_type_encoder( - "interface", (encoder_func)frrlua_newtable_interface); + frrscript_register_type_encoder("prefix", (encoder_func)lua_pushprefix); + frrscript_register_type_encoder("interface", + (encoder_func)lua_pushinterface); } diff --git a/lib/frrscript.h b/lib/frrscript.h index 39eebe6e46..21cd352881 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -19,6 +19,7 @@ #ifndef __FRRSCRIPT_H__ #define __FRRSCRIPT_H__ +#include #include "frrlua.h" #ifdef __cplusplus @@ -27,7 +28,7 @@ extern "C" { #define FRRSCRIPT_PATH "/etc/frr/scripts" -typedef int (*encoder_func)(struct lua_State *, const void *); +typedef int (*encoder_func)(lua_State *, const void *); struct frrscript { /* Script name */ From 42ae55b5d4dcfed694862b969e8c6fb201418a0d Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 16:00:36 -0500 Subject: [PATCH 22/35] lib: add more type encoder funcs - in_addr - in6_addr - sockunion Signed-off-by: Quentin Young --- lib/frrlua.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/frrlua.h | 24 +++++++++++++++++----- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lib/frrlua.c b/lib/frrlua.c index f768575b66..ac28082680 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -105,6 +105,62 @@ int lua_pushinterface(lua_State *L, const struct interface *ifp) return 0; } +int lua_pushinaddr(lua_State *L, const struct in_addr *addr) +{ + zlog_debug("frrlua: pushing inaddr table"); + + char buf[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, addr, buf, sizeof(buf)); + + lua_newtable(L); + lua_pushinteger(L, addr->s_addr); + lua_setfield(L, -2, "value"); + lua_pushstring(L, buf); + lua_setfield(L, -2, "string"); + + return 0; +} + + +int lua_pushin6addr(lua_State *L, const struct in6_addr *addr) +{ + zlog_debug("frrlua: pushing in6addr table"); + + char buf[INET6_ADDRSTRLEN]; + inet_ntop(AF_INET6, addr, buf, sizeof(buf)); + + lua_newtable(L); + lua_pushlstring(L, (const char *)addr->s6_addr, 16); + lua_setfield(L, -2, "value"); + lua_pushstring(L, buf); + lua_setfield(L, -2, "string"); + + return 0; +} + +int lua_pushsockunion(lua_State *L, const union sockunion *su) +{ + zlog_debug("frrlua: pushing sockunion table"); + + char buf[SU_ADDRSTRLEN]; + sockunion2str(su, buf, sizeof(buf)); + + lua_newtable(L); + lua_pushlstring(L, (const char *)sockunion_get_addr(su), + sockunion_get_addrlen(su)); + lua_setfield(L, -2, "value"); + lua_pushstring(L, buf); + lua_setfield(L, -2, "string"); + + return 0; +} + +int lua_pushtimet(lua_State *L, const time_t *time) +{ + lua_pushinteger(L, *time); + return 0; +} + /* * Logging. * diff --git a/lib/frrlua.h b/lib/frrlua.h index 6e646e337d..a411751172 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -43,12 +43,26 @@ int lua_pushprefix(lua_State *L, const struct prefix *prefix); int lua_pushinterface(lua_State *L, const struct interface *ifp); /* - * Retrieve a string from table on the top of the stack. - * - * key - * Key of string value in table + * Pushes a new table containing both numeric and string representations of an + * in_addr to the stack. */ -const char *frrlua_table_get_string(lua_State *L, const char *key); +int lua_pushinaddr(lua_State *L, const struct in_addr *addr); + +/* + * Pushes a new table containing both numeric and string representations of an + * in6_addr to the stack. + */ +int lua_pushin6addr(lua_State *L, const struct in6_addr *addr); + +/* + * Pushes a time_t to the stack. + */ +int lua_pushtimet(lua_State *L, const time_t *time); + +/* + * Pushes a table representing a sockunion to the stack. + */ +int lua_pushsockunion(lua_State *L, const union sockunion *su); /* * Retrieve an integer from table on the top of the stack. From 47dd87363292e326e971d53f4a3b9e58edb6a3c6 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 17:43:16 -0500 Subject: [PATCH 23/35] lib: change encoder_func signature None of the core lua_push* functions return anything, and it helps to not have to wrap those when using them as function pointers for our encoder system, so change the type of our custom encoders to return void as well. Signed-off-by: Quentin Young --- lib/frrlua.c | 23 ++++++----------------- lib/frrlua.h | 12 ++++++------ lib/frrscript.h | 2 +- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/lib/frrlua.c b/lib/frrlua.c index ac28082680..c8a04fb3dc 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -57,7 +57,7 @@ int frrlua_table_get_integer(lua_State *L, const char *key) * datatypes. */ -int lua_pushprefix(lua_State *L, const struct prefix *prefix) +void lua_pushprefix(lua_State *L, const struct prefix *prefix) { char buffer[100]; @@ -70,11 +70,9 @@ int lua_pushprefix(lua_State *L, const struct prefix *prefix) lua_setfield(L, -2, "length"); lua_pushinteger(L, prefix->family); lua_setfield(L, -2, "family"); - - return 0; } -int lua_pushinterface(lua_State *L, const struct interface *ifp) +void lua_pushinterface(lua_State *L, const struct interface *ifp) { zlog_debug("frrlua: pushing interface table"); @@ -101,11 +99,9 @@ int lua_pushinterface(lua_State *L, const struct interface *ifp) lua_setfield(L, -2, "link_ifindex"); lua_pushinteger(L, ifp->ll_type); lua_setfield(L, -2, "linklayer_type"); - - return 0; } -int lua_pushinaddr(lua_State *L, const struct in_addr *addr) +void lua_pushinaddr(lua_State *L, const struct in_addr *addr) { zlog_debug("frrlua: pushing inaddr table"); @@ -117,12 +113,10 @@ int lua_pushinaddr(lua_State *L, const struct in_addr *addr) lua_setfield(L, -2, "value"); lua_pushstring(L, buf); lua_setfield(L, -2, "string"); - - return 0; } -int lua_pushin6addr(lua_State *L, const struct in6_addr *addr) +void lua_pushin6addr(lua_State *L, const struct in6_addr *addr) { zlog_debug("frrlua: pushing in6addr table"); @@ -134,11 +128,9 @@ int lua_pushin6addr(lua_State *L, const struct in6_addr *addr) lua_setfield(L, -2, "value"); lua_pushstring(L, buf); lua_setfield(L, -2, "string"); - - return 0; } -int lua_pushsockunion(lua_State *L, const union sockunion *su) +void lua_pushsockunion(lua_State *L, const union sockunion *su) { zlog_debug("frrlua: pushing sockunion table"); @@ -151,14 +143,11 @@ int lua_pushsockunion(lua_State *L, const union sockunion *su) lua_setfield(L, -2, "value"); lua_pushstring(L, buf); lua_setfield(L, -2, "string"); - - return 0; } -int lua_pushtimet(lua_State *L, const time_t *time) +void lua_pushtimet(lua_State *L, const time_t *time) { lua_pushinteger(L, *time); - return 0; } /* diff --git a/lib/frrlua.h b/lib/frrlua.h index a411751172..8db5cec586 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -35,34 +35,34 @@ extern "C" { /* * Pushes a new table containing relevant fields from a prefix structure. */ -int lua_pushprefix(lua_State *L, const struct prefix *prefix); +void lua_pushprefix(lua_State *L, const struct prefix *prefix); /* * Pushes a new table containing relevant fields from an interface structure. */ -int lua_pushinterface(lua_State *L, const struct interface *ifp); +void lua_pushinterface(lua_State *L, const struct interface *ifp); /* * Pushes a new table containing both numeric and string representations of an * in_addr to the stack. */ -int lua_pushinaddr(lua_State *L, const struct in_addr *addr); +void lua_pushinaddr(lua_State *L, const struct in_addr *addr); /* * Pushes a new table containing both numeric and string representations of an * in6_addr to the stack. */ -int lua_pushin6addr(lua_State *L, const struct in6_addr *addr); +void lua_pushin6addr(lua_State *L, const struct in6_addr *addr); /* * Pushes a time_t to the stack. */ -int lua_pushtimet(lua_State *L, const time_t *time); +void lua_pushtimet(lua_State *L, const time_t *time); /* * Pushes a table representing a sockunion to the stack. */ -int lua_pushsockunion(lua_State *L, const union sockunion *su); +void lua_pushsockunion(lua_State *L, const union sockunion *su); /* * Retrieve an integer from table on the top of the stack. diff --git a/lib/frrscript.h b/lib/frrscript.h index 21cd352881..44067bf0b4 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -28,7 +28,7 @@ extern "C" { #define FRRSCRIPT_PATH "/etc/frr/scripts" -typedef int (*encoder_func)(lua_State *, const void *); +typedef void (*encoder_func)(lua_State *, const void *); struct frrscript { /* Script name */ From eeb61724232734b1ed26523589060b2b471d3584 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 18:00:35 -0500 Subject: [PATCH 24/35] lib: add more type encoders, register existings Signed-off-by: Quentin Young --- lib/frrlua.c | 5 +++++ lib/frrlua.h | 6 ++++++ lib/frrscript.c | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/lib/frrlua.c b/lib/frrlua.c index c8a04fb3dc..dd468a3def 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -150,6 +150,11 @@ void lua_pushtimet(lua_State *L, const time_t *time) lua_pushinteger(L, *time); } +void lua_pushintegerp(lua_State *L, const int *num) +{ + lua_pushinteger(L, *num); +} + /* * Logging. * diff --git a/lib/frrlua.h b/lib/frrlua.h index 8db5cec586..f9ab5509a6 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -64,6 +64,12 @@ void lua_pushtimet(lua_State *L, const time_t *time); */ void lua_pushsockunion(lua_State *L, const union sockunion *su); +/* + * Push integer. This just wraps lua_pushinteger(), but it takes a pointer, so + * as to be compatible with the encoder_func signature. + */ +void lua_pushintegerp(lua_State *L, const int *num); + /* * Retrieve an integer from table on the top of the stack. * diff --git a/lib/frrscript.c b/lib/frrscript.c index f4b696709a..081ecd026f 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -220,7 +220,13 @@ void frrscript_init() "Lua type encoders"); /* Register core library types */ + frrscript_register_type_encoder("integer", (encoder_func) lua_pushintegerp); + frrscript_register_type_encoder("string", (encoder_func) lua_pushstring); frrscript_register_type_encoder("prefix", (encoder_func)lua_pushprefix); frrscript_register_type_encoder("interface", (encoder_func)lua_pushinterface); + frrscript_register_type_encoder("sockunion", (encoder_func) lua_pushsockunion); + frrscript_register_type_encoder("in_addr", (encoder_func) lua_pushinaddr); + frrscript_register_type_encoder("in6_addr", (encoder_func) lua_pushin6addr); + frrscript_register_type_encoder("time_t", (encoder_func) lua_pushtimet); } From f869ab17a74743919817c6472b3bc3d4b5dbaa26 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 22:09:58 -0500 Subject: [PATCH 25/35] lib: add ability to decode from lua scripts This implements the ability to get results out from lua scripts after they've run. For each C type we support passing to Lua, there is a corresponding `struct frrscript_codec`. This struct contains a typename field - just a string identifying the type - and two function pointers. The first function pointer, encode, takes a lua_State and a pointer to the C value and pushes some corresponding Lua representation onto the stack. The second, decode, assumes there is some Lua value on the stack and decodes it into the corresponding C value. Each supported type's `struct frrscript_codec` is registered with the scripting stuff in the library, which creates a mapping between the type name (string) and the `struct frrscript_codec`. When calling a script, you specify arguments by passing an array of `struct frrscript_env`. Each of these structs has a void *, a type name, and a desired binding name. The type names are used to look up the appropriate function to encode the pointed-at value onto the Lua stack, then bind the pushed value to the provided binding name, so that the converted value is accessible by that name within the script. Results work in a similar way. After a script runs, call frrscript_get_result() with the script and a `struct frrscript_env`. The typename and name fields are used to fetch the Lua value from the script's environment and use the registered decoder for the typename to convert the Lua value back into a C value, which is returned from the function. The caller is responsible for freeing these. frrscript_call()'s macro foo has been stripped, as the underlying function now takes fixed arrays. varargs have awful performance characteristics, they're hard to read, and structs are more defined than an order sensitive list. Signed-off-by: Quentin Young --- lib/command.c | 2 +- lib/frrlua.c | 114 +++++++++++++++++++++++++++++++- lib/frrlua.h | 84 +++++++++++++++++++---- lib/frrscript.c | 172 ++++++++++++++++++++++++++++-------------------- lib/frrscript.h | 84 +++++++++++++---------- 5 files changed, 336 insertions(+), 120 deletions(-) diff --git a/lib/command.c b/lib/command.c index 56af946997..3ca5c5882b 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2297,7 +2297,7 @@ DEFUN(script, vty_out(vty, "Script '/etc/frr/scripts/%s.lua' not found\n", argv[1]->arg); } else { - int ret = frrscript_call(fs, FRRSCRIPT_ARGS("cool", "prefix", &p), FRRSCRIPT_RESULTS()); + int ret = frrscript_call(fs, NULL); vty_out(vty, "Script result: %d\n", ret); } diff --git a/lib/frrlua.c b/lib/frrlua.c index dd468a3def..bd1e5b00e5 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -26,7 +26,6 @@ #include "frrlua.h" #include "log.h" #include "buffer.h" -#include "frrscript.h" /* Lua stuff */ @@ -72,6 +71,17 @@ void lua_pushprefix(lua_State *L, const struct prefix *prefix) lua_setfield(L, -2, "family"); } +void *lua_toprefix(lua_State *L, int idx) +{ + struct prefix *p = XCALLOC(MTYPE_TMP, sizeof(struct prefix)); + + lua_getfield(L, idx, "network"); + str2prefix(lua_tostring(L, -1), p); + lua_pop(L, 1); + + return p; +} + void lua_pushinterface(lua_State *L, const struct interface *ifp) { zlog_debug("frrlua: pushing interface table"); @@ -101,6 +111,47 @@ void lua_pushinterface(lua_State *L, const struct interface *ifp) lua_setfield(L, -2, "linklayer_type"); } +void *lua_tointerface(lua_State *L, int idx) +{ + struct interface *ifp = XCALLOC(MTYPE_TMP, sizeof(struct interface)); + + lua_getfield(L, idx, "name"); + strlcpy(ifp->name, lua_tostring(L, -1), sizeof(ifp->name)); + lua_pop(L, 1); + lua_getfield(L, idx, "ifindex"); + ifp->ifindex = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_getfield(L, idx, "status"); + ifp->status = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_getfield(L, idx, "flags"); + ifp->flags = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_getfield(L, idx, "metric"); + ifp->metric = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_getfield(L, idx, "speed"); + ifp->speed = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_getfield(L, idx, "mtu"); + ifp->mtu = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_getfield(L, idx, "mtu6"); + ifp->mtu6 = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_getfield(L, idx, "bandwidth"); + ifp->bandwidth = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_getfield(L, idx, "link_ifindex"); + ifp->link_ifindex = lua_tointeger(L, -1); + lua_pop(L, 1); + lua_getfield(L, idx, "linklayer_type"); + ifp->ll_type = lua_tointeger(L, -1); + lua_pop(L, 1); + + return ifp; +} + void lua_pushinaddr(lua_State *L, const struct in_addr *addr) { zlog_debug("frrlua: pushing inaddr table"); @@ -115,6 +166,17 @@ void lua_pushinaddr(lua_State *L, const struct in_addr *addr) lua_setfield(L, -2, "string"); } +void *lua_toinaddr(lua_State *L, int idx) +{ + struct in_addr *inaddr = XCALLOC(MTYPE_TMP, sizeof(struct in_addr)); + + lua_getfield(L, idx, "value"); + inaddr->s_addr = lua_tointeger(L, -1); + lua_pop(L, 1); + + return inaddr; +} + void lua_pushin6addr(lua_State *L, const struct in6_addr *addr) { @@ -130,6 +192,17 @@ void lua_pushin6addr(lua_State *L, const struct in6_addr *addr) lua_setfield(L, -2, "string"); } +void *lua_toin6addr(lua_State *L, int idx) +{ + struct in6_addr *in6addr = XCALLOC(MTYPE_TMP, sizeof(struct in6_addr)); + + lua_getfield(L, idx, "string"); + inet_pton(AF_INET6, lua_tostring(L, -1), in6addr); + lua_pop(L, 1); + + return in6addr; +} + void lua_pushsockunion(lua_State *L, const union sockunion *su) { zlog_debug("frrlua: pushing sockunion table"); @@ -145,16 +218,53 @@ void lua_pushsockunion(lua_State *L, const union sockunion *su) lua_setfield(L, -2, "string"); } +void *lua_tosockunion(lua_State *L, int idx) +{ + union sockunion *su = XCALLOC(MTYPE_TMP, sizeof(union sockunion)); + + lua_getfield(L, idx, "string"); + str2sockunion(lua_tostring(L, -1), su); + + return su; +} + void lua_pushtimet(lua_State *L, const time_t *time) { lua_pushinteger(L, *time); } -void lua_pushintegerp(lua_State *L, const int *num) +void *lua_totimet(lua_State *L, int idx) +{ + time_t *t = XCALLOC(MTYPE_TMP, sizeof(time_t)); + + *t = lua_tointeger(L, idx); + + return t; +} + +void lua_pushintegerp(lua_State *L, const long long *num) { lua_pushinteger(L, *num); } +void *lua_tointegerp(lua_State *L, int idx) +{ + int isnum; + long long *num = XCALLOC(MTYPE_TMP, sizeof(long long)); + + *num = lua_tonumberx(L, idx, &isnum); + assert(isnum); + + return num; +} + +void *lua_tostringp(lua_State *L, int idx) +{ + char *string = XSTRDUP(MTYPE_TMP, lua_tostring(L, idx)); + + return string; +} + /* * Logging. * diff --git a/lib/frrlua.h b/lib/frrlua.h index f9ab5509a6..a105bd069d 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -33,42 +33,104 @@ extern "C" { #endif /* - * Pushes a new table containing relevant fields from a prefix structure. + * Converts a prefix to a Lua value and pushes it on the stack. */ void lua_pushprefix(lua_State *L, const struct prefix *prefix); /* - * Pushes a new table containing relevant fields from an interface structure. + * Converts the Lua value at idx to a prefix. + * + * Returns: + * struct prefix allocated with MTYPE_TMP + */ +void *lua_toprefix(lua_State *L, int idx); + +/* + * Converts an interface to a Lua value and pushes it on the stack. */ void lua_pushinterface(lua_State *L, const struct interface *ifp); /* - * Pushes a new table containing both numeric and string representations of an - * in_addr to the stack. + * Converts the Lua value at idx to an interface. + * + * Returns: + * struct interface allocated with MTYPE_TMP. This interface is not hooked + * to anything, nor is it inserted in the global interface tree. + */ +void *lua_tointerface(lua_State *L, int idx); + +/* + * Converts an in_addr to a Lua value and pushes it on the stack. */ void lua_pushinaddr(lua_State *L, const struct in_addr *addr); /* - * Pushes a new table containing both numeric and string representations of an - * in6_addr to the stack. + * Converts the Lua value at idx to an in_addr. + * + * Returns: + * struct in_addr allocated with MTYPE_TMP. + */ +void *lua_toinaddr(lua_State *L, int idx); + +/* + * Converts an in6_addr to a Lua value and pushes it on the stack. */ void lua_pushin6addr(lua_State *L, const struct in6_addr *addr); /* - * Pushes a time_t to the stack. + * Converts the Lua value at idx to an in6_addr. + * + * Returns: + * struct in6_addr allocated with MTYPE_TMP. + */ +void *lua_toin6addr(lua_State *L, int idx); + +/* + * Converts a time_t to a Lua value and pushes it on the stack. */ void lua_pushtimet(lua_State *L, const time_t *time); /* - * Pushes a table representing a sockunion to the stack. + * Converts the Lua value at idx to a time_t. + * + * Returns: + * time_t allocated with MTYPE_TMP. + */ +void *lua_totimet(lua_State *L, int idx); + +/* + * Converts a sockunion to a Lua value and pushes it on the stack. */ void lua_pushsockunion(lua_State *L, const union sockunion *su); /* - * Push integer. This just wraps lua_pushinteger(), but it takes a pointer, so - * as to be compatible with the encoder_func signature. + * Converts the Lua value at idx to a sockunion. + * + * Returns: + * sockunion allocated with MTYPE_TMP. */ -void lua_pushintegerp(lua_State *L, const int *num); +void *lua_tosockunion(lua_State *L, int idx); + +/* + * Converts an int to a Lua value and pushes it on the stack. + */ +void lua_pushintegerp(lua_State *L, const long long *num); + +/* + * Converts the Lua value at idx to an int. + * + * Returns: + * int allocated with MTYPE_TMP. + */ +void *lua_tointegerp(lua_State *L, int idx); + +/* + * Pop string. + * + * Sets *string to a copy of the string at the top of the stack. The copy is + * allocated with MTYPE_TMP and the caller is responsible for freeing it. + */ +void *lua_tostringp(lua_State *L, int idx); /* * Retrieve an integer from table on the top of the stack. diff --git a/lib/frrscript.c b/lib/frrscript.c index 081ecd026f..c8ea946334 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -27,133 +27,171 @@ #include "frrlua.h" -/* Type encoders */ +/* Codecs */ -struct encoder { - char *typename; - encoder_func encoder; -}; +struct frrscript_codec frrscript_codecs_lib[] = { + {.typename = "integer", + .encoder = (encoder_func)lua_pushintegerp, + .decoder = lua_tointegerp}, + {.typename = "string", + .encoder = (encoder_func)lua_pushstring, + .decoder = lua_tostringp}, + {.typename = "prefix", + .encoder = (encoder_func)lua_pushprefix, + .decoder = lua_toprefix}, + {.typename = "interface", + .encoder = (encoder_func)lua_pushinterface, + .decoder = lua_tointerface}, + {.typename = "in_addr", + .encoder = (encoder_func)lua_pushinaddr, + .decoder = lua_toinaddr}, + {.typename = "in6_addr", + .encoder = (encoder_func)lua_pushin6addr, + .decoder = lua_toin6addr}, + {.typename = "sockunion", + .encoder = (encoder_func)lua_pushsockunion, + .decoder = lua_tosockunion}, + {.typename = "time_t", + .encoder = (encoder_func)lua_pushtimet, + .decoder = lua_totimet}, + {}}; -struct hash *encoder_hash; +/* Type codecs */ -static unsigned int encoder_hash_key(const void *data) +struct hash *codec_hash; + +static unsigned int codec_hash_key(const void *data) { - const struct encoder *e = data; + const struct frrscript_codec *c = data; - return string_hash_make(e->typename); + return string_hash_make(c->typename); } -static bool encoder_hash_cmp(const void *d1, const void *d2) +static bool codec_hash_cmp(const void *d1, const void *d2) { - const struct encoder *e1 = d1; - const struct encoder *e2 = d2; + const struct frrscript_codec *e1 = d1; + const struct frrscript_codec *e2 = d2; return strmatch(e1->typename, e2->typename); } -static void *encoder_alloc(void *arg) +static void *codec_alloc(void *arg) { - struct encoder *tmp = arg; + struct frrscript_codec *tmp = arg; - struct encoder *e = XCALLOC(MTYPE_TMP, sizeof(struct encoder)); + struct frrscript_codec *e = + XCALLOC(MTYPE_TMP, sizeof(struct frrscript_codec)); e->typename = XSTRDUP(MTYPE_TMP, tmp->typename); e->encoder = tmp->encoder; + e->decoder = tmp->decoder; return e; } #if 0 -static void encoder_free(struct encoder *e) +static void codec_free(struct codec *c) { - XFREE(MTYPE_TMP, e->typename); - XFREE(MTYPE_TMP, e); + XFREE(MTYPE_TMP, c->typename); + XFREE(MTYPE_TMP, c); } #endif /* Generic script APIs */ -int frrscript_lua_call(struct frrscript *fs, ...) +int frrscript_call(struct frrscript *fs, struct frrscript_env *env) { - va_list vl; - va_start(vl, fs); - - int nargs = va_arg(vl, int); - assert(nargs % 3 == 0); - - zlog_debug("%s: Script '%s' called with # args: %d", __func__, fs->name, - nargs); - - struct encoder e = {}; - void *arg; + struct frrscript_codec c = {}; + const void *arg; const char *bindname; /* Encode script arguments */ - for (int i = 0; i < nargs; i += 3) { - bindname = va_arg(vl, const char *); - e.typename = va_arg(vl, char *); - arg = va_arg(vl, void *); + for (int i = 0; env && env[i].val != NULL; i++) { + bindname = env[i].name; + c.typename = env[i].typename; + arg = env[i].val; zlog_debug("Script argument | Bind name: %s | Type: %s", - bindname, e.typename); + bindname, c.typename); - struct encoder *enc = hash_lookup(encoder_hash, &e); - assert(enc + struct frrscript_codec *codec = hash_lookup(codec_hash, &c); + assert(codec && "No encoder for type; rerun with debug logs to see more"); - enc->encoder(fs->L, arg); + codec->encoder(fs->L, arg); lua_setglobal(fs->L, bindname); } - int nresults = va_arg(vl, int); - zlog_debug("Expected script results: %d", nresults); - - int ret = lua_pcall(fs->L, 0, nresults, 0); + int ret = lua_pcall(fs->L, 0, 0, 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("Script '%s' runtime error: %s", fs->name, + lua_tostring(fs->L, -1)); break; case LUA_ERRMEM: - zlog_err("Script '%s' memory error: %s", fs->name, lua_tostring(fs->L, -1)); + zlog_err("Script '%s' memory error: %s", fs->name, + lua_tostring(fs->L, -1)); break; case LUA_ERRERR: - zlog_err("Script '%s' error handler error: %s", fs->name, lua_tostring(fs->L, -1)); + zlog_err("Script '%s' error handler error: %s", fs->name, + lua_tostring(fs->L, -1)); break; case LUA_ERRGCMM: - zlog_err("Script '%s' garbage collector error: %s", fs->name, lua_tostring(fs->L, -1)); + zlog_err("Script '%s' garbage collector error: %s", fs->name, + lua_tostring(fs->L, -1)); break; default: - zlog_err("Script '%s' unknown error: %s", fs->name, lua_tostring(fs->L, -1)); + zlog_err("Script '%s' unknown error: %s", fs->name, + lua_tostring(fs->L, -1)); break; } - if (ret != LUA_OK) + if (ret != LUA_OK) { lua_pop(fs->L, 1); - - /* After script returns, decode results */ - for (int i = 0; i < nresults; i++) { - const char *resultname = va_arg(vl, const char *); - fprintf(stderr, "result: %s\n", resultname); + goto done; } +done: /* LUA_OK is 0, so we can just return lua_pcall's result directly */ return ret; } -void frrscript_register_type_encoder(const char *typename, encoder_func encoder) +void *frrscript_get_result(struct frrscript *fs, + const struct frrscript_env *result) { - struct encoder e = {.typename = (char *)typename, .encoder = encoder}; + void *r; + struct frrscript_codec c = {.typename = result->typename}; - if (hash_lookup(encoder_hash, &e)) { - zlog_backtrace(LOG_ERR); - assert(!"Type encoder double-registered."); - } + struct frrscript_codec *codec = hash_lookup(codec_hash, &c); - assert(hash_get(encoder_hash, &e, encoder_alloc)); + lua_getglobal(fs->L, result->name); + r = codec->decoder(fs->L, -1); + lua_pop(fs->L, 1); + + return r; } +void frrscript_register_type_codec(struct frrscript_codec *codec) +{ + struct frrscript_codec c = *codec; + + zlog_debug("Registering codec for '%s'", codec->typename); + + if (hash_lookup(codec_hash, &c)) { + zlog_backtrace(LOG_ERR); + assert(!"Type codec double-registered."); + } + + assert(hash_get(codec_hash, &c, codec_alloc)); +} + +void frrscript_register_type_codecs(struct frrscript_codec *codecs) +{ + for (int i = 0; codecs[i].typename != NULL; i++) + frrscript_register_type_codec(&codecs[i]); +} struct frrscript *frrscript_load(const char *name, int (*load_cb)(struct frrscript *)) @@ -216,17 +254,9 @@ void frrscript_unload(struct frrscript *fs) void frrscript_init() { - encoder_hash = hash_create(encoder_hash_key, encoder_hash_cmp, - "Lua type encoders"); + codec_hash = hash_create(codec_hash_key, codec_hash_cmp, + "Lua type encoders"); /* Register core library types */ - frrscript_register_type_encoder("integer", (encoder_func) lua_pushintegerp); - frrscript_register_type_encoder("string", (encoder_func) lua_pushstring); - frrscript_register_type_encoder("prefix", (encoder_func)lua_pushprefix); - frrscript_register_type_encoder("interface", - (encoder_func)lua_pushinterface); - frrscript_register_type_encoder("sockunion", (encoder_func) lua_pushsockunion); - frrscript_register_type_encoder("in_addr", (encoder_func) lua_pushinaddr); - frrscript_register_type_encoder("in6_addr", (encoder_func) lua_pushin6addr); - frrscript_register_type_encoder("time_t", (encoder_func) lua_pushtimet); + frrscript_register_type_codecs(frrscript_codecs_lib); } diff --git a/lib/frrscript.h b/lib/frrscript.h index 44067bf0b4..cbc0ca6c51 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -29,6 +29,13 @@ extern "C" { #define FRRSCRIPT_PATH "/etc/frr/scripts" typedef void (*encoder_func)(lua_State *, const void *); +typedef void *(*decoder_func)(lua_State *, int); + +struct frrscript_codec { + const char *typename; + encoder_func encoder; + decoder_func decoder; +}; struct frrscript { /* Script name */ @@ -38,6 +45,16 @@ struct frrscript { struct lua_State *L; }; +struct frrscript_env { + /* Value type */ + const char *typename; + + /* Binding name */ + const char *name; + + /* Value */ + const void *val; +}; /* * Create new FRR script. @@ -51,64 +68,61 @@ struct frrscript *frrscript_load(const char *name, void frrscript_unload(struct frrscript *fs); /* - * Register a Lua encoder for a type. + * Register a Lua codec for a type. * * tname * Name of type; e.g., "peer", "ospf_interface", etc. Chosen at will. * - * encoder - * Function pointer to encoder function. Encoder function should push a Lua + * codec(s) + * Function pointer to codec struct. Encoder function should push a Lua * table representing the passed argument - which will have the C type - * associated with the chosen 'tname' to the provided stack. + * associated with the chosen 'tname' to the provided stack. The decoder + * function should pop a value from the top of the stack and return a heap + * chunk containing that value. Allocations should be made with MTYPE_TMP. + * + * If using the plural function variant, pass a NULL-terminated array. * */ -void frrscript_register_type_encoder(const char *tname, encoder_func encoder); +void frrscript_register_type_codec(struct frrscript_codec *codec); +void frrscript_register_type_codecs(struct frrscript_codec *codecs); /* * Initialize scripting subsystem. Call this before anything else. */ void frrscript_init(void); -/* - * Forward decl for frrscript_lua_call - */ -int frrscript_lua_call(struct frrscript *fs, ...); /* - * Call FRR script. + * Call script. * - * Call it like this: + * fs + * The script to call; this is obtained from frrscript_load(). * - * frrscript_call(fs, FRRSCRIPT_ARGS("cool_prefix", "prefix", p), - * FRRSCRIPT_RESULTS("result1", "result2")) + * env + * The script's environment. Specify this as an array of frrscript_env. + * + * Returns: + * 0 if the script ran successfully, nonzero otherwise. */ -#define frrscript_call(fs, ...) frrscript_lua_call((fs), __VA_ARGS__) +int frrscript_call(struct frrscript *fs, struct frrscript_env *env); + /* - * Macro that defines the arguments to a script. + * Get result from finished script. * - * For each argument you want to pass to a script, pass *three* arguments to - * this function. The first should be name of the variable to bind the argument - * to in the script's environment. The second should be the type, as registered - * by frrscript_register_type_encoder(). The third should be the argument - * itself. + * fs + * The script. This script must have been run already. * - * This macro itself should be used as the second argument to frrscript_call(). + * result + * The result to extract from the script. + * This reuses the frrscript_env type, but only the typename and name fields + * need to be set. The value is returned directly. + * + * Returns: + * The script result of the specified name and type, or NULL. */ -#define FRRSCRIPT_ARGS(...) PP_NARG(__VA_ARGS__), ##__VA_ARGS__ - -/* - * Macro that defines the results from a script. - * - * Similar to FRRSCRIPT_ARGS, except this defines the results from a script. - * - * The first argument should be the name to bind the first result to and will - * be used after the script finishes to get that particular result value. - * - * This macro itself should be used as the third argument to frrscript_call(). - * It may not be omitted. - */ -#define FRRSCRIPT_RESULTS(...) PP_NARG(__VA_ARGS__), ##__VA_ARGS__ +void *frrscript_get_result(struct frrscript *fs, + const struct frrscript_env *result); #ifdef __cplusplus } From 1a3a91e211f60d62376c78e81f11e9db452f3c33 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 23:21:31 -0500 Subject: [PATCH 26/35] lib: use appropriate MTYPE for scripts Signed-off-by: Quentin Young --- lib/frrscript.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/frrscript.c b/lib/frrscript.c index c8ea946334..60d3c2fc6d 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -19,13 +19,15 @@ #include #include +#include #include "frrscript.h" +#include "frrlua.h" #include "memory.h" #include "hash.h" #include "log.h" -#include "frrlua.h" +DEFINE_MTYPE_STATIC(LIB, SCRIPT, "Scripting"); /* Codecs */ @@ -80,8 +82,8 @@ static void *codec_alloc(void *arg) struct frrscript_codec *tmp = arg; struct frrscript_codec *e = - XCALLOC(MTYPE_TMP, sizeof(struct frrscript_codec)); - e->typename = XSTRDUP(MTYPE_TMP, tmp->typename); + XCALLOC(MTYPE_SCRIPT, sizeof(struct frrscript_codec)); + e->typename = XSTRDUP(MTYPE_SCRIPT, tmp->typename); e->encoder = tmp->encoder; e->decoder = tmp->decoder; @@ -114,8 +116,7 @@ int frrscript_call(struct frrscript *fs, struct frrscript_env *env) bindname, c.typename); struct frrscript_codec *codec = hash_lookup(codec_hash, &c); - assert(codec - && "No encoder for type; rerun with debug logs to see more"); + assert(codec && "No encoder for type"); codec->encoder(fs->L, arg); lua_setglobal(fs->L, bindname); @@ -165,6 +166,12 @@ void *frrscript_get_result(struct frrscript *fs, struct frrscript_codec c = {.typename = result->typename}; struct frrscript_codec *codec = hash_lookup(codec_hash, &c); + assert(codec && "No encoder for type"); + + if (!codec->decoder) { + zlog_err("No script decoder for type '%s'", result->typename); + return NULL; + } lua_getglobal(fs->L, result->name); r = codec->decoder(fs->L, -1); @@ -196,9 +203,9 @@ void frrscript_register_type_codecs(struct frrscript_codec *codecs) struct frrscript *frrscript_load(const char *name, int (*load_cb)(struct frrscript *)) { - struct frrscript *fs = XCALLOC(MTYPE_TMP, sizeof(struct frrscript)); + struct frrscript *fs = XCALLOC(MTYPE_SCRIPT, sizeof(struct frrscript)); - fs->name = XSTRDUP(MTYPE_TMP, name); + fs->name = XSTRDUP(MTYPE_SCRIPT, name); fs->L = luaL_newstate(); frrlua_export_logging(fs->L); @@ -248,8 +255,8 @@ fail: void frrscript_unload(struct frrscript *fs) { lua_close(fs->L); - XFREE(MTYPE_TMP, fs->name); - XFREE(MTYPE_TMP, fs); + XFREE(MTYPE_SCRIPT, fs->name); + XFREE(MTYPE_SCRIPT, fs); } void frrscript_init() From b4becb063f0ec06c4ecfe2ca832383d49d375441 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 16:01:59 -0500 Subject: [PATCH 27/35] bgpd: update routemap scripting example - Change from "match command " to "match script