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"
33 static void lua_pushpeer(lua_State
*L
, const struct peer
*peer
)
36 lua_pushinteger(L
, peer
->as
);
37 lua_setfield(L
, -2, "remote_as");
38 lua_pushinteger(L
, peer
->local_as
);
39 lua_setfield(L
, -2, "local_as");
40 lua_pushinaddr(L
, &peer
->remote_id
);
41 lua_setfield(L
, -2, "remote_id");
42 lua_pushinaddr(L
, &peer
->local_id
);
43 lua_setfield(L
, -2, "local_id");
44 lua_pushstring(L
, lookup_msg(bgp_status_msg
, peer
->status
, NULL
));
45 lua_setfield(L
, -2, "state");
46 lua_pushstring(L
, peer
->desc
? peer
->desc
: "");
47 lua_setfield(L
, -2, "description");
48 lua_pushtimet(L
, &peer
->uptime
);
49 lua_setfield(L
, -2, "uptime");
50 lua_pushtimet(L
, &peer
->readtime
);
51 lua_setfield(L
, -2, "last_readtime");
52 lua_pushtimet(L
, &peer
->resettime
);
53 lua_setfield(L
, -2, "last_resettime");
54 lua_pushsockunion(L
, peer
->su_local
);
55 lua_setfield(L
, -2, "local_address");
56 lua_pushsockunion(L
, peer
->su_remote
);
57 lua_setfield(L
, -2, "remote_address");
58 lua_pushinteger(L
, peer
->cap
);
59 lua_setfield(L
, -2, "capabilities");
60 lua_pushinteger(L
, peer
->flags
);
61 lua_setfield(L
, -2, "flags");
62 lua_pushstring(L
, peer
->password
? peer
->password
: "");
63 lua_setfield(L
, -2, "password");
65 /* Nested tables here */
70 lua_pushinteger(L
, peer
->holdtime
);
71 lua_setfield(L
, -2, "hold");
72 lua_pushinteger(L
, peer
->keepalive
);
73 lua_setfield(L
, -2, "keepalive");
74 lua_pushinteger(L
, peer
->connect
);
75 lua_setfield(L
, -2, "connect");
76 lua_pushinteger(L
, peer
->routeadv
);
77 lua_setfield(L
, -2, "route_advertisement");
79 lua_setfield(L
, -2, "configured");
83 lua_pushinteger(L
, peer
->v_holdtime
);
84 lua_setfield(L
, -2, "hold");
85 lua_pushinteger(L
, peer
->v_keepalive
);
86 lua_setfield(L
, -2, "keepalive");
87 lua_pushinteger(L
, peer
->v_connect
);
88 lua_setfield(L
, -2, "connect");
89 lua_pushinteger(L
, peer
->v_routeadv
);
90 lua_setfield(L
, -2, "route_advertisement");
92 lua_setfield(L
, -2, "negotiated");
94 lua_setfield(L
, -2, "timers");
98 lua_pushinteger(L
, atomic_load_explicit(&peer
->open_in
,
99 memory_order_relaxed
));
100 lua_setfield(L
, -2, "open_in");
101 lua_pushinteger(L
, atomic_load_explicit(&peer
->open_out
,
102 memory_order_relaxed
));
103 lua_setfield(L
, -2, "open_out");
104 lua_pushinteger(L
, atomic_load_explicit(&peer
->update_in
,
105 memory_order_relaxed
));
106 lua_setfield(L
, -2, "update_in");
107 lua_pushinteger(L
, atomic_load_explicit(&peer
->update_out
,
108 memory_order_relaxed
));
109 lua_setfield(L
, -2, "update_out");
110 lua_pushinteger(L
, atomic_load_explicit(&peer
->update_time
,
111 memory_order_relaxed
));
112 lua_setfield(L
, -2, "update_time");
113 lua_pushinteger(L
, atomic_load_explicit(&peer
->keepalive_in
,
114 memory_order_relaxed
));
115 lua_setfield(L
, -2, "keepalive_in");
116 lua_pushinteger(L
, atomic_load_explicit(&peer
->keepalive_out
,
117 memory_order_relaxed
));
118 lua_setfield(L
, -2, "keepalive_out");
119 lua_pushinteger(L
, atomic_load_explicit(&peer
->notify_in
,
120 memory_order_relaxed
));
121 lua_setfield(L
, -2, "notify_in");
122 lua_pushinteger(L
, atomic_load_explicit(&peer
->notify_out
,
123 memory_order_relaxed
));
124 lua_setfield(L
, -2, "notify_out");
125 lua_pushinteger(L
, atomic_load_explicit(&peer
->refresh_in
,
126 memory_order_relaxed
));
127 lua_setfield(L
, -2, "refresh_in");
128 lua_pushinteger(L
, atomic_load_explicit(&peer
->refresh_out
,
129 memory_order_relaxed
));
130 lua_setfield(L
, -2, "refresh_out");
131 lua_pushinteger(L
, atomic_load_explicit(&peer
->dynamic_cap_in
,
132 memory_order_relaxed
));
133 lua_setfield(L
, -2, "dynamic_cap_in");
134 lua_pushinteger(L
, atomic_load_explicit(&peer
->dynamic_cap_out
,
135 memory_order_relaxed
));
136 lua_setfield(L
, -2, "dynamic_cap_out");
137 lua_pushinteger(L
, peer
->established
);
138 lua_setfield(L
, -2, "times_established");
139 lua_pushinteger(L
, peer
->dropped
);
140 lua_setfield(L
, -2, "times_dropped");
142 lua_setfield(L
, -2, "stats");
145 static void lua_pushattr(lua_State
*L
, const struct attr
*attr
)
148 lua_pushinteger(L
, attr
->med
);
149 lua_setfield(L
, -2, "metric");
150 lua_pushinteger(L
, attr
->nh_ifindex
);
151 lua_setfield(L
, -2, "ifindex");
152 lua_pushstring(L
, attr
->aspath
->str
);
153 lua_setfield(L
, -2, "aspath");
154 lua_pushinteger(L
, attr
->local_pref
);
155 lua_setfield(L
, -2, "localpref");
158 static void *lua_toattr(lua_State
*L
, int idx
)
160 struct attr
*attr
= XCALLOC(MTYPE_TMP
, sizeof(struct attr
));
162 lua_getfield(L
, -1, "metric");
163 attr
->med
= lua_tointeger(L
, -1);
165 lua_getfield(L
, -1, "ifindex");
166 attr
->nh_ifindex
= lua_tointeger(L
, -1);
168 lua_getfield(L
, -1, "aspath");
169 attr
->aspath
= aspath_str2aspath(lua_tostring(L
, -1));
171 lua_getfield(L
, -1, "localpref");
172 attr
->local_pref
= lua_tointeger(L
, -1);
178 struct frrscript_codec frrscript_codecs_bgpd
[] = {
180 .encoder
= (encoder_func
)lua_pushpeer
,
183 .encoder
= (encoder_func
)lua_pushattr
,
184 .decoder
= lua_toattr
},
187 void bgp_script_init(void)
189 frrscript_register_type_codecs(frrscript_codecs_bgpd
);
192 #endif /* HAVE_SCRIPTING */