From: Donald Lee Date: Mon, 21 Jun 2021 21:03:07 +0000 (+0800) Subject: bgpd: Update bgp_script encoders and decoders X-Git-Tag: base_8.1~355^2~15 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=b7da61c1d1b2824b139bfce73b1d6849d4b75b6a;p=mirror%2Ffrr.git bgpd: Update bgp_script encoders and decoders This is an example of creating encoders and decoders for user defined structs and registering them in the ENCODE_ARGS DECODE_ARGS macro in frrscript. Signed-off-by: Donald Lee --- diff --git a/bgpd/bgp_script.c b/bgpd/bgp_script.c index 0cda1927f8..9446a25a05 100644 --- a/bgpd/bgp_script.c +++ b/bgpd/bgp_script.c @@ -28,9 +28,8 @@ #include "bgp_aspath.h" #include "frratomic.h" #include "frrscript.h" -#include "frrlua.h" -static void lua_pushpeer(lua_State *L, const struct peer *peer) +void lua_pushpeer(lua_State *L, const struct peer *peer) { lua_newtable(L); lua_pushinteger(L, peer->as); @@ -142,7 +141,7 @@ static void lua_pushpeer(lua_State *L, const struct peer *peer) lua_setfield(L, -2, "stats"); } -static void lua_pushattr(lua_State *L, const struct attr *attr) +void lua_pushattr(lua_State *L, const struct attr *attr) { lua_newtable(L); lua_pushinteger(L, attr->med); @@ -155,10 +154,8 @@ static void lua_pushattr(lua_State *L, const struct attr *attr) lua_setfield(L, -2, "localpref"); } -static void *lua_toattr(lua_State *L, int idx) +void lua_decode_attr(lua_State *L, int idx, struct attr *attr) { - struct attr *attr = XCALLOC(MTYPE_TMP, sizeof(struct attr)); - lua_getfield(L, -1, "metric"); attr->med = lua_tointeger(L, -1); lua_pop(L, 1); @@ -171,7 +168,13 @@ static void *lua_toattr(lua_State *L, int idx) lua_getfield(L, -1, "localpref"); attr->local_pref = lua_tointeger(L, -1); lua_pop(L, 1); +} + +void *lua_toattr(lua_State *L, int idx) +{ + struct attr *attr = XCALLOC(MTYPE_TMP, sizeof(struct attr)); + lua_decode_attr(L, idx, attr); return attr; } diff --git a/bgpd/bgp_script.h b/bgpd/bgp_script.h index 6682c2eebd..f8178aa983 100644 --- a/bgpd/bgp_script.h +++ b/bgpd/bgp_script.h @@ -21,14 +21,25 @@ #define __BGP_SCRIPT__ #include +#include "bgpd.h" #ifdef HAVE_SCRIPTING +#include "frrlua.h" + /* * Initialize scripting stuff. */ void bgp_script_init(void); +void lua_pushpeer(lua_State *L, const struct peer *peer); + +void lua_pushattr(lua_State *L, const struct attr *attr); + +void lua_decode_attr(lua_State *L, int idx, struct attr *attr); + +void *lua_toattr(lua_State *L, int idx); + #endif /* HAVE_SCRIPTING */ #endif /* __BGP_SCRIPT__ */ diff --git a/lib/frrscript.h b/lib/frrscript.h index 14e1834a11..5942c3dce8 100644 --- a/lib/frrscript.h +++ b/lib/frrscript.h @@ -25,6 +25,7 @@ #include #include "frrlua.h" +#include "../bgpd/bgp_script.h" #ifdef __cplusplus extern "C" { @@ -117,7 +118,9 @@ struct in_addr * : lua_pushinaddr, \ struct in6_addr * : lua_pushin6addr, \ union sockunion * : lua_pushsockunion, \ time_t * : lua_pushtimet, \ -char * : lua_pushstring_wrapper \ +char * : lua_pushstring_wrapper, \ +struct attr * : lua_pushattr, \ +struct peer * : lua_pushpeer \ )(L, value) #define DECODE_ARGS_WITH_STATE(L, value) \ @@ -129,7 +132,8 @@ struct in_addr * : lua_decode_inaddr, \ struct in6_addr * : lua_decode_in6addr, \ union sockunion * : lua_decode_sockunion, \ time_t * : lua_decode_timet, \ -char * : lua_decode_stringp \ +char * : lua_decode_stringp, \ +struct attr * : lua_decode_attr \ )(L, -1, value) /*