lib: Add encoder/decoder for nexthop/nexthop group

Signed-off-by: Donald Lee <dlqs@gmx.com>
This commit is contained in:
Donald Lee 2021-08-18 20:25:40 +08:00
parent e6f42b94c6
commit 9b851b74b0
2 changed files with 56 additions and 0 deletions

View File

@ -312,6 +312,58 @@ void *lua_tointegerp(lua_State *L, int idx)
return num;
}
void lua_pushnexthop(lua_State *L, const struct nexthop *nexthop)
{
lua_newtable(L);
lua_pushinteger(L, nexthop->vrf_id);
lua_setfield(L, -2, "vrf_id");
lua_pushinteger(L, nexthop->ifindex);
lua_setfield(L, -2, "ifindex");
lua_pushinteger(L, nexthop->type);
lua_setfield(L, -2, "type");
lua_pushinteger(L, nexthop->flags);
lua_setfield(L, -2, "flags");
if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
lua_pushinteger(L, nexthop->bh_type);
lua_setfield(L, -2, "bh_type");
} else if (nexthop->type == NEXTHOP_TYPE_IPV4) {
lua_pushinaddr(L, &nexthop->gate.ipv4);
lua_setfield(L, -2, "gate");
} else if (nexthop->type == NEXTHOP_TYPE_IPV6) {
lua_pushin6addr(L, &nexthop->gate.ipv6);
lua_setfield(L, -2, "gate");
}
lua_pushinteger(L, nexthop->nh_label_type);
lua_setfield(L, -2, "nh_label_type");
lua_pushinteger(L, nexthop->weight);
lua_setfield(L, -2, "weight");
lua_pushinteger(L, nexthop->backup_num);
lua_setfield(L, -2, "backup_num");
lua_pushinteger(L, *(nexthop->backup_idx));
lua_setfield(L, -2, "backup_idx");
if (nexthop->nh_encap_type == NET_VXLAN) {
lua_pushinteger(L, nexthop->nh_encap.vni);
lua_setfield(L, -2, "vni");
}
lua_pushinteger(L, nexthop->nh_encap_type);
lua_setfield(L, -2, "nh_encap_type");
lua_pushinteger(L, nexthop->srte_color);
lua_setfield(L, -2, "srte_color");
}
void lua_pushnexthop_group(lua_State *L, const struct nexthop_group *ng)
{
lua_newtable(L);
struct nexthop *nexthop;
int i = 0;
for (ALL_NEXTHOPS_PTR(ng, nexthop)) {
lua_pushnexthop(L, nexthop);
lua_seti(L, -2, i);
i++;
}
}
void lua_decode_stringp(lua_State *L, int idx, char *str)
{
strlcpy(str, lua_tostring(L, idx), strlen(str) + 1);

View File

@ -142,6 +142,10 @@ void lua_decode_sockunion(lua_State *L, int idx, union sockunion *su);
*/
void *lua_tosockunion(lua_State *L, int idx);
void lua_pushnexthop_group(lua_State *L, const struct nexthop_group *ng);
void lua_pushnexthop(lua_State *L, const struct nexthop *nexthop);
/*
* Converts an int to a Lua value and pushes it on the stack.
*/