Skip to contentSkip to conversation

batctl: Add generic JSON interface

PATCH In review Alexander Sarmanow 2 participants 4 messages May 13, 2021 – May 14, 2021 Kbuild

lore
Share & tools
lore
#0 lore raw

v3 cover batctl: Add generic JSON interface

For the missing JSON debug print a generic JSON print interface is proposed.
A specific nla_policy is describing (a) the key name of the entry and
(b) the corresponding callback function for printing the value. If the netlink attribute is not at disposal no entry for that will be printed.

This patch reffering to following patches:

[1] https://lists.open-mesh.org/mailman3/hyperkitty/list/[email protected]/thread/SAQYUZQ7I7H7VWHFPVLBUDYCFX7HNWLO/
[2] https://lists.open-mesh.org/mailman3/hyperkitty/list/[email protected]/thread/IV2OFJ2KJX52K4ARZ5MIWZ2A42WOKJRO/
[3] https://lists.open-mesh.org/mailman3/hyperkitty/list/[email protected]/thread/EYQBFU5O7E3KKRG2YVPLRGHHN3OFIPYP/
[4] https://lists.open-mesh.org/mailman3/hyperkitty/list/[email protected]/thread/WKIBP64G27O23MBAPHECSKNJMUESWFAG/
[5] https://lists.open-mesh.org/mailman3/hyperkitty/list/[email protected]/thread/Y3XXIYCJWME4CQNWRBIRYJ6ZOL4MI2FT/
[6] https://lists.open-mesh.org/mailman3/hyperkitty/list/[email protected]/thread/LYMU5ZA3I6YVHHRZVY3RJJR4KDHF76QI/

Alexander Sarmanow (2):
batctl: netlink: Make netlink_query_common non-static
batctl: genl_json: Add generic JSON interface

 Makefile    |   1 +
 genl_json.c | 359 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 genl_json.h |  24 ++++
 netlink.c   |  12 +-
 netlink.h   |  14 ++
 5 files changed, 402 insertions(+), 8 deletions(-)
 create mode 100644 genl_json.c
 create mode 100644 genl_json.h
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/2 batctl: netlink: Make netlink_query_common non-static

With a view to the introduction of a JSON debug support the netlink_query_common can be used for simple query of batman-adv atttirubtes without any filters.

---
 netlink.c | 12 ++++--------
 netlink.h |  8 ++++++++
 2 files changed, 12 insertions(+), 8 deletions(-)
netlink.c +4 -8
index eb5888b..2c45645 100644
@@ -29,10 +29,6 @@
 #include "functions.h"
 #include "main.h"
 
