2 * frrscript encoders and decoders for data structures in Zebra
3 * Copyright (C) 2021 Donald Lee
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)
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
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
22 #include "zebra_script.h"
26 void zebra_script_init(void)
28 frrscript_names_add_function_name(ZEBRA_ON_RIB_PROCESS_HOOK_CALL
);
31 void zebra_script_destroy(void)
33 frrscript_names_destroy();
36 void lua_pushnh_grp(lua_State
*L
, const struct nh_grp
*nh_grp
)
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");
45 void lua_pushzebra_dplane_ctx(lua_State
*L
, const struct zebra_dplane_ctx
*ctx
)
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");
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
:
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
);
80 lua_pushprefix(L
, src_pfx
);
81 lua_setfield(L
, -2, "zd_src");
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");
114 lua_pushinteger(L
, dplane_ctx_get_nhe_id(ctx
));
115 lua_setfield(L
, -2, "id");
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");
122 dplane_ctx_get_nhe_vrf_id(ctx
));
123 lua_setfield(L
, -2, "vrf_id");
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");
131 dplane_ctx_get_nhe_nh_grp(ctx
));
132 lua_setfield(L
, -2, "nh_grp");
135 dplane_ctx_get_nhe_nh_grp_count(ctx
));
136 lua_setfield(L
, -2, "nh_grp_count");
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");
151 lua_setfield(L
, -2, "rinfo");
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");
160 case DPLANE_OP_PW_INSTALL
:
161 case DPLANE_OP_PW_UNINSTALL
:
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");
178 lua_setfield(L
, -2, "pw");
180 case DPLANE_OP_SYS_ROUTE_ADD
:
181 case DPLANE_OP_SYS_ROUTE_DELETE
:
182 /* nothing to encode */
184 case DPLANE_OP_MAC_INSTALL
:
185 case DPLANE_OP_MAC_DELETE
:
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");
202 dplane_ctx_mac_get_update_flags(ctx
));
203 lua_setfield(L
, -2, "update_flags");
205 lua_setfield(L
, -2, "macinfo");
207 case DPLANE_OP_RULE_ADD
:
208 case DPLANE_OP_RULE_DELETE
:
209 case DPLANE_OP_RULE_UPDATE
:
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");
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");
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");
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");
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");
258 lua_setfield(L
, -2, "rule");
260 case DPLANE_OP_IPTABLE_ADD
:
261 case DPLANE_OP_IPTABLE_DELETE
: {
262 struct zebra_pbr_iptable iptable
;
264 dplane_ctx_get_pbr_iptable(ctx
, &iptable
);
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");
303 lua_setfield(L
, -2, "iptable");
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
;
312 dplane_ctx_get_pbr_ipset(ctx
, &ipset
);
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");
329 lua_setfield(L
, -2, "ipset");
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
:
341 lua_pushipaddr(L
, dplane_ctx_neigh_get_ipaddr(ctx
));
342 lua_setfield(L
, -2, "ip_addr");
347 dplane_ctx_neigh_get_mac(ctx
));
348 lua_setfield(L
, -2, "mac");
350 L
, dplane_ctx_neigh_get_link_ip(ctx
));
351 lua_setfield(L
, -2, "ip_addr");
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");
359 dplane_ctx_neigh_get_update_flags(ctx
));
360 lua_setfield(L
, -2, "update_flags");
362 lua_setfield(L
, -2, "neigh");
364 case DPLANE_OP_VTEP_ADD
:
365 case DPLANE_OP_VTEP_DELETE
:
367 case DPLANE_OP_BR_PORT_UPDATE
:
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");
377 L
, dplane_ctx_get_br_port_backup_nhg_id(ctx
));
378 lua_setfield(L
, -2, "backup_nhg_id");
380 lua_setfield(L
, -2, "br_port");
382 case DPLANE_OP_NEIGH_TABLE_UPDATE
:
387 dplane_ctx_neightable_get_family(ctx
));
388 lua_setfield(L
, -2, "family");
390 L
, dplane_ctx_neightable_get_app_probes(ctx
));
391 lua_setfield(L
, -2, "app_probes");
393 L
, dplane_ctx_neightable_get_mcast_probes(ctx
));
394 lua_setfield(L
, -2, "ucast_probes");
396 L
, dplane_ctx_neightable_get_ucast_probes(ctx
));
397 lua_setfield(L
, -2, "mcast_probes");
399 lua_setfield(L
, -2, "neightable");
401 case DPLANE_OP_GRE_SET
:
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");
411 lua_setfield(L
, -2, "gre");
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*/
427 } /* Dispatch by op code */
430 #endif /* HAVE_SCRIPTING */