1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#include "nest/bird.h"
#include "conf/conf.h"
#include "filter/filter.h"
#include "lua.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
/* Docs: http://pgl.yoyo.org/luai/i/luaL_dostring */
static lua_State *global_lua_state = NULL;
static inline lua_State * luaB_getstate(void) {
if (!global_lua_state) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
global_lua_state = L;
}
return lua_newthread(global_lua_state);
}
static inline void luaB_close(lua_State *L UNUSED) {
lua_pop(global_lua_state, 1);
}
struct lua_new_filter_writer_data {
struct lua_filter_chunk *first, *last;
};
static int lua_new_filter_writer(lua_State *L UNUSED, const void *p, size_t sz, void *ud) {
struct lua_new_filter_writer_data *d = ud;
struct lua_filter_chunk *cur = cfg_allocz(sizeof(struct lua_filter_chunk));
cur->size = sz;
cur->chunk = cfg_alloc(sz);
memcpy(cur->chunk, p, sz);
if (d->last)
d->last = d->last->next = cur;
else
d->last = d->first = cur;
return 0;
}
struct filter * lua_new_filter(struct f_inst *inst) {
struct filter *f = cfg_alloc(sizeof(struct filter));
f->name = NULL;
f->type = FILTER_LUA;
struct f_val string = f_eval(inst, cfg_mem);
if (string.type != T_STRING) {
cf_error("Lua filter must be a string");
return NULL;
}
lua_State *L = luaB_getstate();
int loadres = luaL_loadstring(L, string.val.s);
switch (loadres) {
case LUA_ERRMEM:
luaB_close(L);
cf_error("Memory allocation error occured when loading lua chunk");
return NULL;
case LUA_ERRSYNTAX:
{
const char *e = lua_tostring(L, -1);
char *ec = cfg_alloc(strlen(e) + 1);
strcpy(ec, e);
luaB_close(L);
cf_error("Lua syntax error: %s", ec);
return NULL;
}
case 0: /* Everything OK */
break;
}
struct lua_new_filter_writer_data lnfwd = {};
lua_dump(L, lua_new_filter_writer, &lnfwd, 0); /* No error to handle */
luaB_close(L);
f->lua_chunk = lnfwd.first;
return f;
}
static const char *lua_interpret_reader(lua_State *L UNUSED, void *ud, size_t *sz) {
struct lua_filter_chunk **cptr = ud;
if ((*cptr) == NULL)
return NULL;
*sz = (*cptr)->size;
void *out = (*cptr)->chunk;
*cptr = (*cptr)->next;
return out;
}
static inline void f_rte_cow(struct rte **e, struct rta **a)
{
if (!((*e)->flags & REF_COW))
return;
*e = rte_do_cow(*e);
}
static void
f_rta_cow(struct rte **e, struct rta **old_rta, struct linpool *lp)
{
if (!rta_is_cached((*e)->attrs))
return;
/* Prepare to modify rte */
f_rte_cow(e, old_rta);
/* Store old rta to free it later, it stores reference from rte_cow() */
*old_rta = (*e)->attrs;
/*
* Get shallow copy of rta. Fields eattrs and nexthops of rta are shared
* with f_old_rta (they will be copied when the cached rta will be obtained
* at the end of f_run()), also the lock of hostentry is inherited (we
* suppose hostentry is not changed by filters).
*/
(*e)->attrs = rta_do_cow((*e)->attrs, lp);
/* Re-cache the ea_list */
/* f_cache_eattrs(); */
}
struct f_val lua_interpret(struct lua_filter_chunk *chunk, struct rte **e, struct rta **a, struct ea_list **ea UNUSED, struct linpool *lp, int flags UNUSED) {
f_rta_cow(e, a, lp);
lua_State *L = luaB_getstate();
lua_bird_state *lbs = luaB_init(L, lp);
luaB_push_route(L, *e);
luaB_push_eattrs(L, *ea);
struct lua_filter_chunk **rptr = &chunk;
lua_load(L, lua_interpret_reader, rptr, "", "b");
int le = lua_pcall(L, 0, LUA_MULTRET, 0);
struct f_val out = F_VAL_VOID;
if (le && lbs->exception) {
out = F_VAL(T_RETURN, i, lbs->exception);
} else if (le) {
log(L_ERR "bad lua: %s", lua_tostring(L, -1));
out = F_VAL(T_RETURN, i, F_ERROR);
} else if (lua_isnumber(L, -1)) {
out = F_VAL(T_INT, i, lua_tonumber(L, -1));
} else {
log(L_WARN "lua return value is not a number (unimplemented): %s", lua_tostring(L, -1));
out = F_VAL(T_RETURN, i, F_ERROR);
}
luaB_close(L);
return out;
}
int lua_filter_same(struct lua_filter_chunk *new, struct lua_filter_chunk *old) {
size_t npos = 0, opos = 0;
while (new && old) {
size_t nrem = new->size - npos;
size_t orem = old->size - opos;
size_t rem = MIN(nrem, orem);
if (memcmp(new->chunk + npos, old->chunk + opos, rem))
return 0;
npos += rem;
opos += rem;
if (npos == new->size) {
new = new->next;
npos = 0;
}
if (opos == old->size) {
old = old->next;
opos = 0;
}
}
if (!new && !old)
return 1;
else
return 0;
}
uint lua_eval(struct f_inst *inst)
{
struct f_val string = f_eval(inst, cfg_mem);
if (string.type != T_STRING) {
cf_error("Lua filter must be a string");
return -1;
}
lua_State *L = luaB_getstate();
int dores = luaL_dostring(L, string.val.s);
log(L_WARN "lua_eval dores '%s' %d", string.val.s, dores);
switch (dores) {
case LUA_ERRMEM:
luaB_close(L);
cf_error("Memory allocation error occured when loading lua chunk");
return -1;
case LUA_ERRSYNTAX:
{
const char *e = lua_tostring(L, -1);
char *ec = cfg_alloc(strlen(e) + 1);
strcpy(ec, e);
luaB_close(L);
cf_error("Lua syntax error: %s", ec);
return -1;
}
case 0: /* Everything OK */
break;
}
luaB_close(L);
return 0;
}
|