diff mbox series

[iproute2,1/5] tc_util: introduce a function to print JSON/non-JSON masked numbers

Message ID 20191113101245.182025-2-roid@mellanox.com
State Changes Requested
Delegated to: stephen hemminger
Headers show
Series fix json output for tos and ttl | expand

Commit Message

Roi Dayan Nov. 13, 2019, 10:12 a.m. UTC
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>
---
 tc/tc_util.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 tc/tc_util.h |  2 ++
 2 files changed, 50 insertions(+)

Comments

Stephen Hemminger Nov. 13, 2019, 5:18 p.m. UTC | #1
On Wed, 13 Nov 2019 12:12:41 +0200
Roi Dayan <roid@mellanox.com> wrote:

> +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) {

code is cleaner if you use short circuit return here. i.e

	if (!attr)
		return;

...
Stephen Hemminger Nov. 13, 2019, 5:20 p.m. UTC | #2
On Wed, 13 Nov 2019 12:12:41 +0200
Roi Dayan <roid@mellanox.com> wrote:

> +
> +		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);
> +				sprintf(namefrm, "\n  %s %%u", mask_name);
> +				print_hu(PRINT_ANY, mask_name, namefrm, mask);

Should use _SL_ to handle single line output format case (instead of \n)
Roi Dayan Nov. 14, 2019, 8:24 a.m. UTC | #3
On 2019-11-13 7:18 PM, Stephen Hemminger wrote:
> On Wed, 13 Nov 2019 12:12:41 +0200
> Roi Dayan <roid@mellanox.com> wrote:
> 
>> +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) {
> 
> code is cleaner if you use short circuit return here. i.e
> 
> 	if (!attr)
> 		return;
> 
> ...
> 

right. thanks for the comments. we'll fix it.
Roi Dayan Nov. 14, 2019, 9:38 a.m. UTC | #4
On 2019-11-13 7:20 PM, Stephen Hemminger wrote:
> On Wed, 13 Nov 2019 12:12:41 +0200
> Roi Dayan <roid@mellanox.com> wrote:
> 
>> +
>> +		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);
>> +				sprintf(namefrm, "\n  %s %%u", mask_name);
>> +				print_hu(PRINT_ANY, mask_name, namefrm, mask);
> 
> Should use _SL_ to handle single line output format case (instead of \n)
> 

now i see there are more places using \n instead of _SL_ which breaks
oneline output. not related to this change so i'll prepare fixes for
those later.
thanks.
diff mbox series

Patch

diff --git a/tc/tc_util.c b/tc/tc_util.c
index 0eb530408d05..2b391f182b96 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -915,6 +915,42 @@  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) {
+		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);
+				sprintf(namefrm, "\n  %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);
+
+			sprintf(namefrm, "\n  %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 +994,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