]> git.puffer.fish Git - matthieu/frr.git/commitdiff
bgpd: Update bgp_script encoders and decoders
authorDonald Lee <dlqs@gmx.com>
Mon, 21 Jun 2021 21:03:07 +0000 (05:03 +0800)
committerDonald Lee <dlqs@gmx.com>
Mon, 21 Jun 2021 21:03:07 +0000 (05:03 +0800)
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>
bgpd/bgp_script.c
bgpd/bgp_script.h
lib/frrscript.h

index 0cda1927f8316c1974259917eab5ef3eb6b04491..9446a25a05c950b16235eb9dd0b77ebd7aa01ed8 100644 (file)
@@ -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;
 }
 
index 6682c2eebdcc8fa1998956a1c925c1ccbcd0d50a..f8178aa98385ed47a67ead573d8770af1743d4d8 100644 (file)
 #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__ */
index 14e1834a115f8cc5a8273d4a745f3af4c3f10e2f..5942c3dce8dd3ac9242f0bb3317f12eeb1d6dcea 100644 (file)
@@ -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)
 
 /*