diff mbox series

[RFC,iproute2,6/8] rdma: do not mix newline and json object

Message ID 20240103003558.20615-7-stephen@networkplumber.org (mailing list archive)
State Superseded
Delegated to: David Ahern
Headers show
Series rdma: print related patches | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Stephen Hemminger Jan. 3, 2024, 12:34 a.m. UTC
Mixing the semantics of ending lines with the json object
leads to several bugs where json object is closed twice, etc.
Replace by breaking the meaning of newline() function into
two parts.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 rdma/dev.c      |  3 ++-
 rdma/link.c     |  4 +++-
 rdma/rdma.h     |  5 +++--
 rdma/res-cmid.c |  3 ++-
 rdma/res-cq.c   |  7 +++++--
 rdma/res-ctx.c  |  3 ++-
 rdma/res-mr.c   |  6 ++++--
 rdma/res-pd.c   |  4 ++--
 rdma/res-qp.c   |  6 ++++--
 rdma/res-srq.c  |  6 ++++--
 rdma/res.c      |  4 +++-
 rdma/stat-mr.c  |  3 ++-
 rdma/stat.c     | 17 ++++++++++-------
 rdma/utils.c    | 16 +++++-----------
 14 files changed, 51 insertions(+), 36 deletions(-)

Comments

Chengchang Tang Jan. 3, 2024, 3:40 a.m. UTC | #1
On 2024/1/3 8:34, Stephen Hemminger wrote:
> Mixing the semantics of ending lines with the json object
> leads to several bugs where json object is closed twice, etc.
> Replace by breaking the meaning of newline() function into
> two parts.

These codes fix the issue mentioned in:
https://lore.kernel.org/netdev/20231229065241.554726-1-huangjunxian6@hisilicon.com/T/#m5476970400a2d6f2b9b8f3f369d8a26b9663d4b9

Might it need a fixes ?

