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