summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelix Fietkau2023-12-05 09:35:30 +0000
committerFelix Fietkau2023-12-05 11:59:05 +0000
commit1b967334189baa138699f637ac07bcbf0289fbf4 (patch)
tree379757c292f0dfdf71e065e5a0b5f55ca091a74b
parent02bc2e14d1d37500e888c0c53ac41398a56b5579 (diff)
netifd: add netlink udebug ring
Signed-off-by: Felix Fietkau <nbd@nbd.name>
-rw-r--r--main.c66
-rw-r--r--netifd.h5
-rw-r--r--system-linux.c11
-rw-r--r--ubus.c10
4 files changed, 54 insertions, 38 deletions
diff --git a/main.c b/main.c
index 403dc12..b6d2aa5 100644
--- a/main.c
+++ b/main.c
@@ -19,8 +19,6 @@
#include <stdarg.h>
#include <syslog.h>
-#include <udebug.h>
-
#include "netifd.h"
#include "ubus.h"
#include "config.h"
@@ -38,8 +36,31 @@ static char **global_argv;
static struct list_head process_list = LIST_HEAD_INIT(process_list);
static struct udebug ud;
-static struct udebug_buf udb;
-static bool udebug_enabled;
+static struct udebug_buf udb_log;
+struct udebug_buf udb_nl;
+static const struct udebug_buf_meta meta_log = {
+ .name = "netifd_log",
+ .format = UDEBUG_FORMAT_STRING,
+};
+static const struct udebug_buf_meta meta_nl = {
+ .name = "netifd_nl",
+ .format = UDEBUG_FORMAT_PACKET,
+ .sub_format = UDEBUG_DLT_NETLINK,
+};
+static struct udebug_ubus_ring rings[] = {
+ {
+ .buf = &udb_log,
+ .meta = &meta_log,
+ .default_entries = 1024,
+ .default_size = 64 * 1024,
+ },
+ {
+ .buf = &udb_nl,
+ .meta = &meta_nl,
+ .default_entries = 1024,
+ .default_size = 64 * 1024,
+ },
+};
#define DEFAULT_LOG_LEVEL L_NOTICE
@@ -71,12 +92,12 @@ netifd_delete_process(struct netifd_process *proc)
static void
netifd_udebug_vprintf(const char *format, va_list ap)
{
- if (!udebug_enabled)
+ if (!udebug_buf_valid(&udb_log))
return;
- udebug_entry_init(&udb);
- udebug_entry_vprintf(&udb, format, ap);
- udebug_entry_add(&udb);
+ udebug_entry_init(&udb_log);
+ udebug_entry_vprintf(&udb_log, format, ap);
+ udebug_entry_add(&udb_log);
}
void netifd_udebug_printf(const char *format, ...)
@@ -88,27 +109,10 @@ void netifd_udebug_printf(const char *format, ...)
va_end(ap);
}
-void netifd_udebug_set_enabled(bool val)
+void netifd_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data,
+ bool enabled)
{
- static const struct udebug_buf_meta meta = {
- .name = "netifd_log",
- .format = UDEBUG_FORMAT_STRING,
- };
-
- if (udebug_enabled == val)
- return;
-
- udebug_enabled = val;
- if (!val) {
- udebug_buf_free(&udb);
- udebug_free(&ud);
- return;
- }
-
- udebug_init(&ud);
- udebug_auto_connect(&ud, NULL);
- udebug_buf_init(&udb, 1024, 64 * 1024);
- udebug_buf_add(&ud, &udb, &meta);
+ udebug_ubus_apply_config(&ud, rings, ARRAY_SIZE(rings), data, enabled);
}
void
@@ -371,6 +375,12 @@ int main(int argc, char **argv)
openlog("netifd", 0, LOG_DAEMON);
netifd_setup_signals();
+ uloop_init();
+ 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]);
+
if (netifd_ubus_init(socket) < 0) {
fprintf(stderr, "Failed to connect to ubus\n");
return 1;
diff --git a/netifd.h b/netifd.h
index c579e7c..df9d324 100644
--- a/netifd.h
+++ b/netifd.h
@@ -25,6 +25,7 @@
#include <libubox/utils.h>
#include <libubus.h>
+#include <udebug.h>
#ifdef linux
#include <netinet/ether.h>
@@ -51,6 +52,7 @@
extern const char *resolv_conf;
extern char *hotplug_cmd_path;
extern unsigned int debug_mask;
+extern struct udebug_buf udb_nl;
enum {
L_CRIT,
@@ -97,7 +99,8 @@ struct netifd_process {
};
void netifd_udebug_printf(const char *format, ...);
-void netifd_udebug_set_enabled(bool val);
+void netifd_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data,
+ bool enabled);
void netifd_log_message(int priority, const char *format, ...);
int netifd_start_process(const char **argv, char **env, struct netifd_process *proc);
diff --git a/system-linux.c b/system-linux.c
index 96cc993..4d25279 100644
--- a/system-linux.c
+++ b/system-linux.c
@@ -203,6 +203,14 @@ abort:
return;
}
+static void
+nl_udebug_cb(void *priv, struct nl_msg *msg)
+{
+ struct nlmsghdr *nlh = nlmsg_hdr(msg);
+
+ udebug_netlink_msg(priv, nlmsg_get_proto(msg), nlh, nlh->nlmsg_len);
+}
+
static struct nl_sock *
create_socket(int protocol, int groups)
{
@@ -220,6 +228,9 @@ create_socket(int protocol, int groups)
return NULL;
}
+ nl_socket_set_tx_debug_cb(sock, nl_udebug_cb, &udb_nl);
+ nl_socket_set_rx_debug_cb(sock, nl_udebug_cb, &udb_nl);
+
return sock;
}
diff --git a/ubus.c b/ubus.c
index 1d3d71f..609cec8 100644
--- a/ubus.c
+++ b/ubus.c
@@ -16,7 +16,6 @@
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
-#include <udebug.h>
#include "netifd.h"
#include "interface.h"
@@ -1367,16 +1366,9 @@ netifd_extdev_invoke(uint32_t id, const char *method, struct blob_attr *msg,
return ubus_invoke(ubus_ctx, id, method, msg, data_cb, data, 3000);
}
-static void
-netifd_udebug_cb(struct udebug_ubus *ctx, struct blob_attr *data, bool enabled)
-{
- netifd_udebug_set_enabled(enabled);
-}
-
int
netifd_ubus_init(const char *path)
{
- uloop_init();
ubus_path = path;
ubus_ctx = ubus_connect(path);
@@ -1392,7 +1384,7 @@ netifd_ubus_init(const char *path)
netifd_add_object(&wireless_object);
netifd_add_iface_object();
- udebug_ubus_init(&udebug, ubus_ctx, "netifd", netifd_udebug_cb);
+ udebug_ubus_init(&udebug, ubus_ctx, "netifd", netifd_udebug_config);
return 0;
}