Skip to content

Commit 519c1ff

Browse files
babeld: send events via ubus (#633)
* babeld: send events via ubus Send a notification via the ubus bus if we experience any changes in neighbours, routes or xroutes. The format looks like this: {route,xroute,neighbour}.add: Object was added {route,xroute,neighbour}.change: Object was changed {route,xroute,neighbour}.flush: Object was flushed If ubus_bindings is turned off, it will minimally effect performance, since only an if-statement has to be evaluated. If no subscriber is available, it will minimally change the performance, since only an if-statmenet that checks for subscribers has to be evaluated. Signed-off-by: Nick Hainke <[email protected]>
1 parent 2e602f7 commit 519c1ff

4 files changed

Lines changed: 169 additions & 12 deletions

File tree

babeld/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
99

1010
PKG_NAME:=babeld
1111
PKG_VERSION:=1.9.2
12-
PKG_RELEASE:=2
12+
PKG_RELEASE:=3
1313

1414
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
1515
PKG_SOURCE_URL:=https://www.irif.fr/~jch/software/files/

babeld/patches/600-add-ubus.patch

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,57 @@
9191
else
9292
abort();
9393
} else if(strcmp(token, "protocol-group") == 0) {
94+
--- a/local.c
95+
+++ b/local.c
96+
@@ -42,6 +42,8 @@ THE SOFTWARE.
97+
#include "local.h"
98+
#include "version.h"
99+
100+
+#include "ubus.h"
101+
+
102+
int local_server_socket = -1;
103+
struct local_socket local_sockets[MAX_LOCAL_SOCKETS];
104+
int num_local_sockets = 0;
105+
@@ -80,7 +82,7 @@ write_timeout(int fd, const void *buf, i
106+
}
107+
}
108+
109+
-static const char *
110+
+const char *
111+
local_kind(int kind)
112+
{
113+
switch(kind) {
114+
@@ -191,6 +193,8 @@ local_notify_neighbour(struct neighbour
115+
if(local_sockets[i].monitor)
116+
local_notify_neighbour_1(&local_sockets[i], neigh, kind);
117+
}
118+
+ if(ubus_bindings)
119+
+ ubus_notify_neighbour(neigh, kind);
120+
}
121+
122+
static void
123+
@@ -228,6 +232,8 @@ local_notify_xroute(struct xroute *xrout
124+
if(local_sockets[i].monitor)
125+
local_notify_xroute_1(&local_sockets[i], xroute, kind);
126+
}
127+
+ if(ubus_bindings)
128+
+ ubus_notify_xroute(xroute, kind);
129+
}
130+
131+
static void
132+
@@ -273,6 +279,8 @@ local_notify_route(struct babel_route *r
133+
if(local_sockets[i].monitor)
134+
local_notify_route_1(&local_sockets[i], route, kind);
135+
}
136+
+ if(ubus_bindings)
137+
+ ubus_notify_route(route, kind);
138+
}
139+
140+
static void
141+
--- a/local.h
142+
+++ b/local.h
143+
@@ -55,3 +55,4 @@ int local_read(struct local_socket *s);
144+
int local_header(struct local_socket *s);
145+
struct local_socket *local_socket_create(int fd);
146+
void local_socket_destroy(int i);
147+
+const char * local_kind(int kind);

babeld/src/ubus.c

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ static void babeld_add_xroute_buf(struct xroute *xroute, struct blob_buf *b) {
8787
blobmsg_close_table(b, prefix);
8888
}
8989

