summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDonald Lee <dlqs@gmx.com>2021-06-21 23:03:07 +0200
committerDonald Lee <dlqs@gmx.com>2021-06-21 23:03:07 +0200
commitb7da61c1d1b2824b139bfce73b1d6849d4b75b6a (patch)
treef2462d0d2710228d1b74795a976f74798031e63e
parentlib: Update script SCRIPT command (diff)
downloadfrr-b7da61c1d1b2824b139bfce73b1d6849d4b75b6a.tar.xz
frr-b7da61c1d1b2824b139bfce73b1d6849d4b75b6a.zip
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 <dlqs@gmx.com>
-rw-r--r--bgpd/bgp_script.c15
-rw-r--r--bgpd/bgp_script.h11
-rw-r--r--lib/frrscript.h8
3 files changed, 26 insertions, 8 deletions
diff --git a/bgpd/bgp_script.c b/bgpd/bgp_script.c
index 0cda1927f..9446a25a0 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 6682c2eeb..f8178aa98 100644
--- a/bgpd/bgp_script.h
+++ b/bgpd/bgp_script.h
@@ -21,14 +21,25 @@
#define __BGP_SCRIPT__
#include <zebra.h>
+#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 14e1834a1..5942c3dce 100644
--- a/lib/frrscript.h
+++ b/lib/frrscript.h
@@ -25,6 +25,7 @@
#include <lua.h>
#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)
/*