Fixes: 331152752a97 ("rdma: print driver resource attributes")
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>   rdma/dev.c      |  3 ++-
>   rdma/link.c     |  4 +++-
>   rdma/rdma.h     |  5 +++--
>   rdma/res-cmid.c |  3 ++-
>   rdma/res-cq.c   |  7 +++++--
>   rdma/res-ctx.c  |  3 ++-
>   rdma/res-mr.c   |  6 ++++--
>   rdma/res-pd.c   |  4 ++--
>   rdma/res-qp.c   |  6 ++++--
>   rdma/res-srq.c  |  6 ++++--
>   rdma/res.c      |  4 +++-
>   rdma/stat-mr.c  |  3 ++-
>   rdma/stat.c     | 17 ++++++++++-------
>   rdma/utils.c    | 16 +++++-----------
>   14 files changed, 51 insertions(+), 36 deletions(-)
>
> diff --git a/rdma/dev.c b/rdma/dev.c
> index 31868c6fe43e..c8cb6d675c3b 100644
>
< ... >
> index aeb627be7715..64f598c5aa8f 100644
> --- a/rdma/utils.c
> +++ b/rdma/utils.c
> @@ -771,16 +771,10 @@ struct dev_map *dev_map_lookup(struct rd *rd, bool allow_port_index)
>   
>   #define nla_type(attr) ((attr)->nla_type & NLA_TYPE_MASK)
>   
> -void newline(struct rd *rd)
> +void print_nl_indent(void)
>   {
> -	close_json_object();
> -	print_nl();
> -}
> -
> -void newline_indent(struct rd *rd)
> -{
> -	newline(rd);
> -	print_string(PRINT_FP, NULL, "    ", NULL);
> +	if (!is_json_context())
> +		printf("%s    ", _SL_);
>   }
>   
>   static int print_driver_string(struct rd *rd, const char *key_str,
> @@ -920,7 +914,7 @@ void print_driver_table(struct rd *rd, struct nlattr *tb)
>   	if (!rd->show_driver_details || !tb)
>   		return;
>   
> -	newline_indent(rd);
> +	print_nl_indent();
>   
>   	/*
>   	 * Driver attrs are tuples of {key, [print-type], value}.
> @@ -932,7 +926,7 @@ void print_driver_table(struct rd *rd, struct nlattr *tb)
>   	mnl_attr_for_each_nested(tb_entry, tb) {
>   
>   		if (cc > MAX_LINE_LENGTH) {
> -			newline_indent(rd);
> +			print_nl_indent();
>   			cc = 0;
>   		}
>   		if (rd_attr_check(tb_entry, &type) != MNL_CB_OK)
diff mbox series

Patch

diff --git a/rdma/dev.c b/rdma/dev.c
index 31868c6fe43e..c8cb6d675c3b 100644
--- a/rdma/dev.c
+++ b/rdma/dev.c
@@ -224,7 +224,8 @@  static int dev_parse_cb(const struct nlmsghdr *nlh, void *data)
 		dev_print_caps(rd, tb);
 	}
 
-	newline(rd);
+	close_json_object();
+	print_nl();
 	return MNL_CB_OK;
 }
 
diff --git a/rdma/link.c b/rdma/link.c
index d7d9558b49f2..0c57a01480ba 100644
--- a/rdma/link.c
+++ b/rdma/link.c
@@ -252,7 +252,9 @@  static int link_parse_cb(const struct nlmsghdr *nlh, void *data)
 	if (rd->show_details)
 		link_print_caps(rd, tb);
 
-	newline(rd);
+	close_json_object();
+	print_nl();
+
 	return MNL_CB_OK;
 }
 
diff --git a/rdma/rdma.h b/rdma/rdma.h
index 65e3557d4036..e93e34cbce45 100644
--- a/rdma/rdma.h
+++ b/rdma/rdma.h
@@ -135,9 +135,10 @@  int rd_attr_cb(const struct nlattr *attr, void *data);
  */
 void print_driver_table(struct rd *rd, struct nlattr *tb);
 void print_raw_data(struct rd *rd, struct nlattr **nla_line);
-void newline(struct rd *rd);
-void newline_indent(struct rd *rd);
 void print_raw_data(struct rd *rd, struct nlattr **nla_line);
+void print_nl_indent(void);
+
 #define MAX_LINE_LENGTH 80
 
+
 #endif /* _RDMA_TOOL_H_ */
diff --git a/rdma/res-cmid.c b/rdma/res-cmid.c
index fb32c58a695a..9714404aaded 100644
--- a/rdma/res-cmid.c
+++ b/rdma/res-cmid.c
@@ -198,7 +198,8 @@  static int res_cm_id_line(struct rd *rd, const char *name, int idx,
 		print_ipaddr(rd, "dst-addr", dst_addr_str, dst_port);
 
 	print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
-	newline(rd);
+	close_json_object();
+	print_nl();
 
 out:
 	return MNL_CB_OK;
diff --git a/rdma/res-cq.c b/rdma/res-cq.c
index b4dcc026ed4b..0d705a552781 100644
--- a/rdma/res-cq.c
+++ b/rdma/res-cq.c
@@ -47,7 +47,8 @@  static int res_cq_line_raw(struct rd *rd, const char *name, int idx,
 	open_json_object(NULL);
 	print_dev(rd, idx, name);
 	print_raw_data(rd, nla_line);
-	newline(rd);
+	close_json_object();
+	print_nl();
 
 	return MNL_CB_OK;
 }
@@ -122,7 +123,9 @@  static int res_cq_line(struct rd *rd, const char *name, int idx,
 	print_comm(rd, comm, nla_line);
 
 	print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
-	newline(rd);
+	close_json_object();
+	print_nl();
+
 
 out:
 	return MNL_CB_OK;
diff --git a/rdma/res-ctx.c b/rdma/res-ctx.c
index 500186d9ff59..1a5d31e817ff 100644
--- a/rdma/res-ctx.c
+++ b/rdma/res-ctx.c
@@ -47,7 +47,8 @@  static int res_ctx_line(struct rd *rd, const char *name, int idx,
 	print_comm(rd, comm, nla_line);
 
 	print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
-	newline(rd);
+	close_json_object();
+	print_nl();
 
 out:
 	return MNL_CB_OK;
diff --git a/rdma/res-mr.c b/rdma/res-mr.c
index fb48d5df6cad..8b647efbc6a9 100644
--- a/rdma/res-mr.c
+++ b/rdma/res-mr.c
@@ -16,7 +16,8 @@  static int res_mr_line_raw(struct rd *rd, const char *name, int idx,
 	open_json_object(NULL);
 	print_dev(rd, idx, name);
 	print_raw_data(rd, nla_line);
-	newline(rd);
+	close_json_object();
+	print_nl();
 
 	return MNL_CB_OK;
 }
@@ -87,7 +88,8 @@  static int res_mr_line(struct rd *rd, const char *name, int idx,
 
 	print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
 	print_raw_data(rd, nla_line);
-	newline(rd);
+	close_json_object();
+	print_nl();
 
 out:
 	return MNL_CB_OK;
diff --git a/rdma/res-pd.c b/rdma/res-pd.c
index 66f91f42860f..3ccfa364a236 100644
--- a/rdma/res-pd.c
+++ b/rdma/res-pd.c
@@ -76,8 +76,8 @@  static int res_pd_line(struct rd *rd, const char *name, int idx,
 	print_comm(rd, comm, nla_line);
 
 	print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
-	newline(rd);
-
+	close_json_object();
+	print_nl();
 out:
 	return MNL_CB_OK;
 }
diff --git a/rdma/res-qp.c b/rdma/res-qp.c
index 2390c0b5732b..d5d6a836e358 100644
--- a/rdma/res-qp.c
+++ b/rdma/res-qp.c
@@ -70,7 +70,8 @@  static int res_qp_line_raw(struct rd *rd, const char *name, int idx,
 	open_json_object(NULL);
 	print_link(rd, idx, name, rd->port_idx, nla_line);
 	print_raw_data(rd, nla_line);
-	newline(rd);
+	close_json_object();
+	print_nl();
 
 	return MNL_CB_OK;
 }
@@ -176,7 +177,8 @@  static int res_qp_line(struct rd *rd, const char *name, int idx,
 	print_comm(rd, comm, nla_line);
 
 	print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
-	newline(rd);
+	close_json_object();
+	print_nl();
 out:
 	return MNL_CB_OK;
 }
diff --git a/rdma/res-srq.c b/rdma/res-srq.c
index e702fecd1f34..b9d1fc45a1a4 100644
--- a/rdma/res-srq.c
+++ b/rdma/res-srq.c
@@ -170,7 +170,8 @@  static int res_srq_line_raw(struct rd *rd, const char *name, int idx,
 	open_json_object(NULL);
 	print_dev(rd, idx, name);
 	print_raw_data(rd, nla_line);
-	newline(rd);
+	close_json_object();
+	print_nl();
 
 	return MNL_CB_OK;
 }
@@ -241,7 +242,8 @@  static int res_srq_line(struct rd *rd, const char *name, int idx,
 	print_comm(rd, comm, nla_line);
 
 	print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
-	newline(rd);
+	close_json_object();
+	print_nl();
 
 out:
 	return MNL_CB_OK;
diff --git a/rdma/res.c b/rdma/res.c
index f64224e1f3eb..2c4507da8223 100644
--- a/rdma/res.c
+++ b/rdma/res.c
@@ -81,7 +81,9 @@  static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data)
 	print_uint(PRINT_ANY, "ifindex", "%u: ", idx);
 	print_string(PRINT_ANY, "ifname", "%s: ", name);
 	res_print_summary(rd, tb);
-	newline(rd);
+	close_json_object();
+	print_nl();
+
 	return MNL_CB_OK;
 }
 
diff --git a/rdma/stat-mr.c b/rdma/stat-mr.c
index 2ba6cb07693e..be41f0db3d93 100644
--- a/rdma/stat-mr.c
+++ b/rdma/stat-mr.c
@@ -31,7 +31,8 @@  static int stat_mr_line(struct rd *rd, const char *name, int idx,
 			return ret;
 	}
 
-	newline(rd);
+	close_json_object();
+	print_nl();
 out:
 	return MNL_CB_OK;
 }
diff --git a/rdma/stat.c b/rdma/stat.c
index e90b6197ceb7..865f301370a0 100644
--- a/rdma/stat.c
+++ b/rdma/stat.c
@@ -136,7 +136,8 @@  static int qp_link_get_mode_parse_cb(const struct nlmsghdr *nlh, void *data)
 	open_json_object(NULL);
 	print_link(rd, idx, name, port, tb);
 	print_string(PRINT_ANY, "mode", "mode %s ", output);
-	newline(rd);
+	close_json_object();
+	print_nl();
 	return MNL_CB_OK;
 }
 
@@ -208,7 +209,7 @@  int res_get_hwcounters(struct rd *rd, struct nlattr *hwc_table, bool print)
 
 		nm = mnl_attr_get_str(hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME]);
 		v = mnl_attr_get_u64(hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE]);
