From: Eli Britstein Date: Thu, 14 Nov 2019 12:44:37 +0000 (+0200) Subject: tc_util: introduce a function to print JSON/non-JSON masked numbers X-Git-Tag: v5.4.0~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04b215015ba8dd214d8dcabc8b958036845f5f5d;p=thirdparty%2Fiproute2.git tc_util: introduce a function to print JSON/non-JSON masked numbers 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 Reviewed-by: Roi Dayan Acked-by: Jiri Pirko Signed-off-by: Stephen Hemminger --- diff --git a/tc/tc_util.c b/tc/tc_util.c index 0eb530408..6bd098fef 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 0c3425abc..7e5d93cba 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