rdma: Rewrite custom JSON and prints logic to use common API
Subsystems (3)
PATCH 1/1 rdma: Rewrite custom JSON and prints logic to use common API
From: Ido Kalir <[email protected]>
Instead of doing open-coded solution to generate JSON and prints, let's reuse existing infrastructure and APIs to do the same as ip/*.
Before this change:
if (rd->json_output)
jsonw_uint_field(rd->jw, "sm_lid", sm_lid);
else
pr_out("sm_lid %u ", sm_lid);After this change:
print_uint(PRINT_ANY, "sm_lid", "sm_lid %u ", sm_lid);
All the print functions are converted to support color but for now the type of color is COLOR_NONE. This is done as a preparation to addition of color enable option. Such change will require rewrite of command line arguments parser which is out-of-scope for this patch.
--- rdma/dev.c | 67 ++++++++------------------- rdma/link.c | 98 +++++++++++----------------------------- rdma/rdma.c | 20 ++------ rdma/rdma.h | 6 +-- rdma/res-cmid.c | 38 ++++++---------- rdma/res-cq.c | 13 ++---- rdma/res-mr.c | 5 +- rdma/res-pd.c | 4 +- rdma/res-qp.c | 35 ++++---------- rdma/res.c | 71 ++++++----------------------- rdma/stat-mr.c | 4 +- rdma/stat.c | 76 ++++++++----------------------- rdma/sys.c | 7 +-- rdma/utils.c | 118 ++++++++++++++++++++++-------------------------- 14 files changed, 170 insertions(+), 392 deletions(-)
rdma/dev.c +18 -49
@@ -94,29 +94,16 @@ static void dev_print_caps(struct rd *rd, struct nlattr **tb) caps =mnl_attr_get_u64 (tb[RDMA_NLDEV_ATTR_CAP_FLAGS ]); -if (rd->json_output) { -jsonw_name (rd->jw,"caps" ); -jsonw_start_array (rd->jw); - }else { -pr_out ("\n caps: <" ); - } +print_color_string (PRINT_FP ,COLOR_NONE ,NULL ,"\n caps: <" ,NULL ); +open_json_array (PRINT_JSON ,"caps" );for (idx =0 ; caps; idx++) { -if (caps &0x1 ) { -if (rd->json_output) { -jsonw_string (rd->jw,dev_caps_to_str (idx)); - }else { -pr_out ("%s" ,dev_caps_to_str (idx)); -if (caps >>0x1 ) -pr_out (", " ); - } - } +if (caps &0x1 ) +print_color_string (PRINT_ANY ,COLOR_NONE ,NULL , + caps >>0x1 ?"%s, " :"%s" , +dev_caps_to_str (idx)); caps >>=0x1 ; } - -if (rd->json_output) -jsonw_end_array (rd->jw); -else -pr_out (">" ); +close_json_array (PRINT_ANY ,">" ); }static void dev_print_fw (struct rd *rd,struct nlattr **tb)
@@ -126,10 +113,7 @@ static void dev_print_fw(struct rd *rd, struct nlattr **tb)return ; str =mnl_attr_get_str (tb[RDMA_NLDEV_ATTR_FW_VERSION ]); -if (rd->json_output) -jsonw_string_field (rd->jw,"fw" , str); -else -pr_out ("fw %s " , str); +print_color_string (PRINT_ANY ,COLOR_NONE ,"fw" ,"fw %s " , str); }static void dev_print_node_guid (struct rd *rd,struct nlattr **tb)
@@ -144,10 +128,8 @@ static void dev_print_node_guid(struct rd *rd, struct nlattr **tb) node_guid =mnl_attr_get_u64 (tb[RDMA_NLDEV_ATTR_NODE_GUID ]);memcpy (vp, &node_guid,sizeof (uint64_t ));snprintf (str,32 ,"%04x:%04x:%04x:%04x" , vp[3 ], vp[2 ], vp[1 ], vp[0 ]); -if (rd->json_output) -jsonw_string_field (rd->jw,"node_guid" , str); -else -pr_out ("node_guid %s " , str); +print_color_string (PRINT_ANY ,COLOR_NONE ,"node_guid" ,"node_guid %s " , + str); }static void dev_print_sys_image_guid (struct rd *rd,struct nlattr **tb)
@@ -162,10 +144,8 @@ static void dev_print_sys_image_guid(struct rd *rd, struct nlattr **tb) sys_image_guid =mnl_attr_get_u64 (tb[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID ]);memcpy (vp, &sys_image_guid,sizeof (uint64_t ));snprintf (str,32 ,"%04x:%04x:%04x:%04x" , vp[3 ], vp[2 ], vp[1 ], vp[0 ]); -if (rd->json_output) -jsonw_string_field (rd->jw,"sys_image_guid" , str); -else -pr_out ("sys_image_guid %s " , str); +print_color_string (PRINT_ANY ,COLOR_NONE ,"sys_image_guid" , +"sys_image_guid %s " , str); }static void dev_print_dim_setting (struct rd *rd,struct nlattr **tb)
@@ -205,10 +185,8 @@ static void dev_print_node_type(struct rd *rd, struct nlattr **tb) node_type =mnl_attr_get_u8 (tb[RDMA_NLDEV_ATTR_DEV_NODE_TYPE ]); node_str =node_type_to_str (node_type); -if (rd->json_output) -jsonw_string_field (rd->jw,"node_type" , node_str); -else -pr_out ("node_type %s " , node_str); +print_color_string (PRINT_ANY ,COLOR_NONE ,"node_type" ,"node_type %s " , + node_str); }static int dev_parse_cb (const struct nlmsghdr *nlh,void *data)
@@ -221,15 +199,11 @@ static int dev_parse_cb(const struct nlmsghdr *nlh, void *data)mnl_attr_parse (nlh,0 , rd_attr_cb, tb);if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX ] || !tb[RDMA_NLDEV_ATTR_DEV_NAME ])return MNL_CB_ERROR ; - +open_json_object (NULL ); idx =mnl_attr_get_u32 (tb[RDMA_NLDEV_ATTR_DEV_INDEX ]); name =mnl_attr_get_str (tb[RDMA_NLDEV_ATTR_DEV_NAME ]); -if (rd->json_output) { -jsonw_uint_field (rd->jw,"ifindex" , idx); -jsonw_string_field (rd->jw,"ifname" , name); - }else { -pr_out ("%u: %s: " , idx, name); - } +print_color_uint (PRINT_ANY ,COLOR_NONE ,"ifindex" ,"%u: " , idx); +print_color_string (PRINT_ANY ,COLOR_NONE ,"ifname" ,"%s: " , name);dev_print_node_type (rd, tb);dev_print_fw (rd, tb);
@@ -240,8 +214,7 @@ static int dev_parse_cb(const struct nlmsghdr *nlh, void *data)dev_print_caps (rd, tb); } -if (!rd->json_output) -pr_out ("\n" ); +newline (rd);return MNL_CB_OK ; }
@@ -257,11 +230,7 @@ static int dev_no_args(struct rd *rd)if (ret)return ret; -if (rd->json_output) -jsonw_start_object (rd->jw); ret =rd_recv_msg (rd, dev_parse_cb, rd, seq); -if (rd->json_output) -jsonw_end_object (rd->jw);return ret; }
rdma/link.c +27 -71
175 lines not on this page · raw message
rdma/rdma.c +4 -16
@@ -6,6 +6,7 @@#include "rdma.h" #include "SNAPSHOT.h" +#include "color.h" static void help (char *name) {
@@ -85,15 +86,6 @@ static int rd_init(struct rd *rd, char *filename)INIT_LIST_HEAD (&rd->dev_map_list);INIT_LIST_HEAD (&rd->filter_list); -if (rd->json_output) { - rd->jw =jsonw_new (stdout); -if (!rd->jw) { -pr_err ("Failed to create JSON writer\n" ); -return -ENOMEM ; - } -jsonw_pretty (rd->jw, rd->pretty_output); - } - rd->buff =malloc (MNL_SOCKET_BUFFER_SIZE );if (!rd->buff)return -ENOMEM ;
@@ -109,8 +101,6 @@ static int rd_init(struct rd *rd, char *filename)static void rd_cleanup (struct rd *rd) { -if (rd->json_output) -jsonw_destroy (&rd->jw);rd_free (rd); }
@@ -128,7 +118,6 @@ int main(int argc, char **argv) };bool show_driver_details = false;const char *batch_file =NULL ; -bool pretty_output = false;bool show_details = false;bool json_output = false;bool force = false;
@@ -136,7 +125,6 @@ int main(int argc, char **argv)char *filename;int opt;int err; - filename =basename (argv[0 ]); while ((opt = getopt_long(argc, argv, ":Vhdpjfb:",
@@ -147,7 +135,7 @@ int main(int argc, char **argv) filename, SNAPSHOT);return EXIT_SUCCESS ;case 'p' : - pretty_output = true; + pretty =1 ;break ;case 'd' :if (show_details)
@@ -156,7 +144,7 @@ int main(int argc, char **argv) show_details = true;break ;case 'j' : - json_output = true; + json_output =1 ;break ;case 'f' : force = true;
@@ -183,7 +171,7 @@ int main(int argc, char **argv) rd.show_details = show_details; rd.show_driver_details = show_driver_details; rd.json_output = json_output; - rd.pretty_output = pretty_output; + rd.pretty_output = pretty; err =rd_init (&rd, filename);if (err)
rdma/rdma.h +3 -3
@@ -19,7 +19,7 @@#include "list.h" #include "utils.h" -#include "json_writer.h" +#include "json_print.h" #define pr_err(args...) fprintf(stderr, ##args) #define pr_out(args...) fprintf(stdout, ##args)
@@ -66,8 +66,8 @@ struct rd {struct nlmsghdr *nlh;char *buff;json_writer_t *jw; -bool json_output; -bool pretty_output; +int json_output; +int pretty_output;bool suppress_errors;struct list_head filter_list;char *link_name;
rdma/res-cmid.c +13 -25
@@ -9,10 +9,8 @@static void print_qp_type (struct rd *rd,uint32_t val) { -if (rd->json_output) -jsonw_string_field (rd->jw,"qp-type" ,qp_types_to_str (val)); -else -pr_out ("qp-type %s " ,qp_types_to_str (val)); +print_color_string (PRINT_ANY ,COLOR_NONE ,"qp-type" ,"qp-type %s " , +qp_types_to_str (val)); }static const char *cm_id_state_to_str (uint8_t idx)
@@ -47,34 +45,26 @@ static const char *cm_id_ps_to_str(uint32_t ps)static void print_cm_id_state (struct rd *rd,uint8_t state) { -if (rd->json_output) { -jsonw_string_field (rd->jw,"state" ,cm_id_state_to_str (state)); -return ; - } -pr_out ("state %s " ,cm_id_state_to_str (state)); +print_color_string (PRINT_ANY ,COLOR_NONE ,"state" ,"state %s " , +cm_id_state_to_str (state)); }static void print_ps (struct rd *rd,uint32_t ps) { -if (rd->json_output) { -jsonw_string_field (rd->jw,"ps" ,cm_id_ps_to_str (ps)); -return ; - } -pr_out ("ps %s " ,cm_id_ps_to_str (ps)); +print_color_string (PRINT_ANY ,COLOR_NONE ,"ps" ,"ps %s " , +cm_id_ps_to_str (ps)); }static void print_ipaddr (struct rd *rd,const char *key,char *addrstr,uint16_t port) { -if (rd->json_output) { -int name_size =INET6_ADDRSTRLEN +strlen (":65535" ); -char json_name[name_size]; +int name_size =INET6_ADDRSTRLEN +strlen (":65535" ); +char json_name[name_size]; -snprintf (json_name, name_size,"%s:%u" , addrstr, port); -jsonw_string_field (rd->jw, key, json_name); -return ; - } -pr_out ("%s %s:%u " , key, addrstr, port); +snprintf (json_name, name_size,"%s:%u" , addrstr, port); +print_color_string (PRINT_ANY ,COLOR_NONE , key, key, json_name); +print_color_string (PRINT_FP ,COLOR_NONE ,NULL ," %s:" , addrstr); +print_color_uint (PRINT_FP ,COLOR_NONE ,NULL ,"%u " , port); }static int ss_ntop (struct nlattr *nla_line,char *addr_str,uint16_t *port)
@@ -195,9 +185,7 @@ static int res_cm_id_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]); } -if (rd->json_output) -jsonw_start_array (rd->jw); - +open_json_object (NULL );print_link (rd, idx, name, port, nla_line);res_print_uint (rd,"cm-idn" , cm_idn, nla_line[RDMA_NLDEV_ATTR_RES_CM_IDN ]);
rdma/res-cq.c +3 -10
@@ -21,13 +21,8 @@ static void print_poll_ctx(struct rd *rd, uint8_t poll_ctx, struct nlattr *attr) {if (!attr)return ; - -if (rd->json_output) { -jsonw_string_field (rd->jw,"poll-ctx" , -poll_ctx_to_str (poll_ctx)); -return ; - } -pr_out ("poll-ctx %s " ,poll_ctx_to_str (poll_ctx)); +print_color_string (PRINT_ANY ,COLOR_NONE ,"poll-ctx" ,"poll-ctx %s " , +poll_ctx_to_str (poll_ctx)); }static void print_cq_dim_setting (struct rd *rd,struct nlattr *attr)
@@ -99,9 +94,7 @@ static int res_cq_line(struct rd *rd, const char *name, int idx, comm = (char *)mnl_attr_get_str ( nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); -if (rd->json_output) -jsonw_start_array (rd->jw); - +open_json_object (NULL );print_dev (rd, idx, name);res_print_uint (rd,"cqn" , cqn, nla_line[RDMA_NLDEV_ATTR_RES_CQN ]);res_print_uint (rd,"cqe" , cqe, nla_line[RDMA_NLDEV_ATTR_RES_CQE ]);
rdma/res-mr.c +1 -4
@@ -57,10 +57,7 @@ static int res_mr_line(struct rd *rd, const char *name, int idx,/* discard const from mnl_attr_get_str */ comm = (char *)mnl_attr_get_str ( nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); - -if (rd->json_output) -jsonw_start_array (rd->jw); - +open_json_object (NULL );print_dev (rd, idx, name);res_print_uint (rd,"mrn" , mrn, nla_line[RDMA_NLDEV_ATTR_RES_MRN ]);print_key (rd,"rkey" , rkey, nla_line[RDMA_NLDEV_ATTR_RES_RKEY ]);
rdma/res-pd.c +1 -3
@@ -60,9 +60,7 @@ static int res_pd_line(struct rd *rd, const char *name, int idx, comm = (char *)mnl_attr_get_str ( nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); -if (rd->json_output) -jsonw_start_array (rd->jw); - +open_json_object (NULL );print_dev (rd, idx, name);res_print_uint (rd,"pdn" , pdn, nla_line[RDMA_NLDEV_ATTR_RES_PDN ]); print_key(rd, "local_dma_lkey", local_dma_lkey,
rdma/res-qp.c +9 -26
@@ -32,27 +32,19 @@ static void print_rqpn(struct rd *rd, uint32_t val, struct nlattr **nla_line) {if (!nla_line[RDMA_NLDEV_ATTR_RES_RQPN ])return ; - -if (rd->json_output) -jsonw_uint_field (rd->jw,"rqpn" , val); -else -pr_out ("rqpn %u " , val); +print_color_uint (PRINT_ANY ,COLOR_NONE ,"rqpn" ,"rqpn %d " , val); }static void print_type (struct rd *rd,uint32_t val) { -if (rd->json_output) -jsonw_string_field (rd->jw,"type" ,qp_types_to_str (val)); -else -pr_out ("type %s " ,qp_types_to_str (val)); +print_color_string (PRINT_ANY ,COLOR_NONE ,"type" ,"type %s " , +qp_types_to_str (val)); }static void print_state (struct rd *rd,uint32_t val) { -if (rd->json_output) -jsonw_string_field (rd->jw,"state" ,qp_states_to_str (val)); -else -pr_out ("state %s " ,qp_states_to_str (val)); +print_color_string (PRINT_ANY ,COLOR_NONE ,"state" ,"state %s " , +qp_states_to_str (val)); }static void print_rqpsn (struct rd *rd,uint32_t val,struct nlattr **nla_line)
@@ -60,10 +52,7 @@ static void print_rqpsn(struct rd *rd, uint32_t val, struct nlattr **nla_line)if (!nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN ])return ; -if (rd->json_output) -jsonw_uint_field (rd->jw,"rq-psn" , val); -else -pr_out ("rq-psn %u " , val); +print_color_uint (PRINT_ANY ,COLOR_NONE ,"rq-psn" ,"rq-psn %d " , val); }static void print_pathmig (struct rd *rd,uint32_t val,struct nlattr **nla_line)
@@ -71,11 +60,8 @@ static void print_pathmig(struct rd *rd, uint32_t val, struct nlattr **nla_line)if (!nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE ])return ; -if (rd->json_output) -jsonw_string_field (rd->jw,"path-mig-state" , -path_mig_to_str (val)); -else -pr_out ("path-mig-state %s " ,path_mig_to_str (val)); +print_color_string (PRINT_ANY ,COLOR_NONE ,"path-mig-state" , +"path-mig-state %s " ,path_mig_to_str (val)); } static int res_qp_line(struct rd *rd, const char *name, int idx,
@@ -159,11 +145,8 @@ static int res_qp_line(struct rd *rd, const char *name, int idx, comm = (char *)mnl_attr_get_str ( nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME ]); -if (rd->json_output) -jsonw_start_array (rd->jw); - +open_json_object (NULL );print_link (rd, idx, name, port, nla_line); -res_print_uint (rd,"lqpn" , lqpn, nla_line[RDMA_NLDEV_ATTR_RES_LQPN ]);print_rqpn (rd, rqpn, nla_line);
rdma/res.c +15 -56
123 lines not on this page · raw message
rdma/stat-mr.c +1 -3
@@ -20,9 +20,7 @@ static int stat_mr_line(struct rd *rd, const char *name, int idx, nla_line[RDMA_NLDEV_ATTR_RES_MRN])) goto out; -if (rd->json_output) -jsonw_start_array (rd->jw); - +open_json_object (NULL );print_dev (rd, idx, name);res_print_uint (rd,"mrn" , mrn, nla_line[RDMA_NLDEV_ATTR_RES_MRN ]);
rdma/stat.c +19 -57
124 lines not on this page · raw message
rdma/sys.c +2 -5
@@ -20,7 +20,6 @@ static const char *netns_modes_str[] = {static int sys_show_parse_cb (const struct nlmsghdr *nlh,void *data) {struct nlattr *tb[RDMA_NLDEV_ATTR_MAX ] = {}; -struct rd *rd = data;mnl_attr_parse (nlh,0 , rd_attr_cb, tb);
@@ -36,10 +35,8 @@ static int sys_show_parse_cb(const struct nlmsghdr *nlh, void *data)else mode_str ="unknown" ; -if (rd->json_output) -jsonw_string_field (rd->jw,"netns" , mode_str); -else -pr_out ("netns %s\n" , mode_str); +print_color_string (PRINT_ANY ,COLOR_NONE ,"netns" ,"netns %s\n" , + mode_str); }return MNL_CB_OK ; }
rdma/utils.c +54 -64
183 lines not on this page · raw message
Signature · 1 line
2.20.1
David Ahern
AppliedQuoted 14 lines · Leon Romanovsky
From: Ido Kalir <[email protected]>
Instead of doing open-coded solution to generate JSON and prints, let's reuse existing infrastructure and APIs to do the same as ip/*.
Before this change:
if (rd->json_output)jsonw_uint_field(rd->jw, "sm_lid", sm_lid); else pr_out("sm_lid %u ", sm_lid);After this change:
print_uint(PRINT_ANY, "sm_lid", "sm_lid %u ", sm_lid);
Quoted 7 lines
All the print functions are converted to support color but for now the type of color is
COLOR_NONE. This is done as a preparation to addition of color enable option. Such change will require rewrite of command line arguments parser which is out-of-scope for this patch.Signed-off-by Ido Kalir <[email protected]>Signed-off-by Leon Romanovsky <[email protected]>
Applied to iproute2-next. Thank you for doing this!
Now we just devlink to be converted.