All of lore.kernel.org
 help / color / mirror / Atom feed
From: Roi Dayan <roid@mellanox.com>
To: netdev@vger.kernel.org
Cc: David Ahern <dsahern@gmail.com>,
	Stephen Hemminger <stephen@networkplumber.org>,
	Jiri Pirko <jiri@mellanox.com>,
	Eli Britstein <elibr@mellanox.com>, Roi Dayan <roid@mellanox.com>
Subject: [PATCH iproute2 v2 1/5] tc_util: introduce a function to print JSON/non-JSON masked numbers
Date: Thu, 14 Nov 2019 14:44:37 +0200	[thread overview]
Message-ID: <20191114124441.2261-2-roid@mellanox.com> (raw)
In-Reply-To: <20191114124441.2261-1-roid@mellanox.com>

From: Eli Britstein <elibr@mellanox.com>

Introduce a function to print masked number with a different output for
JSON or non-JSON methods, as a pre-step towards printing numbers using
this common function.

Signed-off-by: Eli Britstein <elibr@mellanox.com>
Reviewed-by: Roi Dayan <roid@mellanox.com>
Acked-by: Jiri Pirko <jiri@mellanox.com>
---

Notes:
    v2:
    - use _SL_ instead of \n for newline.
    - return earlier if attr is not defined.

 tc/tc_util.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tc/tc_util.h |  2 ++
 2 files changed, 53 insertions(+)

diff --git a/tc/tc_util.c b/tc/tc_util.c
index 0eb530408d05..6bd098feff14 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -915,6 +915,45 @@ compat_xstats:
 		*xstats = tb[TCA_XSTATS];
 }
 
+static void print_masked_type(__u32 type_max,
+			      __u32 (*rta_getattr_type)(const struct rtattr *),
+			      const char *name, struct rtattr *attr,
+			      struct rtattr *mask_attr)
+{
+	SPRINT_BUF(namefrm);
+	__u32 value, mask;
+	SPRINT_BUF(out);
+	size_t done;
+
+	if (!attr)
+		return;
+
+	value = rta_getattr_type(attr);
+	mask = mask_attr ? rta_getattr_type(mask_attr) : type_max;
+
+	if (is_json_context()) {
+		sprintf(namefrm, "\n  %s %%u", name);
+		print_hu(PRINT_ANY, name, namefrm,
+			 rta_getattr_type(attr));
+		if (mask != type_max) {
+			char mask_name[SPRINT_BSIZE-6];
+
+			sprintf(mask_name, "%s_mask", name);
+			print_string(PRINT_FP, NULL, "%s  ", _SL_);
+			sprintf(namefrm, "%s %%u", mask_name);
+			print_hu(PRINT_ANY, mask_name, namefrm, mask);
+		}
+	} else {
+		done = sprintf(out, "%u", value);
+		if (mask != type_max)
+			sprintf(out + done, "/0x%x", mask);
+
+		print_string(PRINT_FP, NULL, "%s  ", _SL_);
+		sprintf(namefrm, "%s %%s", name);
+		print_string(PRINT_ANY, name, namefrm, out);
+	}
+}
+
 void print_masked_u32(const char *name, struct rtattr *attr,
 		      struct rtattr *mask_attr)
 {
@@ -958,3 +997,15 @@ void print_masked_u16(const char *name, struct rtattr *attr,
 	sprintf(namefrm, " %s %%s", name);
 	print_string(PRINT_ANY, name, namefrm, out);
 }
+
+static __u32 __rta_getattr_u8_u32(const struct rtattr *attr)
+{
+	return rta_getattr_u8(attr);
+}
+
+void print_masked_u8(const char *name, struct rtattr *attr,
+		     struct rtattr *mask_attr)
+{
+	print_masked_type(UINT8_MAX,  __rta_getattr_u8_u32, name, attr,
+			  mask_attr);
+}
diff --git a/tc/tc_util.h b/tc/tc_util.h
index 0c3425abc62f..7e5d93cbac66 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -131,4 +131,6 @@ void print_masked_u32(const char *name, struct rtattr *attr,
 		      struct rtattr *mask_attr);
 void print_masked_u16(const char *name, struct rtattr *attr,
 		      struct rtattr *mask_attr);
+void print_masked_u8(const char *name, struct rtattr *attr,
+		     struct rtattr *mask_attr);
 #endif
-- 
2.8.4


  reply	other threads:[~2019-11-14 12:44 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-14 12:44 [PATCH iproute2 v2 0/5] fix json output for some keys Roi Dayan
2019-11-14 12:44 ` Roi Dayan [this message]
2019-11-14 23:13   ` [PATCH iproute2 v2 1/5] tc_util: introduce a function to print JSON/non-JSON masked numbers Stephen Hemminger
2019-11-17  9:10     ` Roi Dayan
2019-11-14 12:44 ` [PATCH iproute2 v2 2/5] tc_util: add an option to print masked numbers with/without a newline Roi Dayan
2019-11-14 12:44 ` [PATCH iproute2 v2 3/5] tc: flower: fix newline prints for ct-mark and ct-zone Roi Dayan
2019-11-14 12:44 ` [PATCH iproute2 v2 4/5] tc_util: fix JSON " Roi Dayan
2019-11-14 12:44 ` [PATCH iproute2 v2 5/5] tc: flower: fix output for ip tos and ttl Roi Dayan
2019-11-19 19:40 ` [PATCH iproute2 v2 0/5] fix json output for some keys Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191114124441.2261-2-roid@mellanox.com \
    --to=roid@mellanox.com \
    --cc=dsahern@gmail.com \
    --cc=elibr@mellanox.com \
    --cc=jiri@mellanox.com \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.