batctl: Add JSON debug support
PATCH 1/1 batctl: Add JSON debug support
A JSON output of the debug tables is still missing. Corresponding JSON output is added for originators, neighbors, translocal, transglobal and interfaces tables. Same parameters of the debug tables can be used for the JSON, except the "-w [interval]" (not useful). The table header is implemented as a JSON equivalent and can be also optionally omitted.
--- Makefile | 5 ++ backbonetable.c | 2 +- claimtable.c | 2 +- dat_cache.c | 2 +- debug.c | 15 +++- gateways.c | 4 +- interfaces_json.c | 167 +++++++++++++++++++++++++++++++++++++++++++++ main.c | 14 ++-- main.h | 3 + mcast_flags.c | 4 +- neighbors.c | 2 +- neighbors_json.c | 109 +++++++++++++++++++++++++++++ netlink.c | 70 +++++++++++++++---- netlink.h | 9 ++- originators.c | 4 +- originators_json.c | 154 +++++++++++++++++++++++++++++++++++++++++ transglobal.c | 2 +- transglobal_json.c | 146 +++++++++++++++++++++++++++++++++++++++ translocal.c | 2 +- translocal_json.c | 139 +++++++++++++++++++++++++++++++++++++ 20 files changed, 823 insertions(+), 32 deletions(-) create mode 100644 interfaces_json.c create mode 100644 neighbors_json.c create mode 100644 originators_json.c create mode 100644 transglobal_json.c create mode 100644 translocal_json.c
Makefile +5 -0
@@ -71,6 +71,11 @@ $(eval $(call add_command,traceroute,y)) $(eval $(call add_command,transglobal,y)) $(eval $(call add_command,translate,y)) $(eval $(call add_command,translocal,y)) +$(eval $(call add_command,originators_json,y)) +$(eval $(call add_command,neighbors_json,y)) +$(eval $(call add_command,translocal_json,y)) +$(eval $(call add_command,transglobal_json,y)) +$(eval $(call add_command,interfaces_json,y)) MANPAGE = man/batctl.8
backbonetable.c +1 -1
@@ -98,7 +98,7 @@ static int netlink_print_bla_backbone(struct state *state, char *orig_iface, orig_timeout, watch_interval,"Originator VID last seen (CRC )\n" ,BATADV_CMD_GET_BLA_BACKBONE , - bla_backbone_callback); + bla_backbone_callback, false); }static struct debug_table_data batctl_debug_table_backbonetable = {
claimtable.c +1 -1
@@ -103,7 +103,7 @@ static int netlink_print_bla_claim(struct state *state, char *orig_iface, orig_timeout, watch_interval,"Client VID Originator [o] (CRC )\n" ,BATADV_CMD_GET_BLA_CLAIM , - bla_claim_callback); + bla_claim_callback, false); }static struct debug_table_data batctl_debug_table_claimtable = {
dat_cache.c +1 -1
@@ -116,7 +116,7 @@ static int netlink_print_dat_cache(struct state *state, char *orig_iface, ret =netlink_print_common (state, orig_iface, read_opts, orig_timeout, watch_interval, header,BATADV_CMD_GET_DAT_CACHE , - dat_cache_callback); + dat_cache_callback, false);free (header);return ret;
debug.c +14 -1
@@ -27,7 +27,9 @@ static void debug_table_usage(struct state *state)fprintf (stderr," \t -h print this help\n" );fprintf (stderr," \t -n don't replace mac addresses with bat-host names\n" );fprintf (stderr," \t -H don't show the header\n" ); -fprintf (stderr," \t -w [interval] watch mode - refresh the table continuously\n" ); + +if (state->cmd->type !=DEBUGJSON ) +fprintf (stderr," \t -w [interval] watch mode - refresh the table continuously\n" );if (debug_table->option_timeout_interval)fprintf (stderr," \t -t timeout interval - don't print originators not seen for x.y seconds \n" );
@@ -60,6 +62,12 @@ int handle_debug_table(struct state *state, int argc, char **argv) read_opt &= ~USE_BAT_HOSTS ;break ;case 'w' : +if (state->cmd->type ==DEBUGJSON ) { +fprintf (stderr,"Error - unrecognised option '-%c'\n" , optchar); +debug_table_usage (state); +return EXIT_FAILURE ; + } + read_opt |=CLR_CONT_READ ;if (optarg[0 ] =='-' ) { optind--;
@@ -123,6 +131,11 @@ int handle_debug_table(struct state *state, int argc, char **argv) }else if (optopt =='i' ) {fprintf (stderr,"Error - option '-i' needs an interface as argument\n" ); }else if (optopt =='w' ) { +if (state->cmd->type ==DEBUGJSON ) { +fprintf (stderr,"Error - unrecognised option '-w'\n" ); +debug_table_usage (state); +return EXIT_FAILURE ; + } read_opt |=CLR_CONT_READ ;break ; }
gateways.c +2 -2
@@ -123,7 +123,7 @@ static int netlink_print_gateways(struct state *state, char *orig_iface,/* only parse routing algorithm name */ last_err = -EINVAL ; info_header =netlink_get_info (state->mesh_ifindex, -BATADV_CMD_GET_ORIGINATORS ,NULL ); +BATADV_CMD_GET_ORIGINATORS ,NULL , false);free (info_header);if (strlen (algo_name_buf) ==0 )
@@ -141,7 +141,7 @@ static int netlink_print_gateways(struct state *state, char *orig_iface, orig_timeout, watch_interval, header,BATADV_CMD_GET_GATEWAYS , - gateways_callback); + gateways_callback, false); }static struct debug_table_data batctl_debug_table_gateways = {
interfaces_json.c new file +167 -0
167 lines not on this page · raw message
main.c +10 -4
@@ -43,6 +43,10 @@ static void print_usage(void) .label = "debug tables: \tdisplay the corresponding debug table\n", .types = BIT(DEBUGTABLE), }, + { + .label = "debug JSONs: \tdisplay the corresponding debug JSON\n", + .types = BIT(DEBUGJSON), + }, };const char *default_prefixes[] = {"" ,
@@ -67,9 +71,9 @@ static void print_usage(void)char buf[64 ];size_t i; -fprintf (stderr,"Usage: batctl [options] command|debug table [parameters]\n" ); +fprintf (stderr,"Usage: batctl [options] command|debug table|debug json [parameters]\n" );fprintf (stderr,"options:\n" ); -fprintf (stderr," \t-h print this help (or 'batctl <command|debug table> -h' for the parameter help)\n" ); +fprintf (stderr," \t-h print this help (or 'batctl <command|debug table|debug json> -h' for the parameter help)\n" );fprintf (stderr," \t-v print version\n" );for (i =0 ; i <sizeof (type) /sizeof (*type); i++) {
@@ -87,6 +91,7 @@ static void print_usage(void)continue ;switch (cmd->type) { +case DEBUGJSON :case DEBUGTABLE :case SUBCOMMAND_MIF : prefixes = meshif_prefixes;
@@ -167,7 +172,8 @@ static const struct command *find_command(struct state *state, const char *name)/* fall through */ case SP_MESHIF : types |=BIT (SUBCOMMAND_MIF ) | -BIT (DEBUGTABLE ); +BIT (DEBUGTABLE ) | +BIT (DEBUGJSON );break ;case SP_VLAN : types =BIT (SUBCOMMAND_VID );
@@ -380,7 +386,7 @@ int main(int argc, char **argv) cmd =find_command (&state, argv[0 ]);if (!cmd) {fprintf (stderr, -"Error - no valid command or debug table specified: %s\n" , +"Error - no valid command or debug table/JSON specified: %s\n" , argv[0 ]); goto err; }
main.h +3 -0
@@ -44,6 +44,8 @@ extern char module_ver_path[];#define ARRAY_SIZE (x) (sizeof(x) / sizeof(*(x))) +#define BOOL_STRING (x) (x ? "true" : "false") +#ifndef container_of#define container_of (ptr, type, member) __extension__ ({ \ const __typeof__(((type *)0)->member) *__pmember = (ptr); \
@@ -69,6 +71,7 @@ enum command_type {SUBCOMMAND_VID ,SUBCOMMAND_HIF ,DEBUGTABLE , + DEBUGJSON, }; struct state {
mcast_flags.c +2 -2
@@ -105,7 +105,7 @@ static int netlink_print_mcast_flags(struct state *state, char *orig_iface,/* only parse own multicast flags */ info_header =netlink_get_info (state->mesh_ifindex, -BATADV_CMD_GET_MCAST_FLAGS ,NULL ); +BATADV_CMD_GET_MCAST_FLAGS ,NULL , false);free (info_header);if (mcast_flags == -EOPNOTSUPP || mcast_flags_priv == -EOPNOTSUPP )
@@ -147,7 +147,7 @@ static int netlink_print_mcast_flags(struct state *state, char *orig_iface, ret =netlink_print_common (state, orig_iface, read_opts, orig_timeout, watch_interval, header,BATADV_CMD_GET_MCAST_FLAGS , - mcast_flags_callback); + mcast_flags_callback, false);free (header);return ret;
neighbors.c +1 -1
@@ -114,7 +114,7 @@ static int netlink_print_neighbors(struct state *state, char *orig_iface, orig_timeout, watch_interval,"IF Neighbor last-seen\n" ,BATADV_CMD_GET_NEIGHBORS , - neighbors_callback); + neighbors_callback, false); }static struct debug_table_data batctl_debug_table_neighbors = {
neighbors_json.c new file +109 -0
109 lines not on this page · raw message
netlink.c +57 -13
123 lines not on this page · raw message
netlink.h +7 -2
@@ -12,6 +12,7 @@#include <netlink/genl/genl.h> #include <netlink/genl/ctrl.h> #include <stdint.h> +#include <stdbool.h> struct state ;
@@ -21,8 +22,12 @@ struct print_opts {float watch_interval;nl_recvmsg_msg_cb_t callback;char *remaining_header; +char *remaining_entry;const char *static_header;uint8_t nl_cmd; +bool is_json; +bool is_first; +bool is_last; };struct ether_addr ;
@@ -30,7 +35,7 @@ struct ether_addr;int netlink_create (struct state *state);void netlink_destroy (struct state *state); -char *netlink_get_info (int ifindex,uint8_t nl_cmd,const char *header); +char *netlink_get_info (int ifindex,uint8_t nl_cmd,const char *header,bool is_json);int translate_mac_netlink (const char *mesh_iface,const struct ether_addr *mac,struct ether_addr *mac_out); int get_nexthop_netlink(const char *mesh_iface, const struct ether_addr *mac,
@@ -46,7 +51,7 @@ int missing_mandatory_attrs(struct nlattr *attrs[], const int mandatory[],int netlink_print_common (struct state *state,char *orig_iface,int read_opt,float orig_timeout,float watch_interval,const char *header,uint8_t nl_cmd, -nl_recvmsg_msg_cb_t callback); +nl_recvmsg_msg_cb_t callback,bool is_json);int netlink_print_common_cb (struct nl_msg *msg,void *arg);int netlink_stop_callback (struct nl_msg *msg,void *arg);
originators.c +2 -2
@@ -175,7 +175,7 @@ static int netlink_print_originators(struct state *state, char *orig_iface,/* only parse routing algorithm name */ last_err = -EINVAL ; info_header =netlink_get_info (state->mesh_ifindex, -BATADV_CMD_GET_ORIGINATORS ,NULL ); +BATADV_CMD_GET_ORIGINATORS ,NULL , false);free (info_header);if (strlen (algo_name_buf) ==0 )
@@ -192,7 +192,7 @@ static int netlink_print_originators(struct state *state, char *orig_iface,return netlink_print_common (state, orig_iface, read_opts, orig_timeout, watch_interval, header,BATADV_CMD_GET_ORIGINATORS , - originators_callback); + originators_callback, false); }static struct debug_table_data batctl_debug_table_originators = {
originators_json.c new file +154 -0
154 lines not on this page · raw message
transglobal.c +1 -1
@@ -132,7 +132,7 @@ static int netlink_print_transglobal(struct state *state, char *orig_iface, orig_timeout, watch_interval," Client VID Flags Last ttvn Via ttvn (CRC )\n" ,BATADV_CMD_GET_TRANSTABLE_GLOBAL , - transglobal_callback); + transglobal_callback, false); }static struct debug_table_data batctl_debug_table_transglobal = {
transglobal_json.c new file +146 -0
146 lines not on this page · raw message
translocal.c +1 -1
@@ -128,7 +128,7 @@ static int netlink_print_translocal(struct state *state, char *orig_iface, orig_timeout, watch_interval,"Client VID Flags Last seen (CRC )\n" ,BATADV_CMD_GET_TRANSTABLE_LOCAL , - translocal_callback); + translocal_callback, false); }static struct debug_table_data batctl_debug_table_translocal = {
translocal_json.c new file +139 -0
139 lines not on this page · raw message
Signature · 1 line
2.25.1
Quoted 3 lines · On Wednesday, 28 April 2021 12:16:08 CEST Alexander Sarmanow wrote:
A JSON output of the debug tables is still missing. Corresponding JSON output is added for originators, neighbors, translocal, transglobal and interfaces tables.
The interface list is not a debug table.
Btw. you print strings without making sure it doesn't break the json. There is something like this in alfred:
for (i = 0; i < data_len; i++) {
if (pos[i] == '"')
printf("\\\"");
else if (pos[i] == '\\')
printf("\\\\");
else if (!isprint(pos[i]))
printf("\\x%02x", pos[i]);
else
printf("%c", pos[i]);
}Quoted 4 lines
Same parameters of the debug tables can be used for
the JSON, except the "-w [interval]" (not useful). The table header is implemented as a JSON equivalent and can be also optionally omitted.
I would like to disagree. Why is it a problem to continuously emit json objects?
netlink.h struct print_opts { · line 30
Quoted 10 lines
netlink.h +4
-0@@ -21,8 +22,12 @@ struct print_opts {float watch_interval;nl_recvmsg_msg_cb_t callback;char *remaining_header; +char *remaining_entry;const char *static_header;uint8_t nl_cmd; +bool is_json; +bool is_first; +bool is_last;
Please don't use bools here. Change it to something more like
uint8_t is_json:1;
uint8_t is_first:1;
uint8_t is_last:1;Makefile $(eval $(call add_command,traceroute,y)) · line 78
Quoted 5 lines
Makefile +5
-0+$(eval $(call add_command,originators_json,y)) +$(eval $(call add_command,neighbors_json,y)) +$(eval $(call add_command,translocal_json,y)) +$(eval $(call add_command,transglobal_json,y)) +$(eval $(call add_command,interfaces_json,y))
Please inserted it sorted.
Quoted 28 lines
netlink.c +13
-9@@ -318,15 +318,29 @@ static int info_callback(struct nl_msg *msg, void *arg)else extra_header ="" ; - ret =asprintf (&opts->remaining_header, -"[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%02x:%02x:%02x:%02x:%02x:%02x (%s/%02x:%02x:%02x:%02x:%02x:%02x %s)%s]\n%s" , - version, primary_if, - primary_mac[0 ], primary_mac[1 ], primary_mac[2 ], - primary_mac[3 ], primary_mac[4 ], primary_mac[5 ], - mesh_name, - mesh_mac[0 ], mesh_mac[1 ], mesh_mac[2 ], - mesh_mac[3 ], mesh_mac[4 ], mesh_mac[5 ], - algo_name, extra_info, extra_header); +if (opts->is_json) { + ret =asprintf (&opts->remaining_header, +"{\"version\":\"%s\",\"main_if\":\"%s\",\"main_mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\",\"mesh_if\":\"%s\",\"mesh_mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\",\"algo_name\":\"%s\",\"extra_info\":\"%s\",\"data\":[" , + version, primary_if, + primary_mac[0 ], primary_mac[1 ], + primary_mac[2 ], primary_mac[3 ], + primary_mac[4 ], primary_mac[5 ], + mesh_name, + mesh_mac[0 ], mesh_mac[1 ], mesh_mac[2 ], + mesh_mac[3 ], mesh_mac[4 ], mesh_mac[5 ], + algo_name, extra_info); + }else { + ret = asprintf(&opts->remaining_header,
netlink.c static int info_callback(struct nl_msg *msg, void *arg) · line 343
Quoted 10 lines
netlink.c +10
-0+"[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%02x:%02x:%02x:%02x:%02x:%02x (%s/%02x:%02x:%02x:%02x:%02x:%02x %s)%s]\n%s" , + version, primary_if, + primary_mac[0 ], primary_mac[1 ], + primary_mac[2 ], primary_mac[3 ], + primary_mac[4 ], primary_mac[5 ], + mesh_name, + mesh_mac[0 ], mesh_mac[1 ], mesh_mac[2 ], + mesh_mac[3 ], mesh_mac[4 ], mesh_mac[5 ], + algo_name, extra_info, extra_header); + }
Do we really have to add such kind of output to each table? Can't we just have another command to get the meshif info and print it?
Regarding the actual command code - why do we have to have manual print code? Can't we just define a structures how things should be printed and then have a semi-generic print function for netlink responses (which just needs the netlink to json definition)? This would also avoid this extremely ugly print which you have and avoids implementation errors for new tables.
There is most likely more stuff in there but I will stop the review for now.
Kind regards,
Sven
Quoted 7 lines · Sven Eckelmann
Quoted 4 lines · On Wednesday, 28 April 2021 12:16:08 CEST Alexander Sarmanow wrote:
Same parameters of the debug tables can be used for
the JSON, except the "-w [interval]" (not useful). The table header is implemented as a JSON equivalent and can be also optionally omitted.I would like to disagree. Why is it a problem to continuously emit json objects?
Simon agreed with me to leave that out, but I can add it and the user can decide whether to use it or not. It does not disturb.
Quoted 41 lines
Quoted 40 lines
---
a/netlink.c
+++b/netlink.c
@@ -318,15 +318,29 @@ static intinfo_callback(struct nl_msg *msg, void *arg)else extra_header = "";- ret = asprintf(&opts->remaining_header,
- "[B.A.T.M.A.N. adv %s, MainIF/MAC:
%s/%02x:%02x:%02x:%02x:%02x:%02x (%s/%02x:%02x:%02x:%02x:%02x:%02x %s)%s]\n%s",- version, primary_if, - primary_mac[0], primary_mac[1], primary_mac[2], - primary_mac[3], primary_mac[4], primary_mac[5], - mesh_name, - mesh_mac[0], mesh_mac[1], mesh_mac[2], - mesh_mac[3], mesh_mac[4], mesh_mac[5], - algo_name, extra_info, extra_header);+ if (
opts->is_json) {
+ ret = asprintf(&opts->remaining_header,
+
"{\"version\":\"%s\",\"main_if\":\"%s\",\"main_mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\",\"mesh_if\":\"%s\",\"mesh_mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\",\"algo_name\":\"%s\",\"extra_info\":\"%s\",\"data\":[",+ version, primary_if, + primary_mac[0], primary_mac[1], + primary_mac[2], primary_mac[3], + primary_mac[4], primary_mac[5], + mesh_name, + mesh_mac[0], mesh_mac[1], mesh_mac[2], + mesh_mac[3], mesh_mac[4], mesh_mac[5], + algo_name, extra_info);+ } else {
+ ret = asprintf(&opts->remaining_header,
+ "[B.A.T.M.A.N. adv %s, MainIF/MAC:
%s/%02x:%02x:%02x:%02x:%02x:%02x (%s/%02x:%02x:%02x:%02x:%02x:%02x %s)%s]\n%s",+ version, primary_if, + primary_mac[0], primary_mac[1], + primary_mac[2], primary_mac[3], + primary_mac[4], primary_mac[5], + mesh_name, + mesh_mac[0], mesh_mac[1], mesh_mac[2], + mesh_mac[3], mesh_mac[4], mesh_mac[5], + algo_name, extra_info, extra_header);Quoted 1 line
+ }
Quoted 2 lines
Do we really have to add such kind of output to each table? Can't we just have another command to get the meshif info and print it?
You are right. I just followed the existing debug tables where the header is also always printed out each time and there is no separate command for that. But I can create a separate command to fetch the "header".
Best,
Alex