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>
This commit is contained in:
Donald Lee 2021-06-22 05:03:07 +08:00
parent bdc1085d7b
commit b7da61c1d1
3 changed files with 26 additions and 8 deletions

View 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;
}

View File

@ -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__ */

View 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)
/*