Skip to content

Commit 5d847e7

Browse files
cosmo0920robskillington
authored andcommitted
plugin_proxy: Implement a capability to process custom Go plugins
Signed-off-by: Rob Skillington <[email protected]> Signed-off-by: Hiroshi Hatake <[email protected]> Co-authored-by: Rob Skillington <[email protected]>
1 parent ea237cf commit 5d847e7

File tree

4 files changed

+217
-1
lines changed

4 files changed

+217
-1
lines changed

include/fluent-bit/flb_plugin_proxy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
/* Plugin Types */
2929
#define FLB_PROXY_INPUT_PLUGIN 1
3030
#define FLB_PROXY_OUTPUT_PLUGIN 2
31+
#define FLB_PROXY_CUSTOM_PLUGIN 3
3132

3233
/* Proxies available */
3334
#define FLB_PROXY_GOLANG 11

src/flb_plugin_proxy.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <fluent-bit/flb_utils.h>
3434
#include <fluent-bit/flb_plugin_proxy.h>
3535
#include <fluent-bit/flb_input_log.h>
36+
#include <fluent-bit/flb_custom.h>
3637

3738
/* Proxies */
3839
#include "proxy/go/go.h"
@@ -422,6 +423,44 @@ static int flb_proxy_register_input(struct flb_plugin_proxy *proxy,
422423
return 0;
423424
}
424425

426+
int flb_proxy_custom_cb_init(struct flb_custom_instance *c_ins,
427+
struct flb_config *config, void *data);
428+
429+
static int flb_proxy_custom_cb_exit(void *custom_context,
430+
struct flb_config *config);
431+
static void flb_proxy_custom_cb_destroy(struct flb_custom_plugin *plugin);
432+
433+
static int flb_proxy_register_custom(struct flb_plugin_proxy *proxy,
434+
struct flb_plugin_proxy_def *def,
435+
struct flb_config *config)
436+
{
437+
struct flb_custom_plugin *custom;
438+
439+
custom = flb_calloc(1, sizeof(struct flb_custom_plugin));
440+
if (!custom) {
441+
flb_errno();
442+
return -1;
443+
}
444+
445+
/* Plugin registration */
446+
custom->type = FLB_CUSTOM_PLUGIN_PROXY;
447+
custom->proxy = proxy;
448+
custom->flags = def->flags;
449+
custom->name = flb_strdup(def->name);
450+
custom->description = def->description;
451+
mk_list_add(&custom->_head, &config->custom_plugins);
452+
453+
/*
454+
* Set proxy callbacks: external plugins which are not following
455+
* the core plugins specs, have a different callback approach, so
456+
* we put our proxy-middle callbacks to do the translation properly.
457+
*/
458+
custom->cb_init = flb_proxy_custom_cb_init;
459+
custom->cb_exit = flb_proxy_custom_cb_exit;
460+
custom->cb_destroy = flb_proxy_custom_cb_destroy;
461+
return 0;
462+
}
463+
425464
void *flb_plugin_proxy_symbol(struct flb_plugin_proxy *proxy,
426465
const char *symbol)
427466
{
@@ -487,6 +526,9 @@ int flb_plugin_proxy_register(struct flb_plugin_proxy *proxy,
487526
}
488527
else if (def->type == FLB_PROXY_INPUT_PLUGIN) {
489528
ret = proxy_go_input_register(proxy, def);
529+
}
530+
else if (def->type == FLB_PROXY_CUSTOM_PLUGIN) {
531+
ret = proxy_go_custom_register(proxy, def);
490532
}
491533
#endif
492534
}
@@ -501,6 +543,9 @@ int flb_plugin_proxy_register(struct flb_plugin_proxy *proxy,
501543
else if (def->type == FLB_PROXY_INPUT_PLUGIN) {
502544
flb_proxy_register_input(proxy, def, config);
503545
}
546+
else if (def->type == FLB_PROXY_CUSTOM_PLUGIN) {
547+
flb_proxy_register_custom(proxy, def, config);
548+
}
504549
}
505550

506551
return 0;
@@ -616,3 +661,70 @@ int flb_plugin_proxy_set(struct flb_plugin_proxy_def *def, int type,
616661

617662
return 0;
618663
}
664+
665+
int flb_proxy_custom_cb_init(struct flb_custom_instance *c_ins,
666+
struct flb_config *config, void *data)
667+
{
668+
int ret = -1;
669+
struct flb_plugin_proxy_context *pc;
670+
struct flb_plugin_proxy *proxy;
671+
672+
pc = (struct flb_plugin_proxy_context *)(c_ins->context);
673+
proxy = pc->proxy;
674+
675+
/* Before to initialize, set the instance reference */
676+
pc->proxy->instance = c_ins;
677+
678+
if (proxy->def->proxy == FLB_PROXY_GOLANG) {
679+
#ifdef FLB_HAVE_PROXY_GO
680+
ret = proxy_go_custom_init(proxy);
681+
#endif
682+
}
683+
684+
if (ret == -1) {
685+
flb_error("[custom] could not initialize '%s' plugin",
686+
c_ins->p->name);
687+
return -1;
688+
}
689+
690+
return 0;
691+
}
692+
693+
int flb_proxy_custom_cb_exit(void *custom_context,
694+
struct flb_config *config)
695+
{
696+
int ret = -1;
697+
struct flb_plugin_proxy_context *ctx = custom_context;
698+
struct flb_plugin_proxy *proxy = (ctx->proxy);
699+
if (!custom_context) {
700+
return ret;
701+
}
702+
703+
if (proxy->def->proxy == FLB_PROXY_GOLANG) {
704+
#ifdef FLB_HAVE_PROXY_GO
705+
ret = proxy_go_custom_destroy(ctx);
706+
#endif
707+
}
708+
709+
flb_free(ctx);
710+
return ret;
711+
}
712+
713+
static void flb_proxy_custom_cb_destroy(struct flb_custom_plugin *plugin)
714+
{
715+
struct flb_plugin_proxy *proxy = (struct flb_plugin_proxy *) plugin->proxy;
716+
717+
if (plugin->name != NULL) {
718+
flb_free(plugin->name);
719+
720+
plugin->name = NULL;
721+
}
722+
723+
if (proxy->def->proxy == FLB_PROXY_GOLANG) {
724+
#ifdef FLB_HAVE_PROXY_GO
725+
proxy_go_custom_unregister(proxy->data);
726+
#endif
727+
}
728+
729+
flb_plugin_proxy_destroy(proxy);
730+
}

