]> git.proxmox.com Git - mirror_frr.git/blame - zebra/sample_plugin.c
tools: config clang-format to allow aligned macros
[mirror_frr.git] / zebra / sample_plugin.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
743dd5f6
MS
2/*
3 * Sample plugin for the FRR zebra dataplane.
4 *
5 * Copyright (c) 2019 Volta Networks, Inc.
743dd5f6
MS
6 */
7
8/*
9 * Should be possible to build this plugin using this sort of command:
10 *
11 * gcc -I ~/work/frr/ -I ~/work/frr/lib -I ~/work/frr/zebra \
12 * -g -O0 -o sample_plugin.so -shared -fPIC sample_plugin.c
13 *
14 * where 'frr' is a configured and built frr sandbox.
15 *
16 * Run zebra with '-M /path/to/sample_plugin.so' to load the module.
17 */
18
19#include "config.h" /* Include this explicitly */
20#include "lib/zebra.h"
21#include "lib/libfrr.h"
22#include "zebra/zebra_dplane.h"
23#include "zebra/debug.h"
24
25static const char *plugin_name = "SAMPLE";
26
27static struct zebra_dplane_provider *prov_p;
28
29/*
30 * Startup/init callback, called from the dataplane.
31 */
32static int sample_start(struct zebra_dplane_provider *prov)
33{
34 /* Nothing special to do - we don't allocate anything. */
35 return 0;
36}
37
38
39/*
40 * Shutdown/cleanup callback, called from the dataplane pthread.
41 */
42static int sample_fini(struct zebra_dplane_provider *prov, bool early)
43{
44 /* Nothing special to do. */
45 return 0;
46}
47
48/*
49 * Callback from the dataplane to process incoming work; this runs in the
50 * dplane pthread.
51 */
52static int sample_process(struct zebra_dplane_provider *prov)
53{
54 int counter, limit;
55 struct zebra_dplane_ctx *ctx;
56
57 limit = dplane_provider_get_work_limit(prov_p);
58
59 /* Respect the configured limit on the amount of work to do in
60 * any one call.
61 */
62 for (counter = 0; counter < limit; counter++) {
63 ctx = dplane_provider_dequeue_in_ctx(prov_p);
64 if (!ctx)
65 break;
66
67 /* Just set 'success' status and return to the dataplane */
68 dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
69 dplane_provider_enqueue_out_ctx(prov_p, ctx);
70 }
71
72 return 0;
73}
74
75/*
76 * Init entry point called during zebra startup. This is registered during
77 * module init.
78 */
cd9d0537 79static int init_sample_plugin(struct event_loop *tm)
743dd5f6
MS
80{
81 int ret;
743dd5f6
MS
82
83 /* Note that we don't use or store the thread_master 'tm'. We
84 * don't use the zebra main pthread: our plugin code will run in
85 * the zebra dataplane pthread context.
86 */
87
88 /* Register the plugin with the dataplane infrastructure. We
89 * register to be called before the kernel, and we register
90 * our init, process work, and shutdown callbacks.
91 */
92 ret = dplane_provider_register(plugin_name, DPLANE_PRIO_PRE_KERNEL,
93 DPLANE_PROV_FLAGS_DEFAULT,
94 sample_start,
95 sample_process,
96 sample_fini,
97 NULL,
98 &prov_p);
99
100 if (IS_ZEBRA_DEBUG_DPLANE)
101 zlog_debug("sample plugin register => %d", ret);
102
103 return 0;
104}
105
106/*
107 * Base FRR loadable module info: basic info including module entry-point.
108 */
109static int module_init(void)
110{
111 hook_register(frr_late_init, init_sample_plugin);
112 return 0;
113}
114
115FRR_MODULE_SETUP(
116 .name = "dplane_sample",
117 .version = "0.0.1",
118 .description = "Dataplane Sample Plugin",
119 .init = module_init,
80413c20 120);