From 5f98c815b6c75115963955a75d1dd2c047d589c9 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sat, 28 Nov 2020 19:02:39 -0500 Subject: 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 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib/command.c') 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) { -- cgit v1.2.3 From 3d19ffc5ef88022bc6be8daf052c4720b81b1bc7 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sat, 28 Nov 2020 21:51:42 -0500 Subject: lib: add 'script foo' test command Signed-off-by: Quentin Young --- lib/command.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'lib/command.c') 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); + + struct frrscript *fs = frrscript_load(argv[1]->arg, NULL); - vty_out(vty, "Script result: %d\n", ret); + 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); -- cgit v1.2.3 From f869ab17a74743919817c6472b3bc3d4b5dbaa26 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 29 Nov 2020 22:09:58 -0500 Subject: 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 | 174 +++++++++++++++++++++++++++++++++----------------------- lib/frrscript.h | 80 +++++++++++++++----------- 5 files changed, 335 insertions(+), 119 deletions(-) (limited to 'lib/command.c') 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_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_pushintegerp(lua_State *L, const int *num); +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 */ - -struct encoder { - char *typename; - encoder_func encoder; -}; - -struct hash *encoder_hash; - -static unsigned int encoder_hash_key(const void *data) +/* Codecs */ + +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}, + {}}; + +/* Type codecs */ + +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}; + + struct frrscript_codec *codec = hash_lookup(codec_hash, &c); + + lua_getglobal(fs->L, result->name); + r = codec->decoder(fs->L, -1); + lua_pop(fs->L, 1); + + return r; +} - if (hash_lookup(encoder_hash, &e)) { +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 encoder double-registered."); + assert(!"Type codec double-registered."); } - assert(hash_get(encoder_hash, &e, encoder_alloc)); + 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 it like this: + * Call script. * - * 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. + * fs + * The script to call; this is obtained from frrscript_load(). * - * 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. + * env + * The script's environment. Specify this as an array of frrscript_env. * - * This macro itself should be used as the second argument to frrscript_call(). + * Returns: + * 0 if the script ran successfully, nonzero otherwise. */ -#define FRRSCRIPT_ARGS(...) PP_NARG(__VA_ARGS__), ##__VA_ARGS__ +int frrscript_call(struct frrscript *fs, struct frrscript_env *env); + /* - * Macro that defines the results from a script. + * Get result from finished script. * - * Similar to FRRSCRIPT_ARGS, except this defines the results from a script. + * fs + * The script. This script must have been run already. * - * 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. + * 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. * - * This macro itself should be used as the third argument to frrscript_call(). - * It may not be omitted. + * Returns: + * The script result of the specified name and type, or NULL. */ -#define FRRSCRIPT_RESULTS(...) PP_NARG(__VA_ARGS__), ##__VA_ARGS__ +void *frrscript_get_result(struct frrscript *fs, + const struct frrscript_env *result); #ifdef __cplusplus } -- cgit v1.2.3 From fa22080d22e1e4e69f7bed6d041083df1b85b4b7 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Mon, 30 Nov 2020 17:01:03 -0500 Subject: build: HAVE_LUA -> HAVE_SCRIPTING And also guard all scripting-related stuff with HAVE_SCRIPTING. Signed-off-by: Quentin Young --- bgpd/bgp_main.c | 2 ++ bgpd/bgp_routemap.c | 13 +++++++------ bgpd/bgp_script.c | 6 +++++- bgpd/bgp_script.h | 8 ++++++++ configure.ac | 24 +++++++++++------------- lib/command.c | 4 ++-- lib/frrlua.c | 6 ++++-- lib/frrlua.h | 7 +++++-- lib/frrscript.c | 7 ++++++- lib/frrscript.h | 6 ++++++ lib/libfrr.c | 2 ++ 11 files changed, 58 insertions(+), 27 deletions(-) (limited to 'lib/command.c') diff --git a/bgpd/bgp_main.c b/bgpd/bgp_main.c index f8669dd663..67704850a7 100644 --- a/bgpd/bgp_main.c +++ b/bgpd/bgp_main.c @@ -507,7 +507,9 @@ int main(int argc, char **argv) /* Initializations. */ bgp_vrf_init(); +#ifdef HAVE_SCRIPTING bgp_script_init(); +#endif hook_register(routing_conf_event, routing_control_plane_protocols_name_validate); diff --git a/bgpd/bgp_routemap.c b/bgpd/bgp_routemap.c index 59c49f76a7..57414eadc0 100644 --- a/bgpd/bgp_routemap.c +++ b/bgpd/bgp_routemap.c @@ -338,7 +338,7 @@ static const struct route_map_rule_cmd route_match_peer_cmd = { route_match_peer_free }; -#if defined(HAVE_LUA) +#ifdef HAVE_SCRIPTING enum frrlua_rm_status { /* @@ -468,7 +468,8 @@ static const struct route_map_rule_cmd route_match_script_cmd = { route_match_script_compile, route_match_script_free }; -#endif + +#endif /* HAVE_SCRIPTING */ /* `match ip address IP_ACCESS_LIST' */ @@ -4134,7 +4135,7 @@ DEFUN (no_match_peer, RMAP_EVENT_MATCH_DELETED); } -#if defined(HAVE_LUA) +#ifdef HAVE_SCRIPTING DEFUN (match_script, match_script_cmd, "[no] match script WORD", @@ -4156,7 +4157,7 @@ DEFUN (match_script, RMAP_EVENT_FILTER_ADDED); } } -#endif +#endif /* HAVE_SCRIPTING */ /* match probability */ DEFUN (match_probability, @@ -5670,7 +5671,7 @@ void bgp_route_map_init(void) route_map_install_match(&route_match_peer_cmd); route_map_install_match(&route_match_local_pref_cmd); -#if defined(HAVE_LUA) +#ifdef HAVE_SCRIPTING route_map_install_match(&route_match_script_cmd); #endif route_map_install_match(&route_match_ip_address_cmd); @@ -5835,7 +5836,7 @@ void bgp_route_map_init(void) install_element(RMAP_NODE, &no_set_ipv6_nexthop_prefer_global_cmd); install_element(RMAP_NODE, &set_ipv6_nexthop_peer_cmd); install_element(RMAP_NODE, &no_set_ipv6_nexthop_peer_cmd); -#if defined(HAVE_LUA) +#ifdef HAVE_SCRIPTING install_element(RMAP_NODE, &match_script_cmd); #endif } diff --git a/bgpd/bgp_script.c b/bgpd/bgp_script.c index c665f2e7c5..0cda1927f8 100644 --- a/bgpd/bgp_script.c +++ b/bgpd/bgp_script.c @@ -19,7 +19,8 @@ */ #include -#include + +#ifdef HAVE_SCRIPTING #include "bgpd.h" #include "bgp_script.h" @@ -27,6 +28,7 @@ #include "bgp_aspath.h" #include "frratomic.h" #include "frrscript.h" +#include "frrlua.h" static void lua_pushpeer(lua_State *L, const struct peer *peer) { @@ -186,3 +188,5 @@ void bgp_script_init(void) { frrscript_register_type_codecs(frrscript_codecs_bgpd); } + +#endif /* HAVE_SCRIPTING */ diff --git a/bgpd/bgp_script.h b/bgpd/bgp_script.h index 15ed3345c4..6682c2eebd 100644 --- a/bgpd/bgp_script.h +++ b/bgpd/bgp_script.h @@ -17,10 +17,18 @@ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * MA 02110-1301 USA */ +#ifndef __BGP_SCRIPT__ +#define __BGP_SCRIPT__ #include +#ifdef HAVE_SCRIPTING + /* * Initialize scripting stuff. */ void bgp_script_init(void); + +#endif /* HAVE_SCRIPTING */ + +#endif /* __BGP_SCRIPT__ */ diff --git a/configure.ac b/configure.ac index 6a7353d51a..11b06cb127 100755 --- a/configure.ac +++ b/configure.ac @@ -274,24 +274,22 @@ if test "$enable_clang_coverage" = "yes"; then ]) fi +if test "$enable_scripting" = "yes"; then + AX_PROG_LUA([5.3]) + AX_LUA_HEADERS + AX_LUA_LIBS([ + AC_DEFINE([HAVE_SCRIPTING], [1], [Have support for scripting]) + LIBS="$LIBS $LUA_LIB" + ]) +fi + if test "$enable_dev_build" = "yes"; then AC_DEFINE([DEV_BUILD], [1], [Build for development]) if test "$orig_cflags" = ""; then AC_C_FLAG([-g3]) AC_C_FLAG([-O0]) fi - if test "$enable_lua" = "yes"; then - AX_PROG_LUA([5.3]) - AX_LUA_HEADERS - AX_LUA_LIBS([ - AC_DEFINE([HAVE_LUA], [1], [Have support for Lua interpreter]) - LIBS="$LIBS $LUA_LIB" - ]) - fi else - if test "$enable_lua" = "yes"; then - AC_MSG_ERROR([Lua is not meant to be built/used outside of development at this time]) - fi if test "$orig_cflags" = ""; then AC_C_FLAG([-g]) AC_C_FLAG([-O2]) @@ -693,8 +691,8 @@ fi AC_ARG_ENABLE([dev_build], AS_HELP_STRING([--enable-dev-build], [build for development])) -AC_ARG_ENABLE([lua], - AS_HELP_STRING([--enable-lua], [Build Lua scripting])) +AC_ARG_ENABLE([scripting], + AS_HELP_STRING([--enable-scripting], [Build with scripting support])) if test "$enable_time_check" != "no" ; then if test "$enable_time_check" = "yes" -o "$enable_time_check" = "" ; then diff --git a/lib/command.c b/lib/command.c index 3ca5c5882b..b19147ed81 100644 --- a/lib/command.c +++ b/lib/command.c @@ -2281,7 +2281,7 @@ done: return CMD_SUCCESS; } -#ifdef DEV_BUILD +#if defined(DEV_BUILD) && defined(HAVE_SCRIPTING) DEFUN(script, script_cmd, "script SCRIPT", @@ -2399,7 +2399,7 @@ 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 +#if defined(DEV_BUILD) && defined(HAVE_SCRIPTING) install_element(VIEW_NODE, &script_cmd); #endif diff --git a/lib/frrlua.c b/lib/frrlua.c index bd1e5b00e5..cd878eb711 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -19,9 +19,11 @@ * 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) +#ifdef HAVE_SCRIPTING + #include "prefix.h" #include "frrlua.h" #include "log.h" @@ -371,4 +373,4 @@ char *frrlua_stackdump(lua_State *L) return result; } -#endif +#endif /* HAVE_SCRIPTING */ diff --git a/lib/frrlua.h b/lib/frrlua.h index a105bd069d..8e52931e50 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -19,7 +19,9 @@ #ifndef __FRRLUA_H__ #define __FRRLUA_H__ -#if defined(HAVE_LUA) +#include + +#ifdef HAVE_SCRIPTING #include #include @@ -166,5 +168,6 @@ char *frrlua_stackdump(lua_State *L); } #endif -#endif /* HAVE_LUA */ +#endif /* HAVE_SCRIPTING */ + #endif /* __FRRLUA_H__ */ diff --git a/lib/frrscript.c b/lib/frrscript.c index 60d3c2fc6d..6d03ca119f 100644 --- a/lib/frrscript.c +++ b/lib/frrscript.c @@ -16,8 +16,10 @@ * 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 + +#ifdef HAVE_SCRIPTING + #include #include @@ -27,6 +29,7 @@ #include "hash.h" #include "log.h" + DEFINE_MTYPE_STATIC(LIB, SCRIPT, "Scripting"); /* Codecs */ @@ -267,3 +270,5 @@ void frrscript_init() /* Register core library types */ frrscript_register_type_codecs(frrscript_codecs_lib); } + +#endif /* HAVE_SCRIPTING */ diff --git a/lib/frrscript.h b/lib/frrscript.h index cbc0ca6c51..4206420f48 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -19,6 +19,10 @@ #ifndef __FRRSCRIPT_H__ #define __FRRSCRIPT_H__ +#include + +#ifdef HAVE_SCRIPTING + #include #include "frrlua.h" @@ -128,4 +132,6 @@ void *frrscript_get_result(struct frrscript *fs, } #endif /* __cplusplus */ +#endif /* HAVE_SCRIPTING */ + #endif /* __FRRSCRIPT_H__ */ diff --git a/lib/libfrr.c b/lib/libfrr.c index 8e21ac11b2..05e6cf36e4 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -718,7 +718,9 @@ struct thread_master *frr_init(void) lib_cmd_init(); frr_pthread_init(); +#ifdef HAVE_SCRIPTING frrscript_init(); +#endif log_ref_init(); log_ref_vty_init(); -- cgit v1.2.3