src/proxy/go/go.c

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <fluent-bit/flb_config.h>
2222
#include <fluent-bit/flb_plugin_proxy.h>
2323
#include <fluent-bit/flb_output.h>
24+
#include <fluent-bit/flb_custom.h>
2425
#include "./go.h"
2526

2627
/*
@@ -41,7 +42,7 @@
4142
*
4243
* - name: shortname of the plugin.
4344
* - description: plugin description.
44-
* - type: input, output, filter, whatever.
45+
* - type: input, output, filter, custom, whatever.
4546
* - proxy: type of proxy e.g. GOLANG
4647
* - flags: optional flags, not used by Go plugins at the moment.
4748
*
@@ -285,3 +286,87 @@ void proxy_go_input_unregister(void *data) {
285286
flb_free(plugin->name);
286287
flb_free(plugin);
287288
}
289+
290+
int proxy_go_custom_register(struct flb_plugin_proxy *proxy,
291+
struct flb_plugin_proxy_def *def)
292+
{
293+
struct flbgo_custom_plugin *plugin;
294+
295+
plugin = flb_malloc(sizeof(struct flbgo_custom_plugin));
296+
if (!plugin) {
297+
flb_errno();
298+
return -1;
299+
}
300+
301+
/*
302+
* Lookup the entry point function:
303+
*
304+
* - FLBPluginInit
305+
* - FLBPluginExit
306+
*
307+
* note: registration callback FLBPluginRegister() is resolved by the
308+
* parent proxy interface.
309+
*/
310+
311+
plugin->cb_init = flb_plugin_proxy_symbol(proxy, "FLBPluginInit");
312+
if (!plugin->cb_init) {
313+
flb_error("[go proxy]: could not load FLBPluginInit symbol");
314+
flb_free(plugin);
315+
return -1;
316+
}
317+
318+
plugin->cb_exit = flb_plugin_proxy_symbol(proxy, "FLBPluginExit");
319+
320+
plugin->name = flb_strdup(def->name);
321+
322+
/* This Go plugin context is an opaque data for the parent proxy */
323+
proxy->data = plugin;
324+
325+
return 0;
326+
}
327+
328+
int proxy_go_custom_init(struct flb_plugin_proxy *proxy)
329+
{
330+
int ret = 0;
331+
struct flbgo_custom_plugin *plugin = proxy->data;
332+
333+
/* set the API */
334+
plugin->api = proxy->api;
335+
plugin->i_ins = proxy->instance;
336+
/* In order to avoid having the whole instance as part of the ABI we */
337+
/* copy the context pointer into the plugin. */
338+
plugin->context = ((struct flb_custom_instance *)proxy->instance)->context;
339+
340+
ret = plugin->cb_init(plugin);
341+
if (ret <= 0) {
342+
flb_error("[go proxy]: plugin '%s' failed to initialize",
343+
plugin->name);
344+
flb_free(plugin);
345+
return -1;
346+
}
347+
348+
return ret;
349+
}
350+
351+
int proxy_go_custom_destroy(struct flb_plugin_proxy_context *ctx)
352+
{
353+
int ret = 0;
354+
struct flbgo_custom_plugin *plugin;
355+
356+
plugin = (struct flbgo_custom_plugin *) ctx->proxy->data;
357+
358+
if (plugin->cb_exit) {
359+
ret = plugin->cb_exit();
360+
}
361+
362+
return ret;
363+
}
364+
365+
void proxy_go_custom_unregister(void *data) {
366+
struct flbgo_custom_plugin *plugin;
367+
368+
plugin = (struct flbgo_custom_plugin *) data;
369+
flb_free(plugin->name);
370+
flb_free(plugin);
371+
}
372+

src/proxy/go/go.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ struct flbgo_input_plugin {
4848
int (*cb_exit)();
4949
};
5050

51+
struct flbgo_custom_plugin {
52+
char *name;
53+
void *api;
54+
void *i_ins;
55+
struct flb_plugin_proxy_context *context;
56+
57+
int (*cb_init)();
58+
int (*cb_exit)();
59+
};
60+
5161
int proxy_go_output_register(struct flb_plugin_proxy *proxy,
5262
struct flb_plugin_proxy_def *def);
5363

@@ -69,4 +79,12 @@ int proxy_go_input_cleanup(struct flb_plugin_proxy *ctx,
6979
void *allocated_data);
7080
int proxy_go_input_destroy(struct flb_plugin_input_proxy_context *ctx);
7181
void proxy_go_input_unregister(void *data);
82+
83+
int proxy_go_custom_register(struct flb_plugin_proxy *proxy,
84+
struct flb_plugin_proxy_def *def);
85+
86+
int proxy_go_custom_init(struct flb_plugin_proxy *proxy);
87+
88+
int proxy_go_custom_destroy(struct flb_plugin_proxy_context *ctx);
89+
void proxy_go_custom_unregister(void *data);
7290
#endif

0 commit comments

Comments
 (0)