90-
// Sends an exported routes message on ubus socket, splitting apart IPv4 and IPv6 routes.
90+
// Sends an exported routes message on ubus socket, splitting apart IPv4 and
91+
// IPv6 routes.
9192
static void babeld_ubus_get_xroutes(struct ubus_context *ctx_local,
9293
struct ubus_object *obj,
9394
struct ubus_request_data *req,
@@ -187,7 +188,8 @@ static void babeld_add_route_buf(struct babel_route *route,
187188
blobmsg_close_table(b, prefix);
188189
}
189190

190-
// Sends received routes message on ubus socket, splitting apart IPv4 and IPv6 routes.
191+
// Sends received routes message on ubus socket, splitting apart IPv4 and IPv6
192+
// routes.
191193
static void babeld_ubus_get_routes(struct ubus_context *ctx_local,
192194
struct ubus_object *obj,
193195
struct ubus_request_data *req,
@@ -259,7 +261,8 @@ static void babeld_add_neighbour_buf(struct neighbour *neigh,
259261
blobmsg_close_table(b, neighbour);
260262
}
261263

262-
// Sends neighbours message on ubus socket, splitting apart IPv4 and IPv6 neighbours.
264+
// Sends neighbours message on ubus socket, splitting apart IPv4 and IPv6
265+
// neighbours.
263266
static void babeld_ubus_get_neighbours(struct ubus_context *ctx_local,
264267
struct ubus_object *obj,
265268
struct ubus_request_data *req,
@@ -342,7 +345,8 @@ static bool ubus_init_object() {
342345
return true;
343346
}
344347

345-
// Initializes the global ubus context, connecting to the bus to be able to receive and send messages.
348+
// Initializes the global ubus context, connecting to the bus to be able to
349+
// receive and send messages.
346350
static bool babeld_ubus_init(void) {
347351
if (shared_ctx)
348352
return true;
@@ -354,6 +358,63 @@ static bool babeld_ubus_init(void) {
354358
return true;
355359
}
356360

361+
void ubus_notify_route(struct babel_route *route, int kind) {
362+
struct blob_buf b = {0};
363+
char method[50]; // possible methods are route.change, route.add, route.flush
364+
365+
if (!babeld_object.has_subscribers)
366+
return;
367+
368+
if (!route)
369+
return;
370+
371+
if (!shared_ctx)
372+
return;
373+
374+
blob_buf_init(&b, 0);
375+
babeld_add_route_buf(route, &b);
376+
snprintf(method, sizeof(method), "route.%s", local_kind(kind));
377+
ubus_notify(shared_ctx, &babeld_object, method, b.head, -1);
378+
}
379+
380+
void ubus_notify_xroute(struct xroute *xroute, int kind) {
381+
struct blob_buf b = {0};
382+
char method[50]; // possible methods are xroute.change, xroute.add, xroute.flush
383+
384+
if (!babeld_object.has_subscribers)
385+
return;
386+
387+
if (!xroute)
388+
return;
389+
390+
if (!shared_ctx)
391+
return;
392+
393+
blob_buf_init(&b, 0);
394+
babeld_add_xroute_buf(xroute, &b);
395+
snprintf(method, sizeof(method), "xroute.%s", local_kind(kind));
396+
ubus_notify(shared_ctx, &babeld_object, method, b.head, -1);
397+
}
398+
399+
void ubus_notify_neighbour(struct neighbour *neigh, int kind) {
400+
struct blob_buf b = {0};
401+
char method[50]; // possible methods are neigh.change, neigh.add, neigh.flush
402+
403+
if (!babeld_object.has_subscribers)
404+
return;
405+
406+
if (!neigh)
407+
return;
408+
409+
if (!shared_ctx)
410+
return;
411+
412+
blob_buf_init(&b, 0);
413+
babeld_add_neighbour_buf(neigh, &b);
414+
snprintf(method, sizeof(method), "neigh.%s", local_kind(kind));
415+
ubus_notify(shared_ctx, &babeld_object, method, b.head, -1);
416+
}
417+
357418
void babeld_ubus_receive(fd_set *readfds) {
358419
if (!shared_ctx)
359420
return;

babeld/src/ubus.h

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
/*
22
IPC integration of babeld with OpenWrt.
3-
3+
44
The ubus interface offers following functions:
55
- get_info
66
- get_neighbours
77
- get_xroutes
88
- get_routes
9-
9+
1010
All output is divided into IPv4 and IPv6.
11+
12+
Ubus notifications are sent if we receive updates for
13+
- xroutes
14+
- routes
15+
- neighbours
16+
17+
The format is:
18+
- {route,xroute,neighbour}.add: Object was added
19+
- {route,xroute,neighbour}.change: Object was changed
20+
- {route,xroute,neighbour}.flush: Object was flushed
21+
1122
*/
1223

1324
#include <libubus.h>
@@ -28,18 +39,49 @@ bool babeld_add_ubus();
2839
* Add ubus socket to given filedescriptor set.
2940
*
3041
* We need to check repeatedly if the ubus socket has something to read.
31-
* The functions allows to add the ubus socket to the normal while(1)-loop of babeld.
42+
* The functions allows to add the ubus socket to the normal while(1)-loop of
43+
* babeld.
3244
*
3345
* @param readfs: the filedescriptor set
3446
* @param maxfd: the current maximum file descriptor
3547
* @return the maximum file descriptor
3648
*/
37-
int babeld_ubus_add_read_sock(fd_set* readfds, int maxfd);
38-
49+
int babeld_ubus_add_read_sock(fd_set *readfds, int maxfd);
3950

4051
/**
4152
* Check and process ubus socket.
4253
*
43-
* If the ubus-socket signals that data is available, the ubus_handle_event is called.
54+
* If the ubus-socket signals that data is available, the ubus_handle_event is
55+
* called.
56+
*/
57+
void babeld_ubus_receive(fd_set *readfds);
58+
59+
/***
60+
* Notify the ubus bus that a new xroute is received.
61+
*
62+
* If a new xroute is received or changed, we will notify subscribers.
63+
*
64+
* @param xroute: xroute that experienced some change
65+
* @param kind: kind that describes if we have a flush, add or change
66+
*/
67+
void ubus_notify_xroute(struct xroute *xroute, int kind);
68+
69+
/***
70+
* Notify the ubus bus that a new route is received.
71+
*
72+
* If a new route is received or changed, we will notify subscribers.
73+
*
74+
* @param route: route that experienced some change
75+
* @param kind: kind that describes if we have a flush, add or change
76+
*/
77+
void ubus_notify_route(struct babel_route *route, int kind);
78+
79+
/***
80+
* Notify the ubus bus that a new neighbour is received.
81+
*
82+
* If a new neighbour is received or changed, we will notify subscribers.
83+
*
84+
* @param neigh: neighbour that experienced some change
85+
* @param kind: kind that describes if we have a flush, add or change
4486
*/
45-
void babeld_ubus_receive(fd_set* readfds);
87+
void ubus_notify_neighbour(struct neighbour *neigh, int kind);

0 commit comments

Comments
 (0)