-		newline_indent(rd);
+		print_nl_indent();
 		res_print_u64(rd, nm, v, hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME]);
 	}
 
@@ -308,7 +309,8 @@  static int res_counter_line(struct rd *rd, const char *name, int index,
 		isfirst = false;
 	}
 	close_json_array(PRINT_ANY, ">");
-	newline(rd);
+	close_json_object();
+	print_nl();
 	return MNL_CB_OK;
 }
 
@@ -757,7 +759,6 @@  static int do_stat_mode_parse_cb(const struct nlmsghdr *nlh, void *data,
 	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
 	struct nlattr *nla_entry;
 	const char *dev, *name;
-	struct rd *rd = data;
 	int enabled, err = 0;
 	bool isfirst = true;
 	uint32_t port;
@@ -801,7 +802,7 @@  static int do_stat_mode_parse_cb(const struct nlmsghdr *nlh, void *data,
 			} else {
 				print_string(PRINT_FP, NULL, ",", NULL);
 			}
-			newline_indent(rd);
+			print_nl_indent();
 
 			print_string(PRINT_ANY, NULL, "%s", name);
 		}
@@ -809,7 +810,8 @@  static int do_stat_mode_parse_cb(const struct nlmsghdr *nlh, void *data,
 
 	if (!isfirst) {
 		close_json_array(PRINT_JSON, NULL);
-		newline(rd);
+		close_json_object();
+		print_nl();
 	}
 
 	return 0;
@@ -1070,7 +1072,8 @@  static int stat_show_parse_cb(const struct nlmsghdr *nlh, void *data)
 	print_uint(PRINT_ANY, "port", "%u ", port);
 	ret = res_get_hwcounters(rd, tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS], true);
 
