]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_script.c
Merge pull request #11819 from sworleys/TC_enum_fix
[mirror_frr.git] / zebra / zebra_script.c
1 /*
2 * frrscript encoders and decoders for data structures in Zebra
3 * Copyright (C) 2021 Donald Lee
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "zebra.h"
21
22 #include "zebra_script.h"
23
24 #ifdef HAVE_SCRIPTING
25
26 void zebra_script_init(void)
27 {
28 frrscript_names_add_function_name(ZEBRA_ON_RIB_PROCESS_HOOK_CALL);
29 }
30
31 void zebra_script_destroy(void)
32 {
33 frrscript_names_destroy();
34 }
35
36 void lua_pushnh_grp(lua_State *L, const struct nh_grp *nh_grp)
37 {
38 lua_newtable(L);
39 lua_pushinteger(L, nh_grp->id);
40 lua_setfield(L, -2, "id");
41 lua_pushinteger(L, nh_grp->weight);
42 lua_setfield(L, -2, "weight");
43 }
44
45 void lua_pushzebra_dplane_ctx(lua_State *L, const struct zebra_dplane_ctx *ctx)
46 {
47
48 lua_newtable(L);
49 lua_pushinteger(L, dplane_ctx_get_op(ctx));
50 lua_setfield(L, -2, "zd_op");
51 lua_pushinteger(L, dplane_ctx_get_status(ctx));
52 lua_setfield(L, -2, "zd_status");
53 lua_pushinteger(L, dplane_ctx_get_provider(ctx));
54 lua_setfield(L, -2, "zd_provider");
55 lua_pushinteger(L, dplane_ctx_get_vrf(ctx));
56 lua_setfield(L, -2, "zd_vrf_id");
57 lua_pushinteger(L, dplane_ctx_get_table(ctx));
58 lua_setfield(L, -2, "zd_table_id");
59 lua_pushstring(L, dplane_ctx_get_ifname(ctx));
60 lua_setfield(L, -2, "zd_ifname");
61 lua_pushinteger(L, dplane_ctx_get_ifindex(ctx));
62 lua_setfield(L, -2, "zd_ifindex");
63
64 switch (dplane_ctx_get_op(ctx)) {
65 case DPLANE_OP_ROUTE_INSTALL:
66 case DPLANE_OP_ROUTE_UPDATE:
67 case DPLANE_OP_ROUTE_DELETE:
68 case DPLANE_OP_ROUTE_NOTIFY:
69 case DPLANE_OP_NH_INSTALL:
70 case DPLANE_OP_NH_UPDATE:
71 case DPLANE_OP_NH_DELETE:
72 /* rinfo */
73 lua_newtable(L);
74 {
75 lua_pushprefix(L, dplane_ctx_get_dest(ctx));
76 lua_setfield(L, -2, "zd_dest");
77 const struct prefix *src_pfx = dplane_ctx_get_src(ctx);
78
79 if (src_pfx) {
80 lua_pushprefix(L, src_pfx);
81 lua_setfield(L, -2, "zd_src");
82 }
83 lua_pushinteger(L, dplane_ctx_get_afi(ctx));
84 lua_setfield(L, -2, "zd_afi");
85 lua_pushinteger(L, dplane_ctx_get_safi(ctx));
86 lua_setfield(L, -2, "zd_safi");
87 lua_pushinteger(L, dplane_ctx_get_type(ctx));
88 lua_setfield(L, -2, "zd_type");
89 lua_pushinteger(L, dplane_ctx_get_old_type(ctx));
90 lua_setfield(L, -2, "zd_old_type");
91 lua_pushinteger(L, dplane_ctx_get_tag(ctx));
92 lua_setfield(L, -2, "zd_tag");
93 lua_pushinteger(L, dplane_ctx_get_old_tag(ctx));
94 lua_setfield(L, -2, "zd_old_tag");
95 lua_pushinteger(L, dplane_ctx_get_metric(ctx));
96 lua_setfield(L, -2, "zd_metric");
97 lua_pushinteger(L, dplane_ctx_get_old_metric(ctx));
98 lua_setfield(L, -2, "zd_old_metric");
99 lua_pushinteger(L, dplane_ctx_get_instance(ctx));
100 lua_setfield(L, -2, "zd_instance");
101 lua_pushinteger(L, dplane_ctx_get_old_instance(ctx));
102 lua_setfield(L, -2, "zd_old_instance");
103 lua_pushinteger(L, dplane_ctx_get_distance(ctx));
104 lua_setfield(L, -2, "zd_distance");
105 lua_pushinteger(L, dplane_ctx_get_old_distance(ctx));
106 lua_setfield(L, -2, "zd_old_distance");
107 lua_pushinteger(L, dplane_ctx_get_mtu(ctx));
108 lua_setfield(L, -2, "zd_mtu");
109 lua_pushinteger(L, dplane_ctx_get_nh_mtu(ctx));
110 lua_setfield(L, -2, "zd_nexthop_mtu");
111 /* nhe */
112 lua_newtable(L);
113 {
114 lua_pushinteger(L, dplane_ctx_get_nhe_id(ctx));
115 lua_setfield(L, -2, "id");
116 lua_pushinteger(L,
117 dplane_ctx_get_old_nhe_id(ctx));
118 lua_setfield(L, -2, "old_id");
119 lua_pushinteger(L, dplane_ctx_get_nhe_afi(ctx));
120 lua_setfield(L, -2, "afi");
121 lua_pushinteger(L,
122 dplane_ctx_get_nhe_vrf_id(ctx));
123 lua_setfield(L, -2, "vrf_id");
124 lua_pushinteger(L,
125 dplane_ctx_get_nhe_type(ctx));
126 lua_setfield(L, -2, "type");
127 lua_pushnexthop_group(
128 L, dplane_ctx_get_nhe_ng(ctx));
129 lua_setfield(L, -2, "ng");
130 lua_pushnh_grp(L,
131 dplane_ctx_get_nhe_nh_grp(ctx));
132 lua_setfield(L, -2, "nh_grp");
133 lua_pushinteger(
134 L,
135 dplane_ctx_get_nhe_nh_grp_count(ctx));
136 lua_setfield(L, -2, "nh_grp_count");
137 }
138 lua_setfield(L, -2, "nhe");
139 lua_pushinteger(L, dplane_ctx_get_nhg_id(ctx));
140 lua_setfield(L, -2, "zd_nhg_id");
141 lua_pushnexthop_group(L, dplane_ctx_get_ng(ctx));
142 lua_setfield(L, -2, "zd_ng");
143 lua_pushnexthop_group(L, dplane_ctx_get_backup_ng(ctx));
144 lua_setfield(L, -2, "backup_ng");
145 lua_pushnexthop_group(L, dplane_ctx_get_old_ng(ctx));
146 lua_setfield(L, -2, "zd_old_ng");
147 lua_pushnexthop_group(
148 L, dplane_ctx_get_old_backup_ng(ctx));
149 lua_setfield(L, -2, "old_backup_ng");
150 }
151 lua_setfield(L, -2, "rinfo");
152 break;
153 case DPLANE_OP_LSP_INSTALL:
154 case DPLANE_OP_LSP_UPDATE:
155 case DPLANE_OP_LSP_DELETE:
156 case DPLANE_OP_LSP_NOTIFY:
157 lua_pushinteger(L, (int)dplane_ctx_get_in_label(ctx));
158 lua_setfield(L, -2, "label");
159 break;
160 case DPLANE_OP_PW_INSTALL:
161 case DPLANE_OP_PW_UNINSTALL:
162 /* pw*/
163 lua_newtable(L);
164 {
165 lua_pushinteger(L, dplane_ctx_get_pw_type(ctx));
166 lua_setfield(L, -2, "type");
167 lua_pushinteger(L, dplane_ctx_get_pw_af(ctx));
168 lua_setfield(L, -2, "af");
169 lua_pushinteger(L, dplane_ctx_get_pw_status(ctx));
170 lua_setfield(L, -2, "status");
171 lua_pushinteger(L, dplane_ctx_get_pw_flags(ctx));
172 lua_setfield(L, -2, "flags");
173 lua_pushinteger(L, dplane_ctx_get_pw_local_label(ctx));
174 lua_setfield(L, -2, "local_label");
175 lua_pushinteger(L, dplane_ctx_get_pw_remote_label(ctx));
176 lua_setfield(L, -2, "remote_label");
177 }
178 lua_setfield(L, -2, "pw");
179 break;
180 case DPLANE_OP_SYS_ROUTE_ADD:
181 case DPLANE_OP_SYS_ROUTE_DELETE:
182 /* nothing to encode */
183 break;
184 case DPLANE_OP_MAC_INSTALL:
185 case DPLANE_OP_MAC_DELETE:
186 /* macinfo */
187 lua_newtable(L);
188 {
189 lua_pushinteger(L, dplane_ctx_mac_get_vlan(ctx));
190 lua_setfield(L, -2, "vid");
191 lua_pushinteger(L, dplane_ctx_mac_get_br_ifindex(ctx));
192 lua_setfield(L, -2, "br_ifindex");
193 lua_pushethaddr(L, dplane_ctx_mac_get_addr(ctx));
194 lua_setfield(L, -2, "mac");
195 lua_pushinaddr(L, dplane_ctx_mac_get_vtep_ip(ctx));
196 lua_setfield(L, -2, "vtep_ip");
197 lua_pushinteger(L, dplane_ctx_mac_is_sticky(ctx));
198 lua_setfield(L, -2, "is_sticky");
199 lua_pushinteger(L, dplane_ctx_mac_get_nhg_id(ctx));
200 lua_setfield(L, -2, "nhg_id");
201 lua_pushinteger(L,
202 dplane_ctx_mac_get_update_flags(ctx));
203 lua_setfield(L, -2, "update_flags");
204 }
205 lua_setfield(L, -2, "macinfo");
206 break;
207 case DPLANE_OP_RULE_ADD:
208 case DPLANE_OP_RULE_DELETE:
209 case DPLANE_OP_RULE_UPDATE:
210 /* rule */
211 lua_newtable(L);
212 {
213 lua_pushinteger(L, dplane_ctx_rule_get_sock(ctx));
214 lua_setfield(L, -2, "sock");
215 lua_pushinteger(L, dplane_ctx_rule_get_unique(ctx));
216 lua_setfield(L, -2, "unique");
217 lua_pushinteger(L, dplane_ctx_rule_get_seq(ctx));
218 lua_setfield(L, -2, "seq");
219 lua_pushstring(L, dplane_ctx_rule_get_ifname(ctx));
220 lua_setfield(L, -2, "ifname");
221 lua_pushinteger(L, dplane_ctx_rule_get_priority(ctx));
222 lua_setfield(L, -2, "priority");
223 lua_pushinteger(L,
224 dplane_ctx_rule_get_old_priority(ctx));
225 lua_setfield(L, -2, "old_priority");
226 lua_pushinteger(L, dplane_ctx_rule_get_table(ctx));
227 lua_setfield(L, -2, "table");
228 lua_pushinteger(L, dplane_ctx_rule_get_old_table(ctx));
229 lua_setfield(L, -2, "old_table");
230 lua_pushinteger(L, dplane_ctx_rule_get_filter_bm(ctx));
231 lua_setfield(L, -2, "filter_bm");
232 lua_pushinteger(L,
233 dplane_ctx_rule_get_old_filter_bm(ctx));
234 lua_setfield(L, -2, "old_filter_bm");
235 lua_pushinteger(L, dplane_ctx_rule_get_fwmark(ctx));
236 lua_setfield(L, -2, "fwmark");
237 lua_pushinteger(L, dplane_ctx_rule_get_old_fwmark(ctx));
238 lua_setfield(L, -2, "old_fwmark");
239 lua_pushinteger(L, dplane_ctx_rule_get_dsfield(ctx));
240 lua_setfield(L, -2, "dsfield");
241 lua_pushinteger(L,
242 dplane_ctx_rule_get_old_dsfield(ctx));
243 lua_setfield(L, -2, "old_dsfield");
244 lua_pushinteger(L, dplane_ctx_rule_get_ipproto(ctx));
245 lua_setfield(L, -2, "ip_proto");
246 lua_pushinteger(L,
247 dplane_ctx_rule_get_old_ipproto(ctx));
248 lua_setfield(L, -2, "old_ip_proto");
249 lua_pushprefix(L, dplane_ctx_rule_get_src_ip(ctx));
250 lua_setfield(L, -2, "src_ip");
251 lua_pushprefix(L, dplane_ctx_rule_get_old_src_ip(ctx));
252 lua_setfield(L, -2, "old_src_ip");
253 lua_pushprefix(L, dplane_ctx_rule_get_dst_ip(ctx));
254 lua_setfield(L, -2, "dst_ip");
255 lua_pushprefix(L, dplane_ctx_rule_get_old_dst_ip(ctx));
256 lua_setfield(L, -2, "old_dst_ip");
257 }
258 lua_setfield(L, -2, "rule");
259 break;
260 case DPLANE_OP_IPTABLE_ADD:
261 case DPLANE_OP_IPTABLE_DELETE: {
262 struct zebra_pbr_iptable iptable;
263
264 dplane_ctx_get_pbr_iptable(ctx, &iptable);
265 /* iptable */
266 lua_newtable(L);
267 {
268 lua_pushinteger(L, iptable.sock);
269 lua_setfield(L, -2, "sock");
270 lua_pushinteger(L, iptable.vrf_id);
271 lua_setfield(L, -2, "vrf_id");
272 lua_pushinteger(L, iptable.unique);
273 lua_setfield(L, -2, "unique");
274 lua_pushinteger(L, iptable.type);
275 lua_setfield(L, -2, "type");
276 lua_pushinteger(L, iptable.filter_bm);
277 lua_setfield(L, -2, "filter_bm");
278 lua_pushinteger(L, iptable.fwmark);
279 lua_setfield(L, -2, "fwmark");
280 lua_pushinteger(L, iptable.action);
281 lua_setfield(L, -2, "action");
282 lua_pushinteger(L, iptable.pkt_len_min);
283 lua_setfield(L, -2, "pkt_len_min");
284 lua_pushinteger(L, iptable.pkt_len_max);
285 lua_setfield(L, -2, "pkt_len_max");
286 lua_pushinteger(L, iptable.tcp_flags);
287 lua_setfield(L, -2, "tcp_flags");
288 lua_pushinteger(L, iptable.dscp_value);
289 lua_setfield(L, -2, "dscp_value");
290 lua_pushinteger(L, iptable.fragment);
291 lua_setfield(L, -2, "fragment");
292 lua_pushinteger(L, iptable.protocol);
293 lua_setfield(L, -2, "protocol");
294 lua_pushinteger(L, iptable.nb_interface);
295 lua_setfield(L, -2, "nb_interface");
296 lua_pushinteger(L, iptable.flow_label);
297 lua_setfield(L, -2, "flow_label");
298 lua_pushinteger(L, iptable.family);
299 lua_setfield(L, -2, "family");
300 lua_pushstring(L, iptable.ipset_name);
301 lua_setfield(L, -2, "ipset_name");
302 }
303 lua_setfield(L, -2, "iptable");
304 break;
305 }
306 case DPLANE_OP_IPSET_ADD:
307 case DPLANE_OP_IPSET_DELETE:
308 case DPLANE_OP_IPSET_ENTRY_ADD:
309 case DPLANE_OP_IPSET_ENTRY_DELETE: {
310 struct zebra_pbr_ipset ipset;
311
312 dplane_ctx_get_pbr_ipset(ctx, &ipset);
313 /* ipset */
314 lua_newtable(L);
315 {
316 lua_pushinteger(L, ipset.sock);
317 lua_setfield(L, -2, "sock");
318 lua_pushinteger(L, ipset.vrf_id);
319 lua_setfield(L, -2, "vrf_id");
320 lua_pushinteger(L, ipset.unique);
321 lua_setfield(L, -2, "unique");
322 lua_pushinteger(L, ipset.type);
323 lua_setfield(L, -2, "type");
324 lua_pushinteger(L, ipset.family);
325 lua_setfield(L, -2, "family");
326 lua_pushstring(L, ipset.ipset_name);
327 lua_setfield(L, -2, "ipset_name");
328 }
329 lua_setfield(L, -2, "ipset");
330 break;
331 }
332 case DPLANE_OP_NEIGH_INSTALL:
333 case DPLANE_OP_NEIGH_UPDATE:
334 case DPLANE_OP_NEIGH_DELETE:
335 case DPLANE_OP_NEIGH_DISCOVER:
336 case DPLANE_OP_NEIGH_IP_INSTALL:
337 case DPLANE_OP_NEIGH_IP_DELETE:
338 /* neigh */
339 lua_newtable(L);
340 {
341 lua_pushipaddr(L, dplane_ctx_neigh_get_ipaddr(ctx));
342 lua_setfield(L, -2, "ip_addr");
343 /* link */
344 lua_newtable(L);
345 {
346 lua_pushethaddr(L,
347 dplane_ctx_neigh_get_mac(ctx));
348 lua_setfield(L, -2, "mac");
349 lua_pushipaddr(
350 L, dplane_ctx_neigh_get_link_ip(ctx));
351 lua_setfield(L, -2, "ip_addr");
352 }
353 lua_setfield(L, -2, "link");
354 lua_pushinteger(L, dplane_ctx_neigh_get_flags(ctx));
355 lua_setfield(L, -2, "flags");
356 lua_pushinteger(L, dplane_ctx_neigh_get_state(ctx));
357 lua_setfield(L, -2, "state");
358 lua_pushinteger(L,
359 dplane_ctx_neigh_get_update_flags(ctx));
360 lua_setfield(L, -2, "update_flags");
361 }
362 lua_setfield(L, -2, "neigh");
363 break;
364 case DPLANE_OP_VTEP_ADD:
365 case DPLANE_OP_VTEP_DELETE:
366 break;
367 case DPLANE_OP_BR_PORT_UPDATE:
368 /* br_port */
369 lua_newtable(L);
370 {
371 lua_pushinteger(
372 L, dplane_ctx_get_br_port_sph_filter_cnt(ctx));
373 lua_setfield(L, -2, "sph_filter_cnt");
374 lua_pushinteger(L, dplane_ctx_get_br_port_flags(ctx));
375 lua_setfield(L, -2, "flags");
376 lua_pushinteger(
377 L, dplane_ctx_get_br_port_backup_nhg_id(ctx));
378 lua_setfield(L, -2, "backup_nhg_id");
379 }
380 lua_setfield(L, -2, "br_port");
381 break;
382 case DPLANE_OP_NEIGH_TABLE_UPDATE:
383 /* neightable */
384 lua_newtable(L);
385 {
386 lua_pushinteger(L,
387 dplane_ctx_neightable_get_family(ctx));
388 lua_setfield(L, -2, "family");
389 lua_pushinteger(
390 L, dplane_ctx_neightable_get_app_probes(ctx));
391 lua_setfield(L, -2, "app_probes");
392 lua_pushinteger(
393 L, dplane_ctx_neightable_get_mcast_probes(ctx));
394 lua_setfield(L, -2, "ucast_probes");
395 lua_pushinteger(
396 L, dplane_ctx_neightable_get_ucast_probes(ctx));
397 lua_setfield(L, -2, "mcast_probes");
398 }
399 lua_setfield(L, -2, "neightable");
400 break;
401 case DPLANE_OP_GRE_SET:
402 /* gre */
403 lua_newtable(L);
404 {
405 lua_pushinteger(L,
406 dplane_ctx_gre_get_link_ifindex(ctx));
407 lua_setfield(L, -2, "link_ifindex");
408 lua_pushinteger(L, dplane_ctx_gre_get_mtu(ctx));
409 lua_setfield(L, -2, "mtu");
410 }
411 lua_setfield(L, -2, "gre");
412
413 case DPLANE_OP_ADDR_INSTALL:
414 case DPLANE_OP_ADDR_UNINSTALL:
415 case DPLANE_OP_INTF_ADDR_ADD:
416 case DPLANE_OP_INTF_ADDR_DEL:
417 case DPLANE_OP_INTF_INSTALL:
418 case DPLANE_OP_INTF_UPDATE:
419 case DPLANE_OP_INTF_DELETE:
420 case DPLANE_OP_TC_INSTALL:
421 case DPLANE_OP_TC_UPDATE:
422 case DPLANE_OP_TC_DELETE:
423 /* Not currently handled */
424 case DPLANE_OP_INTF_NETCONFIG: /*NYI*/
425 case DPLANE_OP_NONE:
426 break;
427 } /* Dispatch by op code */
428 }
429
430 #endif /* HAVE_SCRIPTING */