-struct nlquery_opts {
-	int err;
-};
-
 struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = {
 	[BATADV_ATTR_VERSION] = {
 		.type = NLA_STRING,
@@ -623,10 +619,10 @@ static int nlquery_stop_cb(struct nl_msg *msg, void *arg)
 	return NL_STOP;
 }
 
-static int netlink_query_common(struct state *state,
-				unsigned int mesh_ifindex, uint8_t nl_cmd,
-				nl_recvmsg_msg_cb_t callback, int flags,
-				struct nlquery_opts *query_opts)
+int netlink_query_common(struct state *state,
+			 unsigned int mesh_ifindex, uint8_t nl_cmd,
+			 nl_recvmsg_msg_cb_t callback, int flags,
+			 struct nlquery_opts *query_opts)
 {
 	struct nl_msg *msg;
 	struct nl_cb *cb;
netlink.h +8 -0
index 2cc5862..c93f500 100644
@@ -25,6 +25,10 @@ struct print_opts {
 	uint8_t nl_cmd;
 };
 
+struct nlquery_opts {
+	int err;
+};
+
 struct ether_addr;
 
 int netlink_create(struct state *state);
@@ -54,6 +58,10 @@ int netlink_print_error(struct sockaddr_nl *nla, struct nlmsgerr *nlerr,
 			void *arg);
 void netlink_print_remaining_header(struct print_opts *opts);
 
+int netlink_query_common(struct state *state, unsigned int mesh_ifindex,
+			 uint8_t nl_cmd, nl_recvmsg_msg_cb_t callback, int flags,
+			 struct nlquery_opts *query_opts);
+
 extern char algo_name_buf[256];
 extern int last_err;
 extern int64_t mcast_flags;
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 2/2 batctl: genl_json: Add generic JSON interface

In reply to Alexander Sarmanow

To print JSON strings in a generic way a second nla_policy is used, with a name (dict key) and a callback function for each entry corresponding to its formats.
There are multiple callback functions created, depending on the NLA type.

---
 Makefile    |   1 +
 genl_json.c | 359 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 genl_json.h |  24 ++++
 netlink.h   |   6 +
 4 files changed, 390 insertions(+)
 create mode 100644 genl_json.c
 create mode 100644 genl_json.h
Makefile +1 -0
index 98bf695..cee4f0c 100755
@@ -16,6 +16,7 @@ obj-y += bat-hosts.o
 obj-y += debug.o
 obj-y += functions.o
 obj-y += genl.o
+obj-y += genl_json.o
 obj-y += hash.o
 obj-y += icmp_helper.o
 obj-y += main.o
genl_json.c new file +359 -0

359 lines not on this page · raw message

genl_json.h new file +24 -0
new file mode 100644
index 0000000..0336b40
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) B.A.T.M.A.N. contributors:
+ *
+ * Alexander Sarmanow <[email protected]>
+ *
+ * License-Filename: LICENSES/preferred/GPL-2.0
+ */
+
+#ifndef _BATCTL_GENLJSON_H
+#define _BATCTL_GENLJSON_H
+
+#include <stdint.h>
+
+#include "netlink.h"
+
+struct json_opts {
+	uint8_t is_first;
+	struct nlquery_opts query_opts;
+};
+
+void netlink_print_json_entries(struct nlattr *attrs[], struct json_opts *json_opts);
+void sanitize_string(const char *str);
+
+#endif /* _BATCTL_GENLJSON_H */
netlink.h +6 -0
index c93f500..d96935a 100644
@@ -29,6 +29,11 @@ struct nlquery_opts {
 	int err;
 };
 
+struct nla_policy_json {
+	const char *name;
+	void (*cb)(struct nlattr *attrs[], int idx);
+};
+
 struct ether_addr;
 
 int netlink_create(struct state *state);
@@ -44,6 +49,7 @@ int get_algoname_netlink(struct state *state, unsigned int mesh_ifindex,
 			 char *algoname, size_t algoname_len);
 
 extern struct nla_policy batadv_netlink_policy[];
+extern struct nla_policy_json batadv_netlink_policy_json[];
 
 int missing_mandatory_attrs(struct nlattr *attrs[], const int mandatory[],
 			    int num);
Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v3 2/2] batctl: genl_json: Add generic JSON interface

Quoted 1 line · On Thursday, 13 May 2021 16:02:34 CEST Alexander Sarmanow wrote:

To print JSON strings in a generic way a second nla_policy is used, with

This is not a nla_policy but a private json translation "policy".

[...]

Quoted 13 lines

new file mode 100644
index 0000000..cff54d8

genl_json.c new file +8 -0
@@ -0,0 +1,359 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) B.A.T.M.A.N. contributors:
+ *
+ * Alexander Sarmanow <[email protected]>
+ *
+ * License-Filename: LICENSES/preferred/GPL-2.0
+ */
+

genl_json.c line 18

Quoted 10 lines
genl_json.c new file +10 -0
+#include <stdbool.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <netlink/netlink.h>
+#include <netlink/attr.h>
+
+#include "genl_json.h"
+#include "batman_adv.h"
+#include "netlink.h"
+#include "main.h"

#include "genl_json.h"
#include "main.h"

