summaryrefslogtreecommitdiff
path: root/lib/frrlua.h
AgeCommit message (Collapse)Author
2024-08-28lib: Add a helper function to dump Lua stackDonatas Abraitis
Very handy for debugging. In Lua script just use "log.trace(table)": ``` function on_rib_process_dplane_results(ctx) log.trace(ctx.rinfo.zd_ng) end ``` You will get something like: ``` Aug 28 17:04:36 donatas-laptop zebra[3782199]: [GCZ7N-MM9D9] { 1: { type: 2 weight: 1 flags: 5 backup_idx: 0 vrf_id: 0 nh_encap_type: 0 gate: { value: 5.87967e+08 string: "192.168.11.35" } nh_label_type: 0 srte_color: 0 ifindex: 0 backup_num: 0 } 2: { type: 3 weight: 1 flags: 3 backup_idx: 0 vrf_id: 0 nh_encap_type: 0 nh_label_type: 0 srte_color: 0 ifindex: 4 backup_num: 0 } } ``` Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
2023-11-20lib: Create encoders for int and rename stuffDonald Lee
Create encoders/decoders for int and rename current int encoders /decoders to long long. Signed-off-by: Donald Lee <dlqs@gmx.com>
2023-11-20lib: Remove old noop decodersDonald Lee
Co-authored-by: Quentin Young <qlyoung@qlyoung.net> Signed-off-by: Quentin Young <qlyoung@qlyoung.net> Signed-off-by: Donald Lee <dlqs@gmx.com>
2023-08-01lib: Do not use time_t as a special Lua encoder/decoderDonatas Abraitis
This is purely an integer (long long/long), and causes issues for 32-bit systems. Signed-off-by: Donatas Abraitis <donatas@opensourcerouting.org>
2023-02-09*: auto-convert to SPDX License IDsDavid Lamparter
Done with a combination of regex'ing and banging my head against a wall. Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
2021-10-20lib: Add encoder/decoder for nexthop/nexthop groupDonald Lee
Signed-off-by: Donald Lee <dlqs@gmx.com>
2021-10-20lib: Add encoders/decoders for ipaddr/ethaddrDonald Lee
Signed-off-by: Donald Lee <dlqs@gmx.com>
2021-07-26lib: Add new MTYPE for script resultsDonald Lee
Signed-off-by: Donald Lee <dlqs@gmx.com>
2021-07-24lib: Rename decode_intDonald Lee
Signed-off-by: Donald Lee <dlqs@gmx.com>
2021-07-24lib: comment typoDonald Lee
Signed-off-by: Donald Lee <dlqs@gmx.com>
2021-07-18lib: Add int encoder/decoderDonald Lee
Signed-off-by: Donald Lee <dlqs@gmx.com>
2021-06-22lib: Add lua noop decoderDonald Lee
Use this noop decoder for const values (since we can't mutate a const value passed into frrscript_call anyways) or values we don't want to write a decoder for. Signed-off-by: Donald Lee <dlqs@gmx.com>
2021-06-22lib: Add decoder functionsDonald Lee
Split existing lua_to* functions into two functions: - lua_decode_*: unmarshall Lua values into an existing C data structure - lua_to*: allocate *and* unmarshall Lua values into C data structure This allows us to mutate C data structures passed into frrscript_call, without allocation Signed-off-by: Donald Lee <dlqs@gmx.com>
2021-01-25lib: Wrapper a function to make gcc-10 happyDonald Sharp
gcc-10 is complaining: lib/frrscript.c:42:14: error: cast between incompatible function types from ‘const char * (*)(lua_State *, const char *)’ to ‘void (*)(lua_State *, const void *)’ [-Werror=cast-function-type] 42 | .encoder = (encoder_func)lua_pushstring, | ^ Wrapper it to make it happy. Not sure what else to do. Signed-off-by: Donald Sharp <sharpd@nvidia.com>
2020-12-01build: HAVE_LUA -> HAVE_SCRIPTINGQuentin Young
And also guard all scripting-related stuff with HAVE_SCRIPTING. Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-12-01lib: add ability to decode from lua scriptsQuentin Young
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 <qlyoung@nvidia.com>
2020-12-01lib: add more type encoders, register existingsQuentin Young
Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-12-01lib: change encoder_func signatureQuentin Young
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 <qlyoung@nvidia.com>
2020-12-01lib: add more type encoder funcsQuentin Young
- in_addr - in6_addr - sockunion Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-12-01lib: cleanup / refactor scripting fooQuentin Young
- 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 <qlyoung@nvidia.com>
2020-12-01lib: update script encoder signaturesQuentin Young
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 <qlyoung@nvidia.com>
2020-12-01lib: start adding generic scripting stuffQuentin Young
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 <qlyoung@nvidia.com> mergeme no stdlib Signed-off-by: Quentin Young <qlyoung@nvidia.com>
2020-12-01lib: add Lua stack dumperQuentin Young
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2020-12-01lib: move bgp routemap stuff out of frrlua.[ch]Quentin Young
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2020-12-01lib: remove frrlua_initializeQuentin Young
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 <qlyoung@cumulusnetworks.com>
2020-12-01lib: allow exporting all logging functions to LuaQuentin Young
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 <qlyoung@cumulusnetworks.com>
2020-12-01lib: add interface -> table encoderQuentin Young
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
2020-12-01lib: clean up frrlua.[ch]Quentin Young
* 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 <qlyoung@cumulusnetworks.com>
2020-03-25*: use the current project name (FRRouting)Lou Berger
Signed-off-by: Lou Berger <lberger@labn.net>
2019-05-10build: fix Lua buildQuentin Young
Each of Lua's major versions are incompatible with each other. Ubuntu, at least, does not provide a single liblua.so or /usr/include/lua; all SOs and headers are versioned, e.g. liblua5.3.so and /usr/include/lua5.3. There's already an m4 macro in the GNU collection to handle this situation, so let's use that. This allows building with Lua enabled to work on platforms other than Fedora. * Move lib/lua.[ch] -> lib/frrlua.[ch] to prevent path conflicts * Fix configure.ac search for proper CPP and linker flags * Add Lua include path to AM_CPPFLAGS * Update vtysh/extract.pl.in Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>