]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frrscript.h
2 * Copyright (C) 2020 NVIDIA Corporation
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
19 #ifndef __FRRSCRIPT_H__
20 #define __FRRSCRIPT_H__
28 #include "../bgpd/bgp_script.h"
34 typedef void (*encoder_func
)(lua_State
*, const void *);
35 typedef void *(*decoder_func
)(lua_State
*, int);
37 struct frrscript_codec
{
51 struct frrscript_env
{
63 * Create new FRR script.
65 struct frrscript
*frrscript_load(const char *name
,
66 int (*load_cb
)(struct frrscript
*));
71 void frrscript_unload(struct frrscript
*fs
);
74 * Register a Lua codec for a type.
77 * Name of type; e.g., "peer", "ospf_interface", etc. Chosen at will.
80 * Function pointer to codec struct. Encoder function should push a Lua
81 * table representing the passed argument - which will have the C type
82 * associated with the chosen 'tname' to the provided stack. The decoder
83 * function should pop a value from the top of the stack and return a heap
84 * chunk containing that value. Allocations should be made with MTYPE_TMP.
86 * If using the plural function variant, pass a NULL-terminated array.
89 void frrscript_register_type_codec(struct frrscript_codec
*codec
);
90 void frrscript_register_type_codecs(struct frrscript_codec
*codecs
);
93 * Initialize scripting subsystem. Call this before anything else.
96 * Directory in which to look for scripts
98 void frrscript_init(const char *scriptdir
);
100 #define ENCODE_ARGS(name, value) \
102 ENCODE_ARGS_WITH_STATE(L, value); \
103 lua_setglobal(L, name); \
106 #define DECODE_ARGS(name, value) \
108 lua_getglobal(L, name); \
109 DECODE_ARGS_WITH_STATE(L, value); \
113 * Maps the type of value to its encoder/decoder.
114 * Add new mappings here.
119 * Directory in which to look for scripts
121 #define ENCODE_ARGS_WITH_STATE(L, value) \
123 long long * : lua_pushintegerp, \
124 struct prefix * : lua_pushprefix, \
125 struct interface * : lua_pushinterface, \
126 struct in_addr * : lua_pushinaddr, \
127 struct in6_addr * : lua_pushin6addr, \
128 union sockunion * : lua_pushsockunion, \
129 time_t * : lua_pushtimet, \
130 char * : lua_pushstring_wrapper, \
131 struct attr * : lua_pushattr, \
132 struct peer * : lua_pushpeer, \
133 const struct prefix * : lua_pushprefix \
136 #define DECODE_ARGS_WITH_STATE(L, value) \
138 long long * : lua_decode_integerp, \
139 struct prefix * : lua_decode_prefix, \
140 struct interface * : lua_decode_interface, \
141 struct in_addr * : lua_decode_inaddr, \
142 struct in6_addr * : lua_decode_in6addr, \
143 union sockunion * : lua_decode_sockunion, \
144 time_t * : lua_decode_timet, \
145 char * : lua_decode_stringp, \
146 struct attr * : lua_decode_attr, \
147 struct peer * : lua_decode_noop, \
148 const struct prefix * : lua_decode_noop \
155 * The script to call; this is obtained from frrscript_load().
158 * 0 if the script ran successfully, nonzero otherwise.
160 int _frrscript_call(struct frrscript
*fs
);
163 * Wrapper for call script. Maps values passed in to their encoder
167 * The script to call; this is obtained from frrscript_load().
170 * 0 if the script ran successfully, nonzero otherwise.
172 #define frrscript_call(fs, ...) \
174 lua_State *L = fs->L; \
175 MAP_LISTS(ENCODE_ARGS, ##__VA_ARGS__); \
176 int ret = _frrscript_call(fs); \
178 MAP_LISTS(DECODE_ARGS, ##__VA_ARGS__); \
184 * Get result from finished script.
187 * The script. This script must have been run already.
190 * The result to extract from the script.
191 * This reuses the frrscript_env type, but only the typename and name fields
192 * need to be set. The value is returned directly.
195 * The script result of the specified name and type, or NULL.
197 void *frrscript_get_result(struct frrscript
*fs
,
198 const struct frrscript_env
*result
);
202 #endif /* __cplusplus */
204 #endif /* HAVE_SCRIPTING */
206 #endif /* __FRRSCRIPT_H__ */