From patchwork Sat Feb 9 13:42:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 17793 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from open-mesh.org (localhost [IPv6:::1]) by open-mesh.org (Postfix) with ESMTP id E9FBF81CAB; Sat, 9 Feb 2019 14:43:08 +0100 (CET) Authentication-Results: open-mesh.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=narfation.org header.i=@narfation.org header.b="moOGzuJT"; dkim-atps=neutral Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=2001:4d88:2000:7::2; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver= Received: from v3-1039.vlinux.de (narfation.org [IPv6:2001:4d88:2000:7::2]) by open-mesh.org (Postfix) with ESMTPS id 10F4A81C29 for ; Sat, 9 Feb 2019 14:42:38 +0100 (CET) Received: from sven-desktop.home.narfation.org (p200300C597362EFD0000000000004065.dip0.t-ipconnect.de [IPv6:2003:c5:9736:2efd::4065]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id 7345811013D; Sat, 9 Feb 2019 14:42:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=narfation.org; s=20121; t=1549719754; bh=85HCMHJpIn+TTPMvsNBVFD+FE2jzsMsY5AnEGz7juxs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=moOGzuJT/kGO9fCMrFxuxZ6EqGCY8wEl4NaMiD4i38nG3esZxxeXWtaFq6zem88PM qW15V2Bnj2AeEwSKpuuzlyj4LIovDavlwvHj30EKz3fXl2mENd6uwzMjwJryBi85+O 8pvhR8ekjj1QZkqebAIzPkGHjozBQropH3ZnBrqQ= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Sat, 9 Feb 2019 14:42:10 +0100 Message-Id: <20190209134222.15035-9-sven@narfation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190209134222.15035-1-sven@narfation.org> References: <20190209134222.15035-1-sven@narfation.org> MIME-Version: 1.0 Subject: [B.A.T.M.A.N.] [PATCH v5 08/20] batctl: Support generic netlink for loglevel command X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking Cc: Marek Lindner Errors-To: b.a.t.m.a.n-bounces@lists.open-mesh.org Sender: "B.A.T.M.A.N" sysfs should be avoided for new settings of network interfaces. To still provide a common configuration infrastructure, all the existing settings subcommands also have to be reimplemented via generic netlink while still using sysfs as fallback. The loglevel implementation is using the commands BATADV_CMD_SET_MESH/BATADV_CMD_GET_MESH to set/get the configuration of this feature using the u32 (bitmask) BATADV_ATTR_LOG_LEVEL attribute. Signed-off-by: Sven Eckelmann --- Cc: Marek Lindner --- loglevel.c | 138 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 110 insertions(+), 28 deletions(-) diff --git a/loglevel.c b/loglevel.c index de1f351..778c4b4 100644 --- a/loglevel.c +++ b/loglevel.c @@ -20,7 +20,9 @@ * License-Filename: LICENSES/preferred/GPL-2.0 */ +#include #include +#include #include #include #include @@ -31,6 +33,10 @@ #define SYS_LOG_LEVEL "log_level" +static struct log_level_data { + uint32_t log_level; +} log_level_globals; + static void log_level_usage(void) { fprintf(stderr, "Usage: batctl [options] loglevel [parameters] [level[ level[ level]]...]\n"); @@ -49,14 +55,93 @@ static void log_level_usage(void) fprintf(stderr, " \t tp Messages related to throughput meter\n"); } +static int extract_log_level(struct nl_msg *msg, void *arg) +{ + struct nlattr *attrs[BATADV_ATTR_MAX + 1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + struct genlmsghdr *ghdr; + int *result = arg; + + if (!genlmsg_valid_hdr(nlh, 0)) + return NL_OK; + + ghdr = nlmsg_data(nlh); + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + return NL_OK; + } + + if (!attrs[BATADV_ATTR_LOG_LEVEL]) + return NL_OK; + + log_level_globals.log_level = nla_get_u32(attrs[BATADV_ATTR_LOG_LEVEL]); + + *result = 0; + return NL_STOP; +} + +static int get_log_level(struct state *state) +{ + return sys_simple_nlquery(state, BATADV_CMD_GET_MESH, + NULL, extract_log_level); +} + +static int set_attrs_log_level(struct nl_msg *msg, void *arg __maybe_unused) +{ + nla_put_u32(msg, BATADV_ATTR_LOG_LEVEL, log_level_globals.log_level); + + return 0; +} + +static int set_log_level(struct state *state) +{ + return sys_simple_nlquery(state, BATADV_CMD_SET_MESH, + set_attrs_log_level, NULL); +} + +static int log_level_read_setting(struct state *state, const char *path_buff) +{ + int res; + + res = get_log_level(state); + if (res < 0 && res != -EOPNOTSUPP) + return EXIT_FAILURE; + if (res >= 0) + return EXIT_SUCCESS; + + res = read_file(path_buff, SYS_LOG_LEVEL, USE_READ_BUFF, 0, 0, 0); + if (res != EXIT_SUCCESS) + return res; + + log_level_globals.log_level = strtol(line_ptr, (char **) NULL, 10); + + return res; +} + +static int log_level_write_setting(struct state *state, const char *path_buff) +{ + int res; + char str[4]; + + res = set_log_level(state); + if (res < 0 && res != -EOPNOTSUPP) + return EXIT_FAILURE; + if (res >= 0) + return EXIT_SUCCESS; + + snprintf(str, sizeof(str), "%i", log_level_globals.log_level); + return write_file(path_buff, SYS_LOG_LEVEL, str, NULL); +} + static int loglevel(struct state *state, int argc, char **argv) { int optchar, res = EXIT_FAILURE; - int log_level = 0; char *path_buff; - char str[4]; int i; + log_level_globals.log_level = 0; + while ((optchar = getopt(argc, argv, "h")) != -1) { switch (optchar) { case 'h': @@ -81,63 +166,59 @@ static int loglevel(struct state *state, int argc, char **argv) for (i = 1; i < argc; i++) { if (strcmp(argv[i], "none") == 0) { - log_level = 0; + log_level_globals.log_level = 0; break; } else if (strcmp(argv[i], "all") == 0) { - log_level = 255; + log_level_globals.log_level = 255; break; } else if (strcmp(argv[i], "batman") == 0) - log_level |= BIT(0); + log_level_globals.log_level |= BIT(0); else if (strcmp(argv[i], "routes") == 0) - log_level |= BIT(1); + log_level_globals.log_level |= BIT(1); else if (strcmp(argv[i], "tt") == 0) - log_level |= BIT(2); + log_level_globals.log_level |= BIT(2); else if (strcmp(argv[i], "bla") == 0) - log_level |= BIT(3); + log_level_globals.log_level |= BIT(3); else if (strcmp(argv[i], "dat") == 0) - log_level |= BIT(4); + log_level_globals.log_level |= BIT(4); else if (strcmp(argv[i], "nc") == 0) - log_level |= BIT(5); + log_level_globals.log_level |= BIT(5); else if (strcmp(argv[i], "mcast") == 0) - log_level |= BIT(6); + log_level_globals.log_level |= BIT(6); else if (strcmp(argv[i], "tp") == 0) - log_level |= BIT(7); + log_level_globals.log_level |= BIT(7); else { log_level_usage(); goto out; } } - snprintf(str, sizeof(str), "%i", log_level); - res = write_file(path_buff, SYS_LOG_LEVEL, str, NULL); + log_level_write_setting(state, path_buff); goto out; } - res = read_file(path_buff, SYS_LOG_LEVEL, USE_READ_BUFF, 0, 0, 0); - + res = log_level_read_setting(state, path_buff); if (res != EXIT_SUCCESS) goto out; - log_level = strtol(line_ptr, (char **) NULL, 10); - - printf("[%c] %s (%s)\n", (!log_level) ? 'x' : ' ', + printf("[%c] %s (%s)\n", (!log_level_globals.log_level) ? 'x' : ' ', "all debug output disabled", "none"); - printf("[%c] %s (%s)\n", (log_level & BIT(0)) ? 'x' : ' ', + printf("[%c] %s (%s)\n", (log_level_globals.log_level & BIT(0)) ? 'x' : ' ', "messages related to routing / flooding / broadcasting", "batman"); - printf("[%c] %s (%s)\n", (log_level & BIT(1)) ? 'x' : ' ', + printf("[%c] %s (%s)\n", (log_level_globals.log_level & BIT(1)) ? 'x' : ' ', "messages related to route added / changed / deleted", "routes"); - printf("[%c] %s (%s)\n", (log_level & BIT(2)) ? 'x' : ' ', + printf("[%c] %s (%s)\n", (log_level_globals.log_level & BIT(2)) ? 'x' : ' ', "messages related to translation table operations", "tt"); - printf("[%c] %s (%s)\n", (log_level & BIT(3)) ? 'x' : ' ', + printf("[%c] %s (%s)\n", (log_level_globals.log_level & BIT(3)) ? 'x' : ' ', "messages related to bridge loop avoidance", "bla"); - printf("[%c] %s (%s)\n", (log_level & BIT(4)) ? 'x' : ' ', + printf("[%c] %s (%s)\n", (log_level_globals.log_level & BIT(4)) ? 'x' : ' ', "messages related to arp snooping and distributed arp table", "dat"); - printf("[%c] %s (%s)\n", (log_level & BIT(5)) ? 'x' : ' ', + printf("[%c] %s (%s)\n", (log_level_globals.log_level & BIT(5)) ? 'x' : ' ', "messages related to network coding", "nc"); - printf("[%c] %s (%s)\n", (log_level & BIT(6)) ? 'x' : ' ', + printf("[%c] %s (%s)\n", (log_level_globals.log_level & BIT(6)) ? 'x' : ' ', "messages related to multicast", "mcast"); - printf("[%c] %s (%s)\n", (log_level & BIT(7)) ? 'x' : ' ', + printf("[%c] %s (%s)\n", (log_level_globals.log_level & BIT(7)) ? 'x' : ' ', "messages related to throughput meter", "tp"); out: @@ -145,5 +226,6 @@ static int loglevel(struct state *state, int argc, char **argv) return res; } -COMMAND(SUBCOMMAND, loglevel, "ll", COMMAND_FLAG_MESH_IFACE, NULL, +COMMAND(SUBCOMMAND, loglevel, "ll", + COMMAND_FLAG_MESH_IFACE | COMMAND_FLAG_NETLINK, NULL, "[level] \tdisplay or modify the log level");