add udebug support
authorFelix Fietkau <nbd@nbd.name>
Fri, 28 Feb 2025 13:30:53 +0000 (14:30 +0100)
committerFelix Fietkau <nbd@nbd.name>
Fri, 28 Feb 2025 13:30:56 +0000 (14:30 +0100)
Signed-off-by: Felix Fietkau <nbd@nbd.name>
CMakeLists.txt
main.c
network.c
ubus.c
unetd.h

index 8f7ef9f9bc2bebc92e6e7925877b7656a3d3999b..aba97870ecc7a54553b939f4951aacaf7057b2a2 100644 (file)
@@ -40,15 +40,17 @@ IF(UBUS_SUPPORT)
   SET(DHT_SOURCES ${DHT_SOURCES} udht-ubus.c)
   ADD_DEFINITIONS(-DUBUS_SUPPORT=1)
   FIND_LIBRARY(ubus ubus)
+  FIND_LIBRARY(udebug NAMES udebug)
 ELSE()
   SET(ubus "")
+  SET(udebug "")
 ENDIF()
 
 ADD_LIBRARY(unet SHARED curve25519.c siphash.c sha512.c fprime.c f25519.c ed25519.c edsign.c auth-data.c chacha20.c pex-msg.c utils.c stun.c)
 TARGET_LINK_LIBRARIES(unet ubox)
 
 ADD_EXECUTABLE(unetd ${SOURCES})
-TARGET_LINK_LIBRARIES(unetd unet ubox ${ubus} blobmsg_json ${libjson} ${nl} ${bpf} ${elf} ${zlib})
+TARGET_LINK_LIBRARIES(unetd unet ubox ${ubus} blobmsg_json ${libjson} ${nl} ${bpf} ${elf} ${zlib} ${udebug})
 
 ADD_EXECUTABLE(unet-tool cli.c)
 TARGET_LINK_LIBRARIES(unet-tool unet blobmsg_json ${libjson} ubox)
diff --git a/main.c b/main.c
index be24db768ced14db1737f138a40856b1ef3e2127..7014af86b2c3cb8503b8ce0c59a33f8a857370bf 100644 (file)
--- a/main.c
+++ b/main.c
@@ -19,7 +19,75 @@ static const char *hosts_file;
 const char *mssfix_path = UNETD_MSS_BPF_PATH;
 const char *data_dir = UNETD_DATA_DIR;
 int global_pex_port = UNETD_GLOBAL_PEX_PORT;
-bool debug;
+
+static bool debug;
+
+#ifdef UBUS_SUPPORT
+static struct udebug ud;
+static struct udebug_buf udb_log;
+
+static const struct udebug_buf_meta meta_log = {
+       .name = "log",
+       .format = UDEBUG_FORMAT_STRING
+};
+
+static struct udebug_ubus_ring rings[] = {
+       {
+               .buf = &udb_log,
+               .meta = &meta_log,
+               .default_entries = 1024,
+               .default_size = 64 * 1024,
+       }
+};
+#endif
+
+bool unetd_debug_active(void)
+{
+#ifdef UBUS_SUPPORT
+       if (udebug_buf_valid(&udb_log))
+               return true;
+#endif
+       return debug;
+}
+
+static void __attribute__((format (printf, 1, 0)))
+unetd_udebug_vprintf(const char *format, va_list ap)
+{
+#ifdef UBUS_SUPPORT
+       if (!udebug_buf_valid(&udb_log))
+               return;
+
+       udebug_entry_init(&udb_log);
+       udebug_entry_vprintf(&udb_log, format, ap);
+       udebug_entry_add(&udb_log);
+#endif
+}
+
+void unetd_debug_printf(const char *format, ...)
+{
+       va_list ap;
+
+       va_start(ap, format);
+
+       if (debug) {
+               va_list ap2;
+
+               va_copy(ap2, ap);
+               vfprintf(stderr, format, ap2);
+               va_end(ap2);
+       }
+
+       unetd_udebug_vprintf(format, ap);
+       va_end(ap);
+}
+
+#ifdef UBUS_SUPPORT
+void unetd_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data,
+                        bool enabled)
+{
+       udebug_ubus_apply_config(&ud, rings, ARRAY_SIZE(rings), data, enabled);
+}
+#endif
 
 static void
 network_write_hosts(struct network *net, FILE *f)
@@ -134,6 +202,13 @@ int main(int argc, char **argv)
        }
 
        uloop_init();
+#ifdef UBUS_SUPPORT
+       udebug_init(&ud);
+       udebug_auto_connect(&ud, NULL);
+       for (size_t i = 0; i < ARRAY_SIZE(rings); i++)
+               udebug_ubus_ring_init(&ud, &rings[i]);
+#endif
+
        unetd_ubus_init();
        unetd_write_hosts();
        global_pex_open(unix_socket);
index 09fd01b964077fa1e0d7451975f9aaddec2b2d44..c5de02c825a1f19d06d12d95aad5e35dcaa47370 100644 (file)
--- a/network.c
+++ b/network.c
@@ -459,7 +459,7 @@ network_do_update(struct network *net, bool up)
                network_fill_subnets(net, &b);
        }
 
-       if (debug) {
+       if (unetd_debug_active()) {
                char *s = blobmsg_format_json(b.head, true);
                D_NET(net, "update: %s", s);
                free(s);
diff --git a/ubus.c b/ubus.c
index 6e32b536c35376b457d6ae545beb50924618a109..ba6b3de974afbd7bc382fb2358ad3c996ff08197 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -11,6 +11,7 @@
 static struct ubus_auto_conn conn;
 static struct ubus_subscriber sub;
 static struct blob_buf b;
+static struct udebug_ubus udebug;
 
 static int
 ubus_network_add(struct ubus_context *ctx, struct ubus_object *obj,
@@ -561,6 +562,7 @@ ubus_connect_handler(struct ubus_context *ctx)
 {
        int ret;
 
+       udebug_ubus_init(&udebug, ctx, "unetd", unetd_udebug_config);
        ubus_register_subscriber(ctx, &sub);
        ret = ubus_add_object(ctx, &unetd_object);
        if (ret)
diff --git a/unetd.h b/unetd.h
index 56e8d2d07270554c8346ee0d84e97f842faa999b..a218d0bd7b68f2a9dfb8bbedaf9a627250dc39f2 100644 (file)
--- a/unetd.h
+++ b/unetd.h
@@ -10,6 +10,9 @@
 #include <libubox/vlist.h>
 #include <libubox/blobmsg.h>
 #include <libubox/utils.h>
+#ifdef UBUS_SUPPORT
+#include <udebug.h>
+#endif
 #include "utils.h"
 #include "siphash.h"
 #include "wg.h"
 
 extern const char *mssfix_path;
 extern const char *data_dir;
-extern bool debug;
 extern int global_pex_port;
+bool unetd_debug_active(void);
+void unetd_debug_printf(const char *format, ...);
+#ifdef UBUS_SUPPORT
+void unetd_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data,
+                        bool enabled);
+#endif
 
-#define D(format, ...)                                                         \
-       do {                                                                    \
-               if (debug)                                                      \
-                       fprintf(stderr, "%s(%d) " format "\n",                  \
-                               __func__, __LINE__, ##__VA_ARGS__);             \
+#define D(format, ...)                                                 \
+       do {                                                            \
+               unetd_debug_printf("%s(%d) " format "\n",               \
+                                  __func__, __LINE__, ##__VA_ARGS__);  \
        } while (0)
 
 #define D_NET(net, format, ...)        D("network %s " format, network_name(net), ##__VA_ARGS__)