summaryrefslogtreecommitdiffstats
path: root/lib/frrscript.c
blob: 50410fb58bd66310c2a6d44a455fcd601c4716c8 (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// SPDX-License-Identifier: GPL-2.0-or-later
/* Scripting foo
 * Copyright (C) 2020  NVIDIA Corporation
 * Quentin Young
 */
#include <zebra.h>

#ifdef HAVE_SCRIPTING

#include <stdarg.h>
#include <lua.h>

#include "frrscript.h"
#include "frrlua.h"
#include "memory.h"
#include "hash.h"
#include "log.h"


DEFINE_MTYPE_STATIC(LIB, SCRIPT, "Scripting");

/*
 * Script name hash utilities
 */

struct frrscript_names_head frrscript_names_hash;

/*
 * Wrapper for frrscript_names_add
 * Use this to register hook calls when a daemon starts up
 */
int frrscript_names_add_function_name(const char *function_name)
{
	struct frrscript_names_entry *insert =
		XCALLOC(MTYPE_SCRIPT, sizeof(*insert));
	strlcpy(insert->function_name, function_name,
		sizeof(insert->function_name));

	if (frrscript_names_add(&frrscript_names_hash, insert)) {
		zlog_warn(
			"Failed to add hook call function name to script_names");
		return 1;
	}
	return 0;
}

void frrscript_names_destroy(void)
{
	struct frrscript_names_entry *ne;

	while ((ne = frrscript_names_pop(&frrscript_names_hash)))
		XFREE(MTYPE_SCRIPT, ne);
}

/*
 * Given a function_name, set its script_name. function_names and script_names
 * are one-to-one. Each set will wipe the previous script_name.
 * Return 0 if set was successful, else 1.
 *
 * script_name is the base name of the file, without .lua.
 */
int frrscript_names_set_script_name(const char *function_name,
				    const char *script_name)
{
	struct frrscript_names_entry lookup;

	strlcpy(lookup.function_name, function_name,
		sizeof(lookup.function_name));
	struct frrscript_names_entry *snhe =
		frrscript_names_find(&frrscript_names_hash, &lookup);
	if (!snhe)
		return 1;
	strlcpy(snhe->script_name, script_name, sizeof(snhe->script_name));
	return 0;
}

/*
 * Given a function_name, get its script_name.
 * Return NULL if function_name not found.
 *
 * script_name is the base name of the file, without .lua.
 */
char *frrscript_names_get_script_name(const char *function_name)
{
	struct frrscript_names_entry lookup;

	strlcpy(lookup.function_name, function_name,
		sizeof(lookup.function_name));
	struct frrscript_names_entry *snhe =
		frrscript_names_find(&frrscript_names_hash, &lookup);
	if (!snhe)
		return NULL;

	if (snhe->script_name[0] == '\0')
		return NULL;

	return snhe->script_name;
}

uint32_t frrscript_names_hash_key(const struct frrscript_names_entry *snhe)
{
	return string_hash_make(snhe->function_name);
}

int frrscript_names_hash_cmp(const struct frrscript_names_entry *snhe1,
			     const struct frrscript_names_entry *snhe2)
{
	return strncmp(snhe1->function_name, snhe2->function_name,
		       sizeof(snhe1->function_name));
}

/* Codecs */

struct frrscript_codec frrscript_codecs_lib[] = {
	{.typename = "integer",
	 .encoder = (encoder_func)lua_pushintegerp,
	 .decoder = lua_tointegerp},
	{.typename = "string",
	 .encoder = (encoder_func)lua_pushstring_wrapper,
	 .decoder = lua_tostringp},
	{.typename = "prefix",
	 .encoder = (encoder_func)lua_pushprefix,
	 .decoder = lua_toprefix},
	{.typename = "interface",
	 .encoder = (encoder_func)lua_pushinterface,
	 .decoder = lua_tointerface},
	{.typename = "in_addr",
	 .encoder = (encoder_func)lua_pushinaddr,
	 .decoder = lua_toinaddr},
	{.typename = "in6_addr",
	 .encoder = (encoder_func)lua_pushin6addr,
	 .decoder = lua_toin6addr},
	{.typename = "sockunion",
	 .encoder = (encoder_func)lua_pushsockunion,
	 .decoder = lua_tosockunion},
	{}};

/* Type codecs */

struct hash *codec_hash;
char scriptdir[MAXPATHLEN];

static unsigned int codec_hash_key(const void *data)
{
	const struct frrscript_codec *c = data;

	return string_hash_make(c->typename);
}

static bool codec_hash_cmp(const void *d1, const void *d2)
{
	const struct frrscript_codec *e1 = d1;
	const struct frrscript_codec *e2 = d2;

	return strmatch(e1->typename, e2->typename);
}

static void *codec_alloc(void *arg)
{
	struct frrscript_codec *tmp = arg;

	struct frrscript_codec *e =
		XCALLOC(MTYPE_SCRIPT, sizeof(struct frrscript_codec));
	e->typename = XSTRDUP(MTYPE_SCRIPT, tmp->typename);
	e->encoder = tmp->encoder;
	e->decoder = tmp->decoder;

	return e;
}

static void codec_free(void *data)
{
	struct frrscript_codec *c = data;
	char *constworkaroundandihateit = (char *)c->typename;

	XFREE(MTYPE_SCRIPT, constworkaroundandihateit);
	XFREE(MTYPE_SCRIPT, c);
}

/* Lua function hash utils */

unsigned int lua_function_hash_key(const void *data)
{
	const struct lua_function_state *lfs = data;

	return string_hash_make(lfs->name);
}

bool lua_function_hash_cmp(const void *d1, const void *d2)
{
	const struct lua_function_state *lfs1 = d1;
	const struct lua_function_state *lfs2 = d2;

	return strmatch(lfs1->name, lfs2->name);
}

void *lua_function_alloc(void *arg)
{
	struct lua_function_state *tmp = arg;
	struct lua_function_state *lfs =
		XCALLOC(MTYPE_SCRIPT, sizeof(struct lua_function_state));

	lfs->name = tmp->name;
	lfs->L = tmp->L;
	return lfs;
}

static void lua_function_free(void *data)
{
	struct lua_function_state *lfs = data;

	lua_close(lfs->L);
	XFREE(MTYPE_SCRIPT, lfs);
}

/* internal frrscript APIs */

int _frrscript_call_lua(struct lua_function_state *lfs, int nargs)
{

	int ret;
	ret = lua_pcall(lfs->L, nargs, 1, 0);

	switch (ret) {
	case LUA_OK:
		break;
	case LUA_ERRRUN:
		zlog_err("Lua hook call '%s' : runtime error: %s", lfs->name,
			 lua_tostring(lfs->L, -1));
		break;
	case LUA_ERRMEM:
		zlog_err("Lua hook call '%s' : memory error: %s", lfs->name,
			 lua_tostring(lfs->L, -1));
		break;
	case LUA_ERRERR:
		zlog_err("Lua hook call '%s' : error handler error: %s",
			 lfs->name, lua_tostring(lfs->L, -1));
		break;
	case LUA_ERRGCMM:
		zlog_err("Lua hook call '%s' : garbage collector error: %s",
			 lfs->name, lua_tostring(lfs->L, -1));
		break;
	default:
		zlog_err("Lua hook call '%s' : unknown error: %s", lfs->name,
			 lua_tostring(lfs->L, -1));
		break;
	}

	if (ret != LUA_OK) {
		lua_pop(lfs->L, 1);
		goto done;
	}

	if (lua_gettop(lfs->L) != 1) {
		zlog_err(
			"Lua hook call '%s': Lua function should return only 1 result",
			lfs->name);
		ret = 1;
		goto done;
	}

	if (lua_istable(lfs->L, 1) != 1) {
		zlog_err(
			"Lua hook call '%s': Lua function should return a Lua table",
			lfs->name);
		ret = 1;
	}

done:
	/* LUA_OK is 0, so we can just return lua_pcall's result directly */
	return ret;
}

void *frrscript_get_result(struct frrscript *fs, const char *function_name,
			   const char *name,
			   void *(*lua_to)(lua_State *L, int idx))
{
	void *p;
	struct lua_function_state *lfs;
	struct lua_function_state lookup = {.name = function_name};

	lfs = hash_lookup(fs->lua_function_hash, &lookup);

	if (lfs == NULL)
		return NULL;

	/* At this point, the Lua state should have only the returned table.
	 * We will then search the table for the key/value we're interested in.
	 * Then if the value is present (i.e. non-nil), call the lua_to*
	 * decoder.
	 */
	assert(lua_gettop(lfs->L) == 1);
	assert(lua_istable(lfs->L, -1) == 1);
	lua_getfield(lfs->L, -1, name);
	if (lua_isnil(lfs->L, -1)) {
		lua_pop(lfs->L, 1);
		zlog_warn(
			"frrscript: '%s.lua': '%s': tried to decode '%s' as result but failed",
			fs->name, function_name, name);
		return NULL;
	}
	p = lua_to(lfs->L, 2);

	/* At the end, the Lua state should be same as it was at the start
	 * i.e. containing solely the returned table.
	 */
	assert(lua_gettop(lfs->L) == 1);
	assert(lua_istable(lfs->L, -1) == 1);

	return p;
}

void frrscript_register_type_codec(struct frrscript_codec *codec)
{
	struct frrscript_codec c = *codec;

	if (hash_lookup(codec_hash, &c)) {
		zlog_backtrace(LOG_ERR);
		assert(!"Type codec double-registered.");
	}

	(void)hash_get(codec_hash, &c, codec_alloc);
}

void frrscript_register_type_codecs(struct frrscript_codec *codecs)
{
	for (int i = 0; codecs[i].typename != NULL; i++)
		frrscript_register_type_codec(&codecs[i]);
}

struct frrscript *frrscript_new(const char *name)
{
	struct frrscript *fs = XCALLOC(MTYPE_SCRIPT, sizeof(struct frrscript));

	fs->name = XSTRDUP(MTYPE_SCRIPT, name);
	fs->lua_function_hash =
		hash_create(lua_function_hash_key, lua_function_hash_cmp,
			    "Lua function state hash");
	return fs;
}

int frrscript_load(struct frrscript *fs, const char *function_name,
		   int (*load_cb)(struct frrscript *))
{

	/* Set up the Lua script */
	lua_State *L = luaL_newstate();

	frrlua_export_logging(L);

	char script_name[MAXPATHLEN];

	if (snprintf(script_name, sizeof(script_name), "%s/%s.lua", scriptdir,
		     fs->name)
	    >= (int)sizeof(script_name)) {
		zlog_err("frrscript: path to script %s/%s.lua is too long",
			 scriptdir, fs->name);
		goto fail;
	}

	if (luaL_dofile(L, script_name) != 0) {
		zlog_err("frrscript: failed loading script '%s': error: %s",
			 script_name, lua_tostring(L, -1));
		goto fail;
	}

	/* To check the Lua function, we get it from the global table */
	lua_getglobal(L, function_name);
	if (lua_isfunction(L, lua_gettop(L)) == 0) {
		zlog_err("frrscript: loaded script '%s' but %s not found",
			 script_name, function_name);
		goto fail;
	}
	/* Then pop the function (frrscript_call will push it when it needs it)
	 */
	lua_pop(L, 1);

	if (load_cb && (*load_cb)(fs) != 0) {
		zlog_err(
			"frrscript: '%s': %s: loaded but callback returned non-zero exit code",
			script_name, function_name);
		goto fail;
	}

	/* Add the Lua function state to frrscript */
	struct lua_function_state key = {.name = function_name, .L = L};

	(void)hash_get(fs->lua_function_hash, &key, lua_function_alloc);

	return 0;
fail:
	lua_close(L);
	return 1;
}

void frrscript_delete(struct frrscript *fs)
{
	hash_clean_and_free(&fs->lua_function_hash, lua_function_free);
	XFREE(MTYPE_SCRIPT, fs->name);
	XFREE(MTYPE_SCRIPT, fs);
}

void frrscript_init(const char *sd)
{
	codec_hash = hash_create(codec_hash_key, codec_hash_cmp,
				 "Lua type encoders");

	strlcpy(scriptdir, sd, sizeof(scriptdir));

	/* Register core library types */
	frrscript_register_type_codecs(frrscript_codecs_lib);
}

void frrscript_fini(void)
{
	hash_clean_and_free(&codec_hash, codec_free);

	frrscript_names_destroy();
}
#endif /* HAVE_SCRIPTING */