#include <ctype.h>
#include <inttypes.h>
#include <netlink/netlink.h>
#include <netlink/attr.h>
#include <stdbool.h>
#include <stdio.h>

#include "batman_adv.h"
#include "netlink.h"

genl_json.c line 36

Quoted 6 lines
genl_json.c new file +6 -0
+static void nljson_print_uint8_t(struct nlattr *attrs[], int idx)
+{
+	uint8_t value = nla_get_u8(attrs[idx]);
+
+	printf("%u", value);
+}

"%"PRIu8

genl_json.c line 43

Quoted 7 lines
genl_json.c new file +7 -0
+
+static void nljson_print_uint16_t(struct nlattr *attrs[], int idx)
+{
+	uint16_t value = nla_get_u16(attrs[idx]);
+
+	printf("%u", value);
+}

"%"PRIu16

genl_json.c line 50

Quoted 7 lines
genl_json.c new file +7 -0
+
+static void nljson_print_uint32_t(struct nlattr *attrs[], int idx)
+{
+	uint32_t value = nla_get_u32(attrs[idx]);
+
+	printf("%u", value);
+}

"%"PRIu32

genl_json.c line 57

Quoted 7 lines
genl_json.c new file +7 -0
+
+static void nljson_print_uint64_t(struct nlattr *attrs[], int idx)
+{
+	uint64_t value = nla_get_u64(attrs[idx]);
+
+	printf("%llu", value);
+}

"%"PRIu64

genl_json.c line 65

Quoted 8 lines
genl_json.c new file +8 -0
+
+static void nljson_print_mac(struct nlattr *attrs[], int idx)
+{
+	uint8_t *value = nla_data(attrs[idx]);
+
+	printf("\"%02x:%02x:%02x:%02x:%02x:%02x\"",
+		value[0], value[1], value[2], value[3], value[4], value[5]);
+}

Alignment

genl_json.c line 71

Quoted 5 lines
genl_json.c new file +5 -0
+static void nljson_print_flag(struct nlattr *attrs[], int idx)
+{
+	if (nla_get_flag(attrs[idx]))
+		printf("true");
+}

The check is not necessary because it check only if attrs[idx] is != NULL - which you've already checked. If this would not be the case, then this function would create broken json like '{"foobar":}', right?

Quoted 4 lines
genl_json.c new file +4 -0
+
+void sanitize_string(const char *str)
+{
+	while (*str) {

genl_json.c line 85

Quoted 10 lines
genl_json.c new file +10 -0
+		if (*str == '"' || *str == '\\') {
+			putchar('\\');
+			putchar(*str);
+		} else if (*str == '\\') {
+			printf("\\\\");
+		} else if (!isprint(*str)) {
+			printf("\\x%02x", *str);
+		} else {
+			printf("%c", *str);
+		}
		if (*str == '"' || *str == '\\') {
			putchar('\\');
			putchar(*str);
		} else if (!isprint(*str)) {
			printf("\\x%02x", *str);
		} else {
			putchar(*str);
		}

genl_json.c line 88

Quoted 3 lines
genl_json.c new file +3 -0
+		str++;
+	}
+}

This function should have been static and at the beginning of this file.

genl_json.c line 96

