]>
Commit | Line | Data |
---|---|---|
5f98c815 QY |
1 | /* 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 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 | #ifndef __FRRSCRIPT_H__ | |
20 | #define __FRRSCRIPT_H__ | |
21 | ||
9e47ee98 | 22 | #include <lua.h> |
5f98c815 QY |
23 | #include "frrlua.h" |
24 | ||
25 | #ifdef __cplusplus | |
26 | extern "C" { | |
27 | #endif | |
28 | ||
29 | #define FRRSCRIPT_PATH "/etc/frr/scripts" | |
30 | ||
47dd8736 | 31 | typedef void (*encoder_func)(lua_State *, const void *); |
f869ab17 QY |
32 | typedef void *(*decoder_func)(lua_State *, int); |
33 | ||
34 | struct frrscript_codec { | |
35 | const char *typename; | |
36 | encoder_func encoder; | |
37 | decoder_func decoder; | |
38 | }; | |
f944ec67 | 39 | |
5f98c815 QY |
40 | struct frrscript { |
41 | /* Script name */ | |
42 | char *name; | |
43 | ||
44 | /* Lua state */ | |
45 | struct lua_State *L; | |
46 | }; | |
47 | ||
f869ab17 QY |
48 | struct frrscript_env { |
49 | /* Value type */ | |
50 | const char *typename; | |
51 | ||
52 | /* Binding name */ | |
53 | const char *name; | |
54 | ||
55 | /* Value */ | |
56 | const void *val; | |
57 | }; | |
5f98c815 QY |
58 | |
59 | /* | |
60 | * Create new FRR script. | |
61 | */ | |
62 | struct frrscript *frrscript_load(const char *name, | |
63 | int (*load_cb)(struct frrscript *)); | |
64 | ||
65 | /* | |
66 | * Destroy FRR script. | |
67 | */ | |
68 | void frrscript_unload(struct frrscript *fs); | |
69 | ||
70 | /* | |
f869ab17 | 71 | * Register a Lua codec for a type. |
5f98c815 QY |
72 | * |
73 | * tname | |
74 | * Name of type; e.g., "peer", "ospf_interface", etc. Chosen at will. | |
75 | * | |
f869ab17 QY |
76 | * codec(s) |
77 | * Function pointer to codec struct. Encoder function should push a Lua | |
5f98c815 | 78 | * table representing the passed argument - which will have the C type |
f869ab17 QY |
79 | * associated with the chosen 'tname' to the provided stack. The decoder |
80 | * function should pop a value from the top of the stack and return a heap | |
81 | * chunk containing that value. Allocations should be made with MTYPE_TMP. | |
82 | * | |
83 | * If using the plural function variant, pass a NULL-terminated array. | |
5f98c815 QY |
84 | * |
85 | */ | |
f869ab17 QY |
86 | void frrscript_register_type_codec(struct frrscript_codec *codec); |
87 | void frrscript_register_type_codecs(struct frrscript_codec *codecs); | |
5f98c815 QY |
88 | |
89 | /* | |
90 | * Initialize scripting subsystem. Call this before anything else. | |
91 | */ | |
92 | void frrscript_init(void); | |
93 | ||
5f98c815 QY |
94 | |
95 | /* | |
f869ab17 | 96 | * Call script. |
3b002f19 | 97 | * |
f869ab17 QY |
98 | * fs |
99 | * The script to call; this is obtained from frrscript_load(). | |
3b002f19 | 100 | * |
f869ab17 QY |
101 | * env |
102 | * The script's environment. Specify this as an array of frrscript_env. | |
3b002f19 | 103 | * |
f869ab17 QY |
104 | * Returns: |
105 | * 0 if the script ran successfully, nonzero otherwise. | |
3b002f19 | 106 | */ |
f869ab17 QY |
107 | int frrscript_call(struct frrscript *fs, struct frrscript_env *env); |
108 | ||
3b002f19 QY |
109 | |
110 | /* | |
f869ab17 | 111 | * Get result from finished script. |
3b002f19 | 112 | * |
f869ab17 QY |
113 | * fs |
114 | * The script. This script must have been run already. | |
3b002f19 | 115 | * |
f869ab17 QY |
116 | * result |
117 | * The result to extract from the script. | |
118 | * This reuses the frrscript_env type, but only the typename and name fields | |
119 | * need to be set. The value is returned directly. | |
3b002f19 | 120 | * |
f869ab17 QY |
121 | * Returns: |
122 | * The script result of the specified name and type, or NULL. | |
3b002f19 | 123 | */ |
f869ab17 QY |
124 | void *frrscript_get_result(struct frrscript *fs, |
125 | const struct frrscript_env *result); | |
3b002f19 | 126 | |
5f98c815 QY |
127 | #ifdef __cplusplus |
128 | } | |
129 | #endif /* __cplusplus */ | |
130 | ||
131 | #endif /* __FRRSCRIPT_H__ */ |