]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_script.c
zebra: Do not explicitly set the thread pointer to NULL
[mirror_frr.git] / bgpd / bgp_script.c
1 /* BGP scripting foo
2 * Copyright (C) 2020 NVIDIA Corporation
3 * Quentin Young
4 *
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.
9 *
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.
14 *
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,
18 * MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #ifdef HAVE_SCRIPTING
24
25 #include "bgpd.h"
26 #include "bgp_script.h"
27 #include "bgp_debug.h"
28 #include "bgp_aspath.h"
29 #include "frratomic.h"
30 #include "frrscript.h"
31
32 void lua_pushpeer(lua_State *L, const struct peer *peer)
33 {
34 lua_newtable(L);
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");
63
64 /* Nested tables here */
65 lua_newtable(L);
66 {
67 lua_newtable(L);
68 {
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");
77 }
78 lua_setfield(L, -2, "configured");
79
80 lua_newtable(L);
81 {
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");
90 }
91 lua_setfield(L, -2, "negotiated");
92 }
93 lua_setfield(L, -2, "timers");
94
95 lua_newtable(L);
96 {
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");
140 }
141 lua_setfield(L, -2, "stats");
142 }
143
144 void lua_pushattr(lua_State *L, const struct attr *attr)
145 {
146 lua_newtable(L);
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");
155 }
156
157 void lua_decode_attr(lua_State *L, int idx, struct attr *attr)
158 {
159 lua_getfield(L, -1, "metric");
160 attr->med = lua_tointeger(L, -1);
161 lua_pop(L, 1);
162 lua_getfield(L, -1, "ifindex");
163 attr->nh_ifindex = lua_tointeger(L, -1);
164 lua_pop(L, 1);
165 lua_getfield(L, -1, "aspath");
166 attr->aspath = aspath_str2aspath(lua_tostring(L, -1));
167 lua_pop(L, 1);
168 lua_getfield(L, -1, "localpref");
169 attr->local_pref = lua_tointeger(L, -1);
170 lua_pop(L, 1);
171 }
172
173 void *lua_toattr(lua_State *L, int idx)
174 {
175 struct attr *attr = XCALLOC(MTYPE_TMP, sizeof(struct attr));
176
177 lua_decode_attr(L, idx, attr);
178 return attr;
179 }
180
181 struct frrscript_codec frrscript_codecs_bgpd[] = {
182 {.typename = "peer",
183 .encoder = (encoder_func)lua_pushpeer,
184 .decoder = NULL},
185 {.typename = "attr",
186 .encoder = (encoder_func)lua_pushattr,
187 .decoder = lua_toattr},
188 {}};
189
190 void bgp_script_init(void)
191 {
192 frrscript_register_type_codecs(frrscript_codecs_bgpd);
193 }
194
195 #endif /* HAVE_SCRIPTING */