-	newline(rd);
+	close_json_object();
+	print_nl();
 	return ret;
 }
 
diff --git a/rdma/utils.c b/rdma/utils.c
index aeb627be7715..64f598c5aa8f 100644
--- a/rdma/utils.c
+++ b/rdma/utils.c
@@ -771,16 +771,10 @@  struct dev_map *dev_map_lookup(struct rd *rd, bool allow_port_index)
 
 #define nla_type(attr) ((attr)->nla_type & NLA_TYPE_MASK)
 
-void newline(struct rd *rd)
+void print_nl_indent(void)
 {
-	close_json_object();
-	print_nl();
-}
-
-void newline_indent(struct rd *rd)
-{
-	newline(rd);
-	print_string(PRINT_FP, NULL, "    ", NULL);
+	if (!is_json_context())
+		printf("%s    ", _SL_);
 }
 
 static int print_driver_string(struct rd *rd, const char *key_str,
@@ -920,7 +914,7 @@  void print_driver_table(struct rd *rd, struct nlattr *tb)
 	if (!rd->show_driver_details || !tb)
 		return;
 
-	newline_indent(rd);
+	print_nl_indent();
 
 	/*
 	 * Driver attrs are tuples of {key, [print-type], value}.
@@ -932,7 +926,7 @@  void print_driver_table(struct rd *rd, struct nlattr *tb)
 	mnl_attr_for_each_nested(tb_entry, tb) {
 
 		if (cc > MAX_LINE_LENGTH) {
-			newline_indent(rd);
+			print_nl_indent();
 			cc = 0;
 		}
 		if (rd_attr_check(tb_entry, &type) != MNL_CB_OK)