From f909059b7473992e84d5701fa5021faa5bd76e67 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 30 Nov 2023 20:08:21 +0100 Subject: [PATCH] hostapd: use new udebug ubus api to make debug rings configurable Signed-off-by: Felix Fietkau --- package/libs/udebug/files/udebug.config | 8 + .../network/services/hostapd/files/hostapd.uc | 27 +--- .../services/hostapd/files/wpa_supplicant.uc | 25 +-- .../services/hostapd/src/src/utils/ucode.c | 144 ++++++++++++------ 4 files changed, 108 insertions(+), 96 deletions(-) diff --git a/package/libs/udebug/files/udebug.config b/package/libs/udebug/files/udebug.config index 23d4281b52..21d1ecb18f 100644 --- a/package/libs/udebug/files/udebug.config +++ b/package/libs/udebug/files/udebug.config @@ -9,9 +9,17 @@ config service log config service hostapd option enabled 0 + option wpa_log 1 + option wpa_nl_rx 0 + option wpa_nl_tx 0 + option wpa_nl_ctrl 0 config service wpa_supplicant option enabled 0 + option wpa_log 1 + option wpa_nl_rx 0 + option wpa_nl_tx 0 + option wpa_nl_ctrl 0 config service netifd option enabled 0 diff --git a/package/network/services/hostapd/files/hostapd.uc b/package/network/services/hostapd/files/hostapd.uc index 84138f29a5..b85f523b35 100644 --- a/package/network/services/hostapd/files/hostapd.uc +++ b/package/network/services/hostapd/files/hostapd.uc @@ -782,33 +782,9 @@ let main_obj = { }, }; -function handle_debug_config(cfg) { - hostapd.printf(`handle_debug_config: ${cfg}\n`); - if (!cfg) - return; - - let data = cfg.service; - if (!data) - return; - - data = data.hostapd; - if (!data) - return; - - hostapd.udebug_set(!!+data.enabled); -} - hostapd.data.ubus = ubus; hostapd.data.obj = ubus.publish("hostapd", main_obj); -hostapd.data.debug_sub = ubus.subscriber((req) => { - if (req.type != "config") - return; - - handle_debug_config(req.data); -}); - -hostapd.data.debug_sub.subscribe("udebug"); -handle_debug_config(ubus.call("udebug", "get_config", {})); +hostapd.udebug_set("hostapd", hostapd.data.ubus); function bss_event(type, name, data) { let ubus = hostapd.data.ubus; @@ -823,6 +799,7 @@ return { shutdown: function() { for (let phy in hostapd.data.config) iface_set_config(phy, null); + hostapd.udebug_set(null); hostapd.ubus.disconnect(); }, bss_add: function(name, obj) { diff --git a/package/network/services/hostapd/files/wpa_supplicant.uc b/package/network/services/hostapd/files/wpa_supplicant.uc index aac144b339..1709bb019e 100644 --- a/package/network/services/hostapd/files/wpa_supplicant.uc +++ b/package/network/services/hostapd/files/wpa_supplicant.uc @@ -244,32 +244,9 @@ let main_obj = { }, }; -function handle_debug_config(cfg) { - if (!cfg) - return; - - let data = cfg.service; - if (!data) - return; - - data = data.wpa_supplicant; - if (!data) - return; - - wpas.udebug_set(!!+data.enabled); -} - wpas.data.ubus = ubus; wpas.data.obj = ubus.publish("wpa_supplicant", main_obj); -wpas.data.debug_sub = ubus.subscriber((req) => { - if (req.type != "config") - return; - - handle_debug_config(req.data); -}); - -wpas.data.debug_sub.subscribe("udebug"); -handle_debug_config(ubus.call("udebug", "get_config", {})); +wpas.udebug_set("wpa_supplicant", wpas.data.ubus); function iface_event(type, name, data) { let ubus = wpas.data.ubus; diff --git a/package/network/services/hostapd/src/src/utils/ucode.c b/package/network/services/hostapd/src/src/utils/ucode.c index 14fd6bc5ec..29c753c326 100644 --- a/package/network/services/hostapd/src/src/utils/ucode.c +++ b/package/network/services/hostapd/src/src/utils/ucode.c @@ -16,8 +16,59 @@ static uc_vm_t vm; static struct uloop_timeout gc_timer; static struct udebug ud; static struct udebug_buf ud_log, ud_nl[3]; - +static const struct udebug_buf_meta meta_log = { + .name = "wpa_log", + .format = UDEBUG_FORMAT_STRING, +}; +static const struct udebug_buf_meta meta_nl_ll = { + .name = "wpa_nl_ctrl", + .format = UDEBUG_FORMAT_PACKET, + .sub_format = UDEBUG_DLT_NETLINK, +}; +static const struct udebug_buf_meta meta_nl_tx = { + .name = "wpa_nl_tx", + .format = UDEBUG_FORMAT_PACKET, + .sub_format = UDEBUG_DLT_NETLINK, +}; #define UDEBUG_FLAG_RX_FRAME (1ULL << 0) +static const struct udebug_buf_flag rx_flags[] = { + { "rx_frame", UDEBUG_FLAG_RX_FRAME }, +}; +static const struct udebug_buf_meta meta_nl_rx = { + .name = "wpa_nl_rx", + .format = UDEBUG_FORMAT_PACKET, + .sub_format = UDEBUG_DLT_NETLINK, + .flags = rx_flags, + .n_flags = ARRAY_SIZE(rx_flags), +}; +static struct udebug_ubus_ring udebug_rings[] = { + { + .buf = &ud_log, + .meta = &meta_log, + .default_entries = 1024, + .default_size = 64 * 1024 + }, + { + .buf = &ud_nl[0], + .meta = &meta_nl_rx, + .default_entries = 1024, + .default_size = 256 * 1024, + }, + { + .buf = &ud_nl[1], + .meta = &meta_nl_tx, + .default_entries = 1024, + .default_size = 64 * 1024, + }, + { + .buf = &ud_nl[2], + .meta = &meta_nl_ll, + .default_entries = 1024, + .default_size = 32 * 1024, + } +}; +char *udebug_service; +struct udebug_ubus ud_ubus; static void uc_gc_timer(struct uloop_timeout *timeout) { @@ -301,68 +352,67 @@ static void udebug_netlink_hook(int tx, const void *data, size_t len) !(udebug_buf_flags(buf) & UDEBUG_FLAG_RX_FRAME)) return; + if (!udebug_buf_valid(buf)) + return; + udebug_entry_init(buf); udebug_entry_append(buf, &hdr, sizeof(hdr)); udebug_entry_append(buf, data, len); udebug_entry_add(buf); } +static void +wpa_udebug_config(struct udebug_ubus *ctx, struct blob_attr *data, + bool enabled) +{ + udebug_ubus_apply_config(&ud, udebug_rings, ARRAY_SIZE(udebug_rings), + data, enabled); + + if (udebug_buf_valid(&ud_log)) { + wpa_printf_hook = udebug_printf_hook; + wpa_hexdump_hook = udebug_hexdump_hook; + } else { + wpa_printf_hook = NULL; + wpa_hexdump_hook = NULL; + } + + if (udebug_buf_valid(&ud_nl[0]) || + udebug_buf_valid(&ud_nl[1]) || + udebug_buf_valid(&ud_nl[2])) + wpa_netlink_hook = udebug_netlink_hook; + else + wpa_netlink_hook = NULL; +} + uc_value_t *uc_wpa_udebug_set(uc_vm_t *vm, size_t nargs) { - static const struct udebug_buf_meta meta_log = { - .name = "wpa_log", - .format = UDEBUG_FORMAT_STRING, - }; - static const struct udebug_buf_meta meta_nl_ll = { - .name = "wpa_nl_ctrl", - .format = UDEBUG_FORMAT_PACKET, - .sub_format = UDEBUG_DLT_NETLINK, - }; - static const struct udebug_buf_meta meta_nl_tx = { - .name = "wpa_nl_tx", - .format = UDEBUG_FORMAT_PACKET, - .sub_format = UDEBUG_DLT_NETLINK, - }; - static const struct udebug_buf_flag rx_flags[] = { - { "rx_frame", UDEBUG_FLAG_RX_FRAME }, - }; - static const struct udebug_buf_meta meta_nl_rx = { - .name = "wpa_nl_rx", - .format = UDEBUG_FORMAT_PACKET, - .sub_format = UDEBUG_DLT_NETLINK, - .flags = rx_flags, - .n_flags = ARRAY_SIZE(rx_flags), - }; - bool val = ucv_is_truish(uc_fn_arg(0)); + uc_value_t *name = uc_fn_arg(0); + uc_value_t *ubus = uc_fn_arg(1); static bool enabled = false; + struct ubus_context *ctx; + bool cur_en; + + cur_en = ucv_type(name) == UC_STRING; + ctx = ucv_resource_data(ubus, "ubus.connection"); + if (!ctx) + cur_en = false; - if (enabled == val) + if (enabled == cur_en) return ucv_boolean_new(true); - enabled = val; - if (val) { + enabled = cur_en; + if (enabled) { + udebug_service = strdup(ucv_string_get(name)); udebug_init(&ud); udebug_auto_connect(&ud, NULL); - udebug_buf_init(&ud_log, 1024, 64 * 1024); - udebug_buf_add(&ud, &ud_log, &meta_log); - udebug_buf_init(&ud_nl[0], 1024, 256 * 1024); - udebug_buf_add(&ud, &ud_nl[0], &meta_nl_rx); - udebug_buf_init(&ud_nl[1], 1024, 64 * 1024); - udebug_buf_add(&ud, &ud_nl[1], &meta_nl_tx); - udebug_buf_init(&ud_nl[2], 256, 32 * 1024); - udebug_buf_add(&ud, &ud_nl[2], &meta_nl_ll); - - wpa_printf_hook = udebug_printf_hook; - wpa_hexdump_hook = udebug_hexdump_hook; - wpa_netlink_hook = udebug_netlink_hook; + udebug_ubus_init(&ud_ubus, ctx, udebug_service, wpa_udebug_config); } else { - for (size_t i = 0; i < ARRAY_SIZE(ud_nl); i++) - udebug_buf_free(&ud_nl[i]); - udebug_buf_free(&ud_log); + udebug_ubus_free(&ud_ubus); + for (size_t i = 0; i < ARRAY_SIZE(udebug_rings); i++) + if (udebug_buf_valid(udebug_rings[i].buf)) + udebug_buf_free(udebug_rings[i].buf); udebug_free(&ud); - wpa_printf_hook = NULL; - wpa_hexdump_hook = NULL; - wpa_netlink_hook = NULL; + free(udebug_service); } return ucv_boolean_new(true); -- 2.30.2