2 * Copyright (C) 2020 NVIDIA Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; see the file COPYING; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
26 #include "bgp_script.h"
27 #include "bgp_debug.h"
28 #include "bgp_aspath.h"
29 #include "frratomic.h"
30 #include "frrscript.h"
32 void lua_pushpeer(lua_State
*L
, const struct peer
*peer
)
35 lua_pushinteger(L
, peer
->as
);
36 lua_setfield(L
, -2, "remote_as");
37 lua_pushinteger(L
, peer
->local_as
);
38 lua_setfield(L
, -2, "local_as");
39 lua_pushinaddr(L
, &peer
->remote_id
);
40 lua_setfield(L
, -2, "remote_id");
41 lua_pushinaddr(L
, &peer
->local_id
);
42 lua_setfield(L
, -2, "local_id");
43 lua_pushstring(L
, lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
44 lua_setfield(L
, -2, "state");
45 lua_pushstring(L
, peer
->desc
? peer
->desc
: "");
46 lua_setfield(L
, -2, "description");
47 lua_pushtimet(L
, &peer
->uptime
);
48 lua_setfield(L
, -2, "uptime");
49 lua_pushtimet(L
, &peer
->readtime
);
50 lua_setfield(L
, -2, "last_readtime");
51 lua_pushtimet(L
, &peer
->resettime
);
52 lua_setfield(L
, -2, "last_resettime");
53 lua_pushsockunion(L
, peer
->su_local
);
54 lua_setfield(L
, -2, "local_address");
55 lua_pushsockunion(L
, peer
->su_remote
);
56 lua_setfield(L
, -2, "remote_address");
57 lua_pushinteger(L
, peer
->cap
);
58 lua_setfield(L
, -2, "capabilities");
59 lua_pushinteger(L
, peer
->flags
);
60 lua_setfield(L
, -2, "flags");
61 lua_pushstring(L
, peer
->password
? peer
->password
: "");
62 lua_setfield(L
, -2, "password");
64 /* Nested tables here */
69 lua_pushinteger(L
, peer
->holdtime
);
70 lua_setfield(L
, -2, "hold");
71 lua_pushinteger(L
, peer
->keepalive
);
72 lua_setfield(L
, -2, "keepalive");
73 lua_pushinteger(L
, peer
->connect
);
74 lua_setfield(L
, -2, "connect");
75 lua_pushinteger(L
, peer
->routeadv
);
76 lua_setfield(L
, -2, "route_advertisement");
78 lua_setfield(L
, -2, "configured");
82 lua_pushinteger(L
, peer
->v_holdtime
);
83 lua_setfield(L
, -2, "hold");
84 lua_pushinteger(L
, peer
->v_keepalive
);
85 lua_setfield(L
, -2, "keepalive");
86 lua_pushinteger(L
, peer
->v_connect
);
87 lua_setfield(L
, -2, "connect");
88 lua_pushinteger(L
, peer
->v_routeadv
);
89 lua_setfield(L
, -2, "route_advertisement");
91 lua_setfield(L
, -2, "negotiated");
93 lua_setfield(L
, -2, "timers");
97 lua_pushinteger(L
, atomic_load_explicit(&peer
->open_in
,
98 memory_order_relaxed
));
99 lua_setfield(L
, -2, "open_in");
100 lua_pushinteger(L
, atomic_load_explicit(&peer
->open_out
,
101 memory_order_relaxed
));
102 lua_setfield(L
, -2, "open_out");
103 lua_pushinteger(L
, atomic_load_explicit(&peer
->update_in
,
104 memory_order_relaxed
));
105 lua_setfield(L
, -2, "update_in");
106 lua_pushinteger(L
, atomic_load_explicit(&peer
->update_out
,
107 memory_order_relaxed
));
108 lua_setfield(L
, -2, "update_out");
109 lua_pushinteger(L
, atomic_load_explicit(&peer
->update_time
,
110 memory_order_relaxed
));
111 lua_setfield(L
, -2, "update_time");
112 lua_pushinteger(L
, atomic_load_explicit(&peer
->keepalive_in
,
113 memory_order_relaxed
));
114 lua_setfield(L
, -2, "keepalive_in");
115 lua_pushinteger(L
, atomic_load_explicit(&peer
->keepalive_out
,
116 memory_order_relaxed
));
117 lua_setfield(L
, -2, "keepalive_out");
118 lua_pushinteger(L
, atomic_load_explicit(&peer
->notify_in
,
119 memory_order_relaxed
));
120 lua_setfield(L
, -2, "notify_in");
121 lua_pushinteger(L
, atomic_load_explicit(&peer
->notify_out
,
122 memory_order_relaxed
));
123 lua_setfield(L
, -2, "notify_out");
124 lua_pushinteger(L
, atomic_load_explicit(&peer
->refresh_in
,
125 memory_order_relaxed
));
126 lua_setfield(L
, -2, "refresh_in");
127 lua_pushinteger(L
, atomic_load_explicit(&peer
->refresh_out
,
128 memory_order_relaxed
));
129 lua_setfield(L
, -2, "refresh_out");
130 lua_pushinteger(L
, atomic_load_explicit(&peer
->dynamic_cap_in
,
131 memory_order_relaxed
));
132 lua_setfield(L
, -2, "dynamic_cap_in");
133 lua_pushinteger(L
, atomic_load_explicit(&peer
->dynamic_cap_out
,
134 memory_order_relaxed
));
135 lua_setfield(L
, -2, "dynamic_cap_out");
136 lua_pushinteger(L
, peer
->established
);
137 lua_setfield(L
, -2, "times_established");
138 lua_pushinteger(L
, peer
->dropped
);
139 lua_setfield(L
, -2, "times_dropped");
141 lua_setfield(L
, -2, "stats");
144 void lua_pushattr(lua_State
*L
, const struct attr
*attr
)
147 lua_pushinteger(L
, attr
->med
);
148 lua_setfield(L
, -2, "metric");
149 lua_pushinteger(L
, attr
->nh_ifindex
);
150 lua_setfield(L
, -2, "ifindex");
151 lua_pushstring(L
, attr
->aspath
->str
);
152 lua_setfield(L
, -2, "aspath");
153 lua_pushinteger(L
, attr
->local_pref
);
154 lua_setfield(L
, -2, "localpref");
157 void lua_decode_attr(lua_State
*L
, int idx
, struct attr
*attr
)
159 lua_getfield(L
, idx
, "metric");
160 attr
->med
= lua_tointeger(L
, -1);
162 lua_getfield(L
, idx
, "ifindex");
163 attr
->nh_ifindex
= lua_tointeger(L
, -1);
165 lua_getfield(L
, idx
, "aspath");
166 attr
->aspath
= aspath_str2aspath(lua_tostring(L
, -1));
168 lua_getfield(L
, idx
, "localpref");
169 attr
->local_pref
= lua_tointeger(L
, -1);
174 void *lua_toattr(lua_State
*L
, int idx
)
176 struct attr
*attr
= XCALLOC(MTYPE_TMP
, sizeof(struct attr
));
178 lua_decode_attr(L
, idx
, attr
);
182 struct frrscript_codec frrscript_codecs_bgpd
[] = {
184 .encoder
= (encoder_func
)lua_pushpeer
,
187 .encoder
= (encoder_func
)lua_pushattr
,
188 .decoder
= lua_toattr
},
191 void bgp_script_init(void)
193 frrscript_register_type_codecs(frrscript_codecs_bgpd
);
196 #endif /* HAVE_SCRIPTING */