Quoted 8 lines
genl_json.c new file +8 -0
+
+void netlink_print_json_entries(struct nlattr *attrs[], struct json_opts *json_opts)
+{
+	bool first_valid_attr = true;
+	int i;
+
+	if (!json_opts->is_first)
+		printf(",");

Code to handle the json_opts->is_first = false was again not added to this function.

Quoted 14 lines
genl_json.c new file +14 -0
+
+	printf("{");
+	for (i = 0; i < BATADV_ATTR_MAX + 1; i++) {
+		if (!attrs[i])
+			continue;
+
+		if (!batadv_netlink_policy_json[i].cb)
+			continue;
+
+		if (!first_valid_attr)
+			printf(",");
+
+		putc('"', stdout);
+		sanitize_string(batadv_netlink_policy_json[i].name);

genl_json.c line 120

Quoted 10 lines
genl_json.c new file +10 -0
+		printf("\":");
+		batadv_netlink_policy_json[i].cb(attrs, i);
+
+		first_valid_attr = false;
+	}
+	printf("}");
+}
+
+
+struct nla_policy_json batadv_netlink_policy_json[NUM_BATADV_ATTR] = {

this should have been static const

genl_json.c line 175

Quoted 3 lines
genl_json.c new file +3 -0
+	[BATADV_ATTR_PAD] = {
+		.name = "pad",
+	},

I would guess we can ignore this one.

genl_json.c line 203

Quoted 4 lines
genl_json.c new file +4 -0
+	[BATADV_ATTR_TT_FLAGS] = {
+		.name = "tt_flags",
+		.cb = nljson_print_uint32_t,
+	},

Why was the actually decoding removed?

genl_json.c line 258

Quoted 3 lines
genl_json.c new file +3 -0
+	[BATADV_ATTR_DAT_CACHE_IP4ADDRESS] = {
+		.name = "dat_cache_ip4_address",
+	},

IPv4 print code missing

[...]

genl_json.c line 278

Quoted 4 lines
genl_json.c new file +4 -0
+	[BATADV_ATTR_VLANID] = {
+		.name = "vlan_id",
+		.cb = nljson_print_uint16_t,
+	},

wasn't this originally printed in a special way?

genl_json.c line 282

Quoted 4 lines
genl_json.c new file +4 -0
+	[BATADV_ATTR_AGGREGATED_OGMS_ENABLED] = {
+		.name = "aggregated_ogms_enabled",
+		.cb = nljson_print_uint8_t,
+	},

This and other _ENABLED are booleans.

[...]

There were also different other weird names used. Like fragmented_enabled for the fragmentation feature.

Quoted 13 lines
genl_json.h new file +7 -0
new file mode 100644
index 0000000..0336b40
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) B.A.T.M.A.N. contributors:
+ *
+ * Alexander Sarmanow <[email protected]>
+ *
+ * License-Filename: LICENSES/preferred/GPL-2.0
+ */

genl_json.h line 17

Quoted 10 lines
genl_json.h new file +10 -0
+
+#ifndef _BATCTL_GENLJSON_H
+#define _BATCTL_GENLJSON_H
+
+#include <stdint.h>
+
+#include "netlink.h"
+
+struct json_opts {
+	uint8_t is_first;

is_first:1

Quoted 12 lines
genl_json.h new file +7 -0
+	struct nlquery_opts query_opts;
+};
+
+void netlink_print_json_entries(struct nlattr *attrs[], struct json_opts *json_opts);
+void sanitize_string(const char *str);
+
+#endif /* _BATCTL_GENLJSON_H */
netlink.h +0 -0
index c93f500..d96935a 100644

netlink.h struct nlquery_opts { · line 36

Quoted 8 lines
netlink.h +5 -0
@@ -29,6 +29,11 @@ struct nlquery_opts {
 	int err;
 };
 
+struct nla_policy_json {
+	const char *name;
+	void (*cb)(struct nlattr *attrs[], int idx);
+};
+

This doesn't belong to netlink.h. If it would really be needed outside of genl_json.c then you should have added it to genl_json.h

netlink.h int get_algoname_netlink(struct state *state, unsigned int mesh_ifindex, · line 52

Quoted 8 lines
netlink.h +1 -0
 struct ether_addr;
 
 int netlink_create(struct state *state);
@@ -44,6 +49,7 @@ int get_algoname_netlink(struct state *state, unsigned int mesh_ifindex,
 			 char *algoname, size_t algoname_len);
 
 extern struct nla_policy batadv_netlink_policy[];
+extern struct nla_policy_json batadv_netlink_policy_json[];

This doesn't belong to netlink.h. If it would really be needed outside of genl_json.c then you should have added it to genl_json.h

Kind regards,
Sven