From 1d6b73be708d8e8e8d5438ee2c169d3992b07190 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Sun, 29 Dec 2019 10:58:13 -0800 Subject: [PATCH 01/18] tc: prio: fix space in JSON tag The priomap should not have extra space in the tag. Signed-off-by: Stephen Hemminger --- tc/q_prio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tc/q_prio.c b/tc/q_prio.c index 8ef7cfa2..a723a151 100644 --- a/tc/q_prio.c +++ b/tc/q_prio.c @@ -108,7 +108,7 @@ int prio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) return -1; print_uint(PRINT_ANY, "bands", "bands %u ", qopt->bands); - open_json_array(PRINT_ANY, "priomap "); + open_json_array(PRINT_ANY, "priomap"); for (i = 0; i <= TC_PRIO_MAX; i++) print_uint(PRINT_ANY, NULL, " %d", qopt->priomap[i]); close_json_array(PRINT_ANY, ""); From f8bebea915e39e734e82bcf5e16d233dc877c1cf Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Sun, 29 Dec 2019 10:07:18 -0800 Subject: [PATCH 02/18] tc: skbprio: add support for JSON output Print limit in JSON Signed-off-by: Stephen Hemminger --- tc/q_skbprio.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tc/q_skbprio.c b/tc/q_skbprio.c index 2e65a589..ca81a72c 100644 --- a/tc/q_skbprio.c +++ b/tc/q_skbprio.c @@ -73,7 +73,8 @@ static int skbprio_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) if (RTA_PAYLOAD(opt) < sizeof(*qopt)) return -1; qopt = RTA_DATA(opt); - fprintf(f, "limit %u ", qopt->limit); + + print_uint(PRINT_ANY, "limit", "limit %u ", qopt->limit); return 0; } From d5391e186f04214315a5a80797c78e50ad9f5271 Mon Sep 17 00:00:00 2001 From: Tuong Lien Date: Mon, 30 Dec 2019 10:03:33 +0700 Subject: [PATCH 03/18] tipc: fix clang warning in tipc/node.c When building tipc with clang, the following warning is found: tipc CC bearer.o CC cmdl.o CC link.o CC media.o CC misc.o CC msg.o CC nametable.o CC node.o node.c:182:24: warning: field 'key' with variable sized type 'struct tipc_aead_key' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end] struct tipc_aead_key key; This commit fixes it by putting the memory area allocated for the user input key along with the variable-sized 'key' structure in the 'union' form instead. Fixes: 24bee3bf9752 ("tipc: add new commands to set TIPC AEAD key") Reported-by: Stephen Hemminger Signed-off-by: Tuong Lien Signed-off-by: Stephen Hemminger --- tipc/node.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tipc/node.c b/tipc/node.c index 6c796bfb..ffdaeaea 100644 --- a/tipc/node.c +++ b/tipc/node.c @@ -179,8 +179,10 @@ static int cmd_node_set_key(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl, void *data) { struct { - struct tipc_aead_key key; - char mem[TIPC_AEAD_KEYLEN_MAX + 1]; + union { + struct tipc_aead_key key; + char mem[TIPC_AEAD_KEY_SIZE_MAX]; + }; } input = {}; struct opt opts[] = { { "algname", OPT_KEYVAL, NULL }, From 970db267a0fb5036b28b2d9d4f53cb4be267ffd5 Mon Sep 17 00:00:00 2001 From: Vladis Dronov Date: Sun, 19 Jan 2020 02:12:51 +0100 Subject: [PATCH 04/18] ip: fix link type and vlan oneline output Move link type printing in print_linkinfo() so multiline output does not break link options line. Add oneline support for vlan's ingress and egress qos maps. Before the fix: 5: veth90.4000@veth90: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 26:9a:05:af:db:00 brd ff:ff:ff:ff:ff:ff promiscuity 0 minmtu 0 maxmtu 65535 vlan protocol 802.1Q id 4000 the option line is broken ^^^ ingress-qos-map { 1:2 } egress-qos-map { 2:1 } addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 5: veth90.4000@veth90: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000\ link/ether 26:9a:05:af:db:00 brd ff:ff:ff:ff:ff:ff promiscuity 0 minmtu 0 maxmtu 65535 \ vlan protocol 802.1Q id 4000 ingress-qos-map { 1:2 } <<< a multiline output despite -oneline egress-qos-map { 2:1 } addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 After the fix: 5: veth90.4000@veth90: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 26:9a:05:af:db:00 brd ff:ff:ff:ff:ff:ff promiscuity 0 minmtu 0 maxmtu 65535 addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 vlan protocol 802.1Q id 4000 ingress-qos-map { 1:2 } egress-qos-map { 2:1 } 5: veth90.4000@veth90: mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000\ link/ether 26:9a:05:af:db:00 brd ff:ff:ff:ff:ff:ff promiscuity 0 minmtu 0 maxmtu 65535 addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 \ vlan protocol 802.1Q id 4000 \ ingress-qos-map { 1:2 } \ egress-qos-map { 2:1 } Fixes: 5c302d518f10 ("vlan support") Link: https://bugzilla.kernel.org/show_bug.cgi?id=206241 Reported-by: George Shuklin Signed-off-by: Vladis Dronov Signed-off-by: Stephen Hemminger --- ip/iplink_vlan.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ip/iplink_vlan.c b/ip/iplink_vlan.c index 0dfb4a8d..1e6817f5 100644 --- a/ip/iplink_vlan.c +++ b/ip/iplink_vlan.c @@ -183,7 +183,8 @@ static void vlan_print_map(FILE *f, int rem; open_json_array(PRINT_JSON, name_json); - print_string(PRINT_FP, NULL, "\n %s { ", name_fp); + print_nl(); + print_string(PRINT_FP, NULL, " %s { ", name_fp); rem = RTA_PAYLOAD(attr); for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { From f4d7ce9bfae9710fcb984f72d1119d8bf4ff2f8c Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Mon, 20 Jan 2020 09:29:05 -0800 Subject: [PATCH 05/18] ip: use print_nl() to handle one line mode The helper function print_nl() does the right thing and prints the newline or backslash. Signed-off-by: Stephen Hemminger --- ip/ipaddress.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ip/ipaddress.c b/ip/ipaddress.c index 964f14df..b7e91324 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -1633,7 +1633,8 @@ static int show_handler(struct rtnl_ctrl_data *ctrl, struct ifaddrmsg *ifa = NLMSG_DATA(n); open_json_object(NULL); - print_int(PRINT_ANY, "index", "if%d:\n", ifa->ifa_index); + print_int(PRINT_ANY, "index", "if%d:", ifa->ifa_index); + print_nl(); print_addrinfo(n, stdout); close_json_object(); return 0; From 31f45088c9c891542e38144d0a1bba124934693c Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Wed, 8 Jan 2020 11:04:24 +0100 Subject: [PATCH 06/18] build: fix build failure with -fno-common $ make CCOPTS=-fno-common gcc ... -o ip ld: rt_names.o (symbol from plugin): in function "rtnl_rtprot_n2a": (.text+0x0): multiple definition of "numeric"; ip.o (symbol from plugin):(.text+0x0): first defined here gcc ... -o tipc ld: ../lib/libutil.a(utils.o):(.bss+0xc): multiple definition of `pretty'; tipc.o:tipc.c:28: first defined here References: https://bugzilla.opensuse.org/1160244 Signed-off-by: Jan Engelhardt Signed-off-by: Stephen Hemminger --- include/rt_names.h | 2 ++ ip/ip.c | 2 +- misc/ss.c | 2 +- tc/tc.c | 2 +- tipc/tipc.c | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/rt_names.h b/include/rt_names.h index 62ebbd6a..7afce170 100644 --- a/include/rt_names.h +++ b/include/rt_names.h @@ -33,4 +33,6 @@ int ll_proto_a2n(unsigned short *id, const char *buf); const char *nl_proto_n2a(int id, char *buf, int len); int nl_proto_a2n(__u32 *id, const char *arg); +extern int numeric; + #endif diff --git a/ip/ip.c b/ip/ip.c index fed26f8d..90392c2a 100644 --- a/ip/ip.c +++ b/ip/ip.c @@ -23,6 +23,7 @@ #include "ip_common.h" #include "namespace.h" #include "color.h" +#include "rt_names.h" int preferred_family = AF_UNSPEC; int human_readable; @@ -36,7 +37,6 @@ int timestamp; int force; int max_flush_loops = 10; int batch_mode; -int numeric; bool do_all; struct rtnl_handle rth = { .fd = -1 }; diff --git a/misc/ss.c b/misc/ss.c index 95f1d37a..1e8bca5a 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -35,6 +35,7 @@ #include "libnetlink.h" #include "namespace.h" #include "SNAPSHOT.h" +#include "rt_names.h" #include #include @@ -121,7 +122,6 @@ static int follow_events; static int sctp_ino; static int show_tipcinfo; static int show_tos; -int numeric; int oneline; enum col_id { diff --git a/tc/tc.c b/tc/tc.c index 37294b31..b72657ec 100644 --- a/tc/tc.c +++ b/tc/tc.c @@ -29,6 +29,7 @@ #include "tc_util.h" #include "tc_common.h" #include "namespace.h" +#include "rt_names.h" int show_stats; int show_details; @@ -43,7 +44,6 @@ bool use_names; int json; int color; int oneline; -int numeric; static char *conf_file; diff --git a/tipc/tipc.c b/tipc/tipc.c index f85ddee0..60176a04 100644 --- a/tipc/tipc.c +++ b/tipc/tipc.c @@ -22,10 +22,10 @@ #include "node.h" #include "peer.h" #include "cmdl.h" +#include "utils.h" int help_flag; int json; -int pretty; static void about(struct cmdl *cmdl) { From 5f78bc3e1da9a371582f2609e9a674ee294d7bcf Mon Sep 17 00:00:00 2001 From: Ethan Sommer Date: Wed, 8 Jan 2020 14:57:05 -0500 Subject: [PATCH 07/18] make yacc usage POSIX compatible config: put YACC in config.mk and use environmental variable if present ss: use YACC variable instead of hardcoding bison place options before source file argument use -b to specify file prefix instead of output file, as -o isn't POSIX compatible, this generates ssfilter.tab.c instead of ssfilter.c replace any references to ssfilter.c with references to ssfilter.tab.c tc: use -p flag to set name prefix instead of bison-specific api.prefix directive remove unneeded bison-specific directives use -b instead of -o, replace references to previously generated emp_ematch.yacc.[ch] with references to newly generated emp_ematch.tab.[ch] Signed-off-by: Ethan Sommer Signed-off-by: Stephen Hemminger --- configure | 2 ++ misc/.gitignore | 2 +- misc/Makefile | 6 +++--- tc/.gitignore | 4 ++-- tc/Makefile | 13 ++++++------- tc/emp_ematch.l | 2 +- tc/emp_ematch.y | 5 ----- 7 files changed, 15 insertions(+), 19 deletions(-) diff --git a/configure b/configure index 45fcffb6..f415bf49 100755 --- a/configure +++ b/configure @@ -16,9 +16,11 @@ check_toolchain() : ${PKG_CONFIG:=pkg-config} : ${AR=ar} : ${CC=gcc} + : ${YACC=bison} echo "PKG_CONFIG:=${PKG_CONFIG}" >>$CONFIG echo "AR:=${AR}" >>$CONFIG echo "CC:=${CC}" >>$CONFIG + echo "YACC:=${YACC}" >>$CONFIG } check_atm() diff --git a/misc/.gitignore b/misc/.gitignore index f73f7f21..d7df0b02 100644 --- a/misc/.gitignore +++ b/misc/.gitignore @@ -1,7 +1,7 @@ arpd ifstat ss -ssfilter.c +ssfilter.tab.c nstat lnstat rtacct diff --git a/misc/Makefile b/misc/Makefile index 6a849af4..1debfb15 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -SSOBJ=ss.o ssfilter.o +SSOBJ=ss.o ssfilter.tab.o LNSTATOBJ=lnstat.o lnstat_util.o TARGETS=ss nstat ifstat rtacct lnstat @@ -27,8 +27,8 @@ rtacct: rtacct.c arpd: arpd.c $(QUIET_CC)$(CC) $(CFLAGS) -I$(DBM_INCLUDE) $(CPPFLAGS) $(LDFLAGS) -o arpd arpd.c $(LDLIBS) -ldb -ssfilter.c: ssfilter.y - $(QUIET_YACC)bison ssfilter.y -o ssfilter.c +ssfilter.tab.c: ssfilter.y + $(QUIET_YACC)$(YACC) -b ssfilter ssfilter.y lnstat: $(LNSTATOBJ) $(QUIET_LINK)$(CC) $^ $(LDFLAGS) $(LDLIBS) -o $@ diff --git a/tc/.gitignore b/tc/.gitignore index e8e86c97..0dbe9195 100644 --- a/tc/.gitignore +++ b/tc/.gitignore @@ -1,5 +1,5 @@ -*.yacc.c +*.tab.c *.lex.c *.output -*.yacc.h +*.tab.h tc diff --git a/tc/Makefile b/tc/Makefile index 14171a28..a378c403 100644 --- a/tc/Makefile +++ b/tc/Makefile @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 TCOBJ= tc.o tc_qdisc.o tc_class.o tc_filter.o tc_util.o tc_monitor.o \ tc_exec.o m_police.o m_estimator.o m_action.o m_ematch.o \ - emp_ematch.yacc.o emp_ematch.lex.o + emp_ematch.tab.o emp_ematch.lex.o include ../config.mk @@ -125,7 +125,6 @@ ifneq ($(IPT_LIB_DIR),) CFLAGS += -DIPT_LIB_DIR=\"$(IPT_LIB_DIR)\" endif -YACC := bison LEX := flex CFLAGS += -DYY_NO_INPUT @@ -158,8 +157,8 @@ install: all fi clean: - rm -f $(TCOBJ) $(TCLIB) libtc.a tc *.so emp_ematch.yacc.h; \ - rm -f emp_ematch.yacc.* + rm -f $(TCOBJ) $(TCLIB) libtc.a tc *.so emp_ematch.tab.h; \ + rm -f emp_ematch.tab.* q_atm.so: q_atm.c $(QUIET_CC)$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -shared -fpic -o q_atm.so q_atm.c -latm @@ -178,8 +177,8 @@ ifeq ($(TC_CONFIG_XT),y) LDLIBS += $$($(PKG_CONFIG) xtables --libs) endif -%.yacc.c: %.y - $(QUIET_YACC)$(YACC) $(YACCFLAGS) -o $@ $< +%.tab.c: %.y + $(QUIET_YACC)$(YACC) $(YACCFLAGS) -p ematch_ -b $(basename $(basename $@)) $< %.lex.c: %.l $(QUIET_LEX)$(LEX) $(LEXFLAGS) -o$@ $< @@ -187,7 +186,7 @@ endif # our lexer includes the header from yacc, so make sure # we don't attempt to compile it before the header has # been generated as part of the yacc step. -emp_ematch.lex.o: emp_ematch.yacc.c +emp_ematch.lex.o: emp_ematch.tab.c ifneq ($(SHARED_LIBS),y) diff --git a/tc/emp_ematch.l b/tc/emp_ematch.l index d7a99304..2f4926d4 100644 --- a/tc/emp_ematch.l +++ b/tc/emp_ematch.l @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0 */ %{ - #include "emp_ematch.yacc.h" + #include "emp_ematch.tab.h" #include "m_ematch.h" extern int ematch_argc; diff --git a/tc/emp_ematch.y b/tc/emp_ematch.y index a02e831a..4da3daed 100644 --- a/tc/emp_ematch.y +++ b/tc/emp_ematch.y @@ -6,11 +6,6 @@ #include "m_ematch.h" %} -%locations -%token-table -%define parse.error verbose -%define api.prefix {ematch_} - %union { unsigned int i; struct bstr *b; From 919046d326bacd3d5fd76801cc71294fe2cfcbc7 Mon Sep 17 00:00:00 2001 From: Roi Dayan Date: Tue, 21 Jan 2020 10:53:30 +0200 Subject: [PATCH 08/18] tc: flower: fix print with oneline option This commit fix all location in flower to use _SL_ instead of \n for newline to allow support for oneline option. Example before this commit: filter protocol ip pref 2 flower chain 0 handle 0x1 indev ens1f0 dst_mac 11:22:33:44:55:66 eth_type ipv4 ip_proto tcp src_ip 2.2.2.2 src_port 99 dst_port 1-10\ tcp_flags 0x5/5 ip_flags frag ct_state -trk\ ct_zone 4\ ct_mark 255 ct_label 00000000000000000000000000000000 skip_hw not_in_hw\ action order 1: ct zone 5 pipe index 1 ref 1 bind 1 installed 287 sec used 287 sec Action statistics:\ Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0\ Example output after this commit: filter protocol ip pref 2 flower chain 0 handle 0x1 \ indev ens1f0\ dst_mac 11:22:33:44:55:66\ eth_type ipv4\ ip_proto tcp\ src_ip 2.2.2.2\ src_port 99\ dst_port 1-10\ tcp_flags 0x5/5\ ip_flags frag\ ct_state -trk\ ct_zone 4\ ct_mark 255\ ct_label 00000000000000000000000000000000\ skip_hw\ not_in_hw\action order 1: ct zone 5 pipe index 1 ref 1 bind 1 installed 346 sec used 346 sec Action statistics:\ Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0\ Signed-off-by: Roi Dayan Acked-by: Jiri Pirko Signed-off-by: Stephen Hemminger --- tc/f_flower.c | 82 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/tc/f_flower.c b/tc/f_flower.c index a193c0ec..8f248db5 100644 --- a/tc/f_flower.c +++ b/tc/f_flower.c @@ -1599,7 +1599,8 @@ static void flower_print_eth_addr(char *name, struct rtattr *addr_attr, sprintf(out + done, "/%d", bits); } - sprintf(namefrm, "\n %s %%s", name); + print_nl(); + sprintf(namefrm, " %s %%s", name); print_string(PRINT_ANY, name, namefrm, out); } @@ -1624,7 +1625,8 @@ static void flower_print_eth_type(__be16 *p_eth_type, else sprintf(out, "%04x", ntohs(eth_type)); - print_string(PRINT_ANY, "eth_type", "\n eth_type %s", out); + print_nl(); + print_string(PRINT_ANY, "eth_type", " eth_type %s", out); *p_eth_type = eth_type; } @@ -1651,7 +1653,8 @@ static void flower_print_ip_proto(__u8 *p_ip_proto, else sprintf(out, "%02x", ip_proto); - print_string(PRINT_ANY, "ip_proto", "\n ip_proto %s", out); + print_nl(); + print_string(PRINT_ANY, "ip_proto", " ip_proto %s", out); *p_ip_proto = ip_proto; } @@ -1682,7 +1685,8 @@ static void flower_print_matching_flags(char *name, continue; if (mtf_mask & flags_str[i].flag) { if (++count == 1) { - print_string(PRINT_FP, NULL, "\n %s ", name); + print_nl(); + print_string(PRINT_FP, NULL, " %s ", name); open_json_object(name); } else { print_string(PRINT_FP, NULL, "/", NULL); @@ -1741,7 +1745,8 @@ static void flower_print_ip_addr(char *name, __be16 eth_type, else if (bits < len * 8) sprintf(out + done, "/%d", bits); - sprintf(namefrm, "\n %s %%s", name); + print_nl(); + sprintf(namefrm, " %s %%s", name); print_string(PRINT_ANY, name, namefrm, out); } static void flower_print_ip4_addr(char *name, struct rtattr *addr_attr, @@ -1775,7 +1780,8 @@ static void flower_print_port_range(char *name, struct rtattr *min_attr, done = sprintf(out, "%u", rta_getattr_be16(min_attr)); sprintf(out + done, "-%u", rta_getattr_be16(max_attr)); - sprintf(namefrm, "\n %s %%s", name); + print_nl(); + sprintf(namefrm, " %s %%s", name); print_string(PRINT_ANY, name, namefrm, out); } } @@ -1794,8 +1800,8 @@ static void flower_print_tcp_flags(const char *name, struct rtattr *flags_attr, if (mask_attr) sprintf(out + done, "/%x", rta_getattr_be16(mask_attr)); - print_string(PRINT_FP, NULL, "%s ", _SL_); - sprintf(namefrm, "%s %%s", name); + print_nl(); + sprintf(namefrm, " %s %%s", name); print_string(PRINT_ANY, name, namefrm, out); } @@ -1829,7 +1835,8 @@ static void flower_print_ct_state(struct rtattr *flags_attr, flower_ct_states[i].str); } - print_string(PRINT_ANY, "ct_state", "\n ct_state %s", out); + print_nl(); + print_string(PRINT_ANY, "ct_state", " ct_state %s", out); } static void flower_print_ct_label(struct rtattr *attr, @@ -1864,7 +1871,8 @@ static void flower_print_ct_label(struct rtattr *attr, } *p = '\0'; - print_string(PRINT_ANY, "ct_label", "\n ct_label %s", out); + print_nl(); + print_string(PRINT_ANY, "ct_label", " ct_label %s", out); } static void flower_print_ct_zone(struct rtattr *attr, @@ -1886,7 +1894,8 @@ static void flower_print_key_id(const char *name, struct rtattr *attr) if (!attr) return; - sprintf(namefrm,"\n %s %%u", name); + print_nl(); + sprintf(namefrm, " %s %%u", name); print_uint(PRINT_ANY, name, namefrm, rta_getattr_be32(attr)); } @@ -1934,7 +1943,7 @@ static void flower_print_geneve_opts(const char *name, struct rtattr *attr, static void flower_print_geneve_parts(const char *name, struct rtattr *attr, char *key, char *mask) { - char *namefrm = "\n geneve_opt %s"; + char *namefrm = " geneve_opt %s"; char *key_token, *mask_token, *out; int len; @@ -1952,6 +1961,7 @@ static void flower_print_geneve_parts(const char *name, struct rtattr *attr, } out[len - 1] = '\0'; + print_nl(); print_string(PRINT_FP, name, namefrm, out); free(out); } @@ -2015,7 +2025,8 @@ static void flower_print_masked_u8(const char *name, struct rtattr *attr, if (mask != UINT8_MAX) sprintf(out + done, "/%d", mask); - sprintf(namefrm,"\n %s %%s", name); + print_nl(); + sprintf(namefrm, " %s %%s", name); print_string(PRINT_ANY, name, namefrm, out); } @@ -2031,7 +2042,8 @@ static void flower_print_u32(const char *name, struct rtattr *attr) if (!attr) return; - sprintf(namefrm,"\n %s %%u", name); + print_nl(); + sprintf(namefrm, " %s %%u", name); print_uint(PRINT_ANY, name, namefrm, rta_getattr_u32(attr)); } @@ -2077,7 +2089,8 @@ static int flower_print_opt(struct filter_util *qu, FILE *f, if (tb[TCA_FLOWER_INDEV]) { struct rtattr *attr = tb[TCA_FLOWER_INDEV]; - print_string(PRINT_ANY, "indev", "\n indev %s", + print_nl(); + print_string(PRINT_ANY, "indev", " indev %s", rta_getattr_str(attr)); } @@ -2086,14 +2099,16 @@ static int flower_print_opt(struct filter_util *qu, FILE *f, if (tb[TCA_FLOWER_KEY_VLAN_ID]) { struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_ID]; - print_uint(PRINT_ANY, "vlan_id", "\n vlan_id %u", + print_nl(); + print_uint(PRINT_ANY, "vlan_id", " vlan_id %u", rta_getattr_u16(attr)); } if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) { struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_PRIO]; - print_uint(PRINT_ANY, "vlan_prio", "\n vlan_prio %d", + print_nl(); + print_uint(PRINT_ANY, "vlan_prio", " vlan_prio %d", rta_getattr_u8(attr)); } @@ -2101,7 +2116,8 @@ static int flower_print_opt(struct filter_util *qu, FILE *f, SPRINT_BUF(buf); struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]; - print_string(PRINT_ANY, "vlan_ethtype", "\n vlan_ethtype %s", + print_nl(); + print_string(PRINT_ANY, "vlan_ethtype", " vlan_ethtype %s", ll_proto_n2a(rta_getattr_u16(attr), buf, sizeof(buf))); } @@ -2109,14 +2125,16 @@ static int flower_print_opt(struct filter_util *qu, FILE *f, if (tb[TCA_FLOWER_KEY_CVLAN_ID]) { struct rtattr *attr = tb[TCA_FLOWER_KEY_CVLAN_ID]; - print_uint(PRINT_ANY, "cvlan_id", "\n cvlan_id %u", + print_nl(); + print_uint(PRINT_ANY, "cvlan_id", " cvlan_id %u", rta_getattr_u16(attr)); } if (tb[TCA_FLOWER_KEY_CVLAN_PRIO]) { struct rtattr *attr = tb[TCA_FLOWER_KEY_CVLAN_PRIO]; - print_uint(PRINT_ANY, "cvlan_prio", "\n cvlan_prio %d", + print_nl(); + print_uint(PRINT_ANY, "cvlan_prio", " cvlan_prio %d", rta_getattr_u8(attr)); } @@ -2124,7 +2142,8 @@ static int flower_print_opt(struct filter_util *qu, FILE *f, SPRINT_BUF(buf); struct rtattr *attr = tb[TCA_FLOWER_KEY_CVLAN_ETH_TYPE]; - print_string(PRINT_ANY, "cvlan_ethtype", "\n cvlan_ethtype %s", + print_nl(); + print_string(PRINT_ANY, "cvlan_ethtype", " cvlan_ethtype %s", ll_proto_n2a(rta_getattr_u16(attr), buf, sizeof(buf))); } @@ -2254,13 +2273,17 @@ static int flower_print_opt(struct filter_util *qu, FILE *f, if (tb[TCA_FLOWER_FLAGS]) { __u32 flags = rta_getattr_u32(tb[TCA_FLOWER_FLAGS]); - if (flags & TCA_CLS_FLAGS_SKIP_HW) - print_bool(PRINT_ANY, "skip_hw", "\n skip_hw", true); - if (flags & TCA_CLS_FLAGS_SKIP_SW) - print_bool(PRINT_ANY, "skip_sw", "\n skip_sw", true); - + if (flags & TCA_CLS_FLAGS_SKIP_HW) { + print_nl(); + print_bool(PRINT_ANY, "skip_hw", " skip_hw", true); + } + if (flags & TCA_CLS_FLAGS_SKIP_SW) { + print_nl(); + print_bool(PRINT_ANY, "skip_sw", " skip_sw", true); + } if (flags & TCA_CLS_FLAGS_IN_HW) { - print_bool(PRINT_ANY, "in_hw", "\n in_hw", true); + print_nl(); + print_bool(PRINT_ANY, "in_hw", " in_hw", true); if (tb[TCA_FLOWER_IN_HW_COUNT]) { __u32 count = rta_getattr_u32(tb[TCA_FLOWER_IN_HW_COUNT]); @@ -2268,9 +2291,10 @@ static int flower_print_opt(struct filter_util *qu, FILE *f, print_uint(PRINT_ANY, "in_hw_count", " in_hw_count %u", count); } + } else if (flags & TCA_CLS_FLAGS_NOT_IN_HW) { + print_nl(); + print_bool(PRINT_ANY, "not_in_hw", " not_in_hw", true); } - else if (flags & TCA_CLS_FLAGS_NOT_IN_HW) - print_bool(PRINT_ANY, "not_in_hw", "\n not_in_hw", true); } if (tb[TCA_FLOWER_ACT]) From 31ca29b2bec4020929686eb240fa1abd963819a4 Mon Sep 17 00:00:00 2001 From: Ron Diskin Date: Thu, 23 Jan 2020 12:32:26 +0200 Subject: [PATCH 09/18] json_print: Introduce print_#type_name_value Until now print_#type functions supported printing constant names and unknown (variable) values only. Add functions to allow printing when the name is also sent to the function as a variable. Signed-off-by: Ron Diskin Reviewed-by: Moshe Shemesh Reviewed-by: Jiri Pirko Signed-off-by: Stephen Hemminger --- include/json_print.h | 7 +++++++ lib/json_print.c | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/json_print.h b/include/json_print.h index 6695654f..fcc28d5e 100644 --- a/include/json_print.h +++ b/include/json_print.h @@ -72,4 +72,11 @@ _PRINT_FUNC(lluint, unsigned long long) _PRINT_FUNC(float, double) #undef _PRINT_FUNC +#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \ + void print_##type_name##_name_value(const char *name, type value) \ + +_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u); +_PRINT_NAME_VALUE_FUNC(string, const char*, s); +#undef _PRINT_NAME_VALUE_FUNC + #endif /* _JSON_PRINT_H_ */ diff --git a/lib/json_print.c b/lib/json_print.c index 43ea69bb..fb5f0e5d 100644 --- a/lib/json_print.c +++ b/lib/json_print.c @@ -127,6 +127,19 @@ _PRINT_FUNC(lluint, unsigned long long); _PRINT_FUNC(float, double); #undef _PRINT_FUNC +#define _PRINT_NAME_VALUE_FUNC(type_name, type, format_char) \ + void print_##type_name##_name_value(const char *name, type value)\ + { \ + SPRINT_BUF(format); \ + \ + snprintf(format, SPRINT_BSIZE, \ + "%s %%"#format_char, name); \ + print_##type_name(PRINT_ANY, name, format, value); \ + } +_PRINT_NAME_VALUE_FUNC(uint, unsigned int, u); +_PRINT_NAME_VALUE_FUNC(string, const char*, s); +#undef _PRINT_NAME_VALUE_FUNC + void print_color_string(enum output_type type, enum color_attr color, const char *key, From 98e48e7dd0e5d64752bcdbf07e30baae9b22f636 Mon Sep 17 00:00:00 2001 From: Ron Diskin Date: Thu, 23 Jan 2020 12:32:27 +0200 Subject: [PATCH 10/18] json_print: Add new json object function not as array item Currently new json object opens (and delete_json_obj closes) the object as an array, what adds prints for the matching bracket '[' ']' at the start/end of the object. This patch adds new_json_obj_plain() and the matching delete_json_obj_plain() to enable opening and closing json object, not as array and leave it to the using function to decide which type of object to open/close as the main object. Signed-off-by: Ron Diskin Reviewed-by: Moshe Shemesh Reviewed-by: Jiri Pirko Signed-off-by: Stephen Hemminger --- include/json_print.h | 2 ++ lib/json_print.c | 34 ++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/json_print.h b/include/json_print.h index fcc28d5e..34444793 100644 --- a/include/json_print.h +++ b/include/json_print.h @@ -31,6 +31,8 @@ enum output_type { void new_json_obj(int json); void delete_json_obj(void); +void new_json_obj_plain(int json); +void delete_json_obj_plain(void); bool is_json_context(void); diff --git a/lib/json_print.c b/lib/json_print.c index fb5f0e5d..8e7f32dc 100644 --- a/lib/json_print.c +++ b/lib/json_print.c @@ -20,7 +20,7 @@ static json_writer_t *_jw; #define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw) #define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY)) -void new_json_obj(int json) +static void __new_json_obj(int json, bool have_array) { if (json) { _jw = jsonw_new(stdout); @@ -30,16 +30,38 @@ void new_json_obj(int json) } if (pretty) jsonw_pretty(_jw, true); - jsonw_start_array(_jw); + if (have_array) + jsonw_start_array(_jw); } } +static void __delete_json_obj(bool have_array) +{ + if (_jw) { + if (have_array) + jsonw_end_array(_jw); + jsonw_destroy(&_jw); + } +} + +void new_json_obj(int json) +{ + __new_json_obj(json, true); +} + void delete_json_obj(void) { - if (_jw) { - jsonw_end_array(_jw); - jsonw_destroy(&_jw); - } + __delete_json_obj(true); +} + +void new_json_obj_plain(int json) +{ + __new_json_obj(json, false); +} + +void delete_json_obj_plain(void) +{ + __delete_json_obj(false); } bool is_json_context(void) From b20555e4023b84fed41bfb3251706041806acb1a Mon Sep 17 00:00:00 2001 From: Ron Diskin Date: Thu, 23 Jan 2020 12:32:28 +0200 Subject: [PATCH 11/18] devlink: Replace json prints by common library functions Substitute json prints to use json_print.c common library functions, instead of directly calling jsonw_functions. Signed-off-by: Ron Diskin Reviewed-by: Moshe Shemesh Acked-by: Jiri Pirko Signed-off-by: Stephen Hemminger --- devlink/devlink.c | 147 +++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 86 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 95f05a0b..a641c4dc 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -1715,19 +1715,17 @@ static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb, if (dl->json_output) { if (array) { if (should_arr_last_handle_end(dl, bus_name, dev_name)) - jsonw_end_array(dl->jw); + close_json_array(PRINT_JSON, NULL); if (should_arr_last_handle_start(dl, bus_name, dev_name)) { - jsonw_name(dl->jw, buf); - jsonw_start_array(dl->jw); - jsonw_start_object(dl->jw); + open_json_array(PRINT_JSON, buf); + open_json_object(NULL); arr_last_handle_set(dl, bus_name, dev_name); } else { - jsonw_start_object(dl->jw); + open_json_object(NULL); } } else { - jsonw_name(dl->jw, buf); - jsonw_start_object(dl->jw); + open_json_object(buf); } } else { if (array) { @@ -1754,7 +1752,7 @@ static void pr_out_handle_start_arr(struct dl *dl, struct nlattr **tb) static void pr_out_handle_end(struct dl *dl) { if (dl->json_output) - jsonw_end_object(dl->jw); + close_json_object(); else __pr_out_newline(); } @@ -1816,21 +1814,19 @@ static void __pr_out_port_handle_start(struct dl *dl, const char *bus_name, if (should_arr_last_port_handle_end(dl, bus_name, dev_name, port_index)) - jsonw_end_array(dl->jw); + close_json_array(PRINT_JSON, NULL); if (should_arr_last_port_handle_start(dl, bus_name, dev_name, port_index)) { - jsonw_name(dl->jw, buf); - jsonw_start_array(dl->jw); - jsonw_start_object(dl->jw); + open_json_array(PRINT_JSON, buf); + open_json_object(NULL); arr_last_port_handle_set(dl, bus_name, dev_name, port_index); } else { - jsonw_start_object(dl->jw); + open_json_object(NULL); } } else { - jsonw_name(dl->jw, buf); - jsonw_start_object(dl->jw); + open_json_object(buf); } } else { pr_out("%s:", buf); @@ -1864,7 +1860,7 @@ static void pr_out_port_handle_start_arr(struct dl *dl, struct nlattr **tb, bool static void pr_out_port_handle_end(struct dl *dl) { if (dl->json_output) - jsonw_end_object(dl->jw); + close_json_object(); else pr_out("\n"); } @@ -1874,7 +1870,7 @@ static void pr_out_str(struct dl *dl, const char *name, const char *val) { __pr_out_indent_newline(dl); if (dl->json_output) - jsonw_string_field(dl->jw, name, val); + print_string(PRINT_JSON, name, NULL, val); else pr_out("%s %s", name, val); } @@ -1882,7 +1878,7 @@ static void pr_out_str(struct dl *dl, const char *name, const char *val) static void pr_out_bool(struct dl *dl, const char *name, bool val) { if (dl->json_output) - jsonw_bool_field(dl->jw, name, val); + print_bool(PRINT_JSON, name, NULL, val); else pr_out_str(dl, name, val ? "true" : "false"); } @@ -1891,7 +1887,7 @@ static void pr_out_uint(struct dl *dl, const char *name, unsigned int val) { __pr_out_indent_newline(dl); if (dl->json_output) - jsonw_uint_field(dl->jw, name, val); + print_uint(PRINT_JSON, name, NULL, val); else pr_out("%s %u", name, val); } @@ -1903,7 +1899,7 @@ static void pr_out_u64(struct dl *dl, const char *name, uint64_t val) return pr_out_str(dl, name, "unlimited"); if (dl->json_output) - jsonw_u64_field(dl->jw, name, val); + print_u64(PRINT_JSON, name, NULL, val); else pr_out("%s %"PRIu64, name, val); } @@ -1912,7 +1908,7 @@ static void pr_out_bool_value(struct dl *dl, bool value) { __pr_out_indent_newline(dl); if (dl->json_output) - jsonw_bool(dl->jw, value); + print_bool(PRINT_JSON, NULL, NULL, value); else pr_out("%s", value ? "true" : "false"); } @@ -1921,7 +1917,7 @@ static void pr_out_uint_value(struct dl *dl, unsigned int value) { __pr_out_indent_newline(dl); if (dl->json_output) - jsonw_uint(dl->jw, value); + print_uint(PRINT_JSON, NULL, NULL, value); else pr_out("%u", value); } @@ -1930,7 +1926,7 @@ static void pr_out_uint64_value(struct dl *dl, uint64_t value) { __pr_out_indent_newline(dl); if (dl->json_output) - jsonw_u64(dl->jw, value); + print_u64(PRINT_JSON, NULL, NULL, value); else pr_out("%"PRIu64, value); } @@ -1946,7 +1942,7 @@ static void pr_out_binary_value(struct dl *dl, uint8_t *data, uint32_t len) while (i < len) { if (dl->json_output) - jsonw_printf(dl->jw, "%d", data[i]); + print_int(PRINT_JSON, NULL, NULL, data[i]); else pr_out("%02x ", data[i]); i++; @@ -1961,7 +1957,7 @@ static void pr_out_str_value(struct dl *dl, const char *value) { __pr_out_indent_newline(dl); if (dl->json_output) - jsonw_string(dl->jw, value); + print_string(PRINT_JSON, NULL, NULL, value); else pr_out("%s", value); } @@ -1970,7 +1966,7 @@ static void pr_out_name(struct dl *dl, const char *name) { __pr_out_indent_newline(dl); if (dl->json_output) - jsonw_name(dl->jw, name); + print_string(PRINT_JSON, name, NULL, NULL); else pr_out("%s:", name); } @@ -1978,17 +1974,15 @@ static void pr_out_name(struct dl *dl, const char *name) static void pr_out_region_chunk_start(struct dl *dl, uint64_t addr) { if (dl->json_output) { - jsonw_name(dl->jw, "address"); - jsonw_uint(dl->jw, addr); - jsonw_name(dl->jw, "data"); - jsonw_start_array(dl->jw); + print_uint(PRINT_JSON, "address", NULL, addr); + open_json_array(PRINT_JSON, "data"); } } static void pr_out_region_chunk_end(struct dl *dl) { if (dl->json_output) - jsonw_end_array(dl->jw); + close_json_array(PRINT_JSON, NULL); } static void pr_out_region_chunk(struct dl *dl, uint8_t *data, uint32_t len, @@ -2008,7 +2002,7 @@ static void pr_out_region_chunk(struct dl *dl, uint8_t *data, uint32_t len, align_val++; if (dl->json_output) - jsonw_printf(dl->jw, "%d", data[i]); + print_int(PRINT_JSON, NULL, NULL, data[i]); else pr_out("%02x ", data[i]); @@ -2021,9 +2015,8 @@ static void pr_out_region_chunk(struct dl *dl, uint8_t *data, uint32_t len, static void pr_out_section_start(struct dl *dl, const char *name) { if (dl->json_output) { - jsonw_start_object(dl->jw); - jsonw_name(dl->jw, name); - jsonw_start_object(dl->jw); + open_json_object(NULL); + open_json_object(name); } } @@ -2031,17 +2024,16 @@ static void pr_out_section_end(struct dl *dl) { if (dl->json_output) { if (dl->arr_last.present) - jsonw_end_array(dl->jw); - jsonw_end_object(dl->jw); - jsonw_end_object(dl->jw); + close_json_array(PRINT_JSON, NULL); + close_json_object(); + close_json_object(); } } static void pr_out_array_start(struct dl *dl, const char *name) { if (dl->json_output) { - jsonw_name(dl->jw, name); - jsonw_start_array(dl->jw); + open_json_array(PRINT_JSON, name); } else { __pr_out_indent_inc(); __pr_out_newline(); @@ -2054,7 +2046,7 @@ static void pr_out_array_start(struct dl *dl, const char *name) static void pr_out_array_end(struct dl *dl) { if (dl->json_output) { - jsonw_end_array(dl->jw); + close_json_array(PRINT_JSON, NULL); } else { __pr_out_indent_dec(); __pr_out_indent_dec(); @@ -2064,8 +2056,7 @@ static void pr_out_array_end(struct dl *dl) static void pr_out_object_start(struct dl *dl, const char *name) { if (dl->json_output) { - jsonw_name(dl->jw, name); - jsonw_start_object(dl->jw); + open_json_object(name); } else { __pr_out_indent_inc(); __pr_out_newline(); @@ -2078,7 +2069,7 @@ static void pr_out_object_start(struct dl *dl, const char *name) static void pr_out_object_end(struct dl *dl) { if (dl->json_output) { - jsonw_end_object(dl->jw); + close_json_object(); } else { __pr_out_indent_dec(); __pr_out_indent_dec(); @@ -2088,13 +2079,13 @@ static void pr_out_object_end(struct dl *dl) static void pr_out_entry_start(struct dl *dl) { if (dl->json_output) - jsonw_start_object(dl->jw); + open_json_object(NULL); } static void pr_out_entry_end(struct dl *dl) { if (dl->json_output) - jsonw_end_object(dl->jw); + close_json_object(); else __pr_out_newline(); } @@ -3887,20 +3878,18 @@ static void pr_out_json_occ_show_item_list(struct dl *dl, const char *label, struct occ_item *occ_item; char buf[32]; - jsonw_name(dl->jw, label); - jsonw_start_object(dl->jw); + open_json_object(label); list_for_each_entry(occ_item, list, list) { sprintf(buf, "%u", occ_item->index); - jsonw_name(dl->jw, buf); - jsonw_start_object(dl->jw); + open_json_object(buf); if (bound_pool) - jsonw_uint_field(dl->jw, "bound_pool", - occ_item->bound_pool_index); - jsonw_uint_field(dl->jw, "current", occ_item->cur); - jsonw_uint_field(dl->jw, "max", occ_item->max); - jsonw_end_object(dl->jw); + print_uint(PRINT_JSON, "bound_pool", NULL, + occ_item->bound_pool_index); + print_uint(PRINT_JSON, "current", NULL, occ_item->cur); + print_uint(PRINT_JSON, "max", NULL, occ_item->max); + close_json_object(); } - jsonw_end_object(dl->jw); + close_json_object(); } static void pr_out_occ_show_port(struct dl *dl, struct occ_port *occ_port) @@ -6140,18 +6129,16 @@ static void pr_out_region_handle_start(struct dl *dl, struct nlattr **tb) char buf[256]; sprintf(buf, "%s/%s/%s", bus_name, dev_name, region_name); - if (dl->json_output) { - jsonw_name(dl->jw, buf); - jsonw_start_object(dl->jw); - } else { + if (dl->json_output) + open_json_object(buf); + else pr_out("%s:", buf); - } } static void pr_out_region_handle_end(struct dl *dl) { if (dl->json_output) - jsonw_end_object(dl->jw); + close_json_object(); else pr_out("\n"); } @@ -6159,18 +6146,16 @@ static void pr_out_region_handle_end(struct dl *dl) static void pr_out_region_snapshots_start(struct dl *dl, bool array) { __pr_out_indent_newline(dl); - if (dl->json_output) { - jsonw_name(dl->jw, "snapshot"); - jsonw_start_array(dl->jw); - } else { + if (dl->json_output) + open_json_array(PRINT_JSON, "snapshot"); + else pr_out("snapshot %s", array ? "[" : ""); - } } static void pr_out_region_snapshots_end(struct dl *dl, bool array) { if (dl->json_output) - jsonw_end_array(dl->jw); + close_json_array(PRINT_JSON, NULL); else if (array) pr_out("]"); } @@ -6185,7 +6170,7 @@ static void pr_out_region_snapshots_id(struct dl *dl, struct nlattr **tb, int in snapshot_id = mnl_attr_get_u32(tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID]); if (dl->json_output) - jsonw_uint(dl->jw, snapshot_id); + print_uint(PRINT_JSON, NULL, NULL, snapshot_id); else pr_out("%s%u", index ? " " : "", snapshot_id); } @@ -6527,7 +6512,7 @@ static void pr_out_fmsg_start_object(struct dl *dl, char **name) { if (dl->json_output) { pr_out_fmsg_name(dl, name); - jsonw_start_object(dl->jw); + open_json_object(NULL); } else { pr_out_fmsg_group_start(dl, name); } @@ -6536,7 +6521,7 @@ static void pr_out_fmsg_start_object(struct dl *dl, char **name) static void pr_out_fmsg_end_object(struct dl *dl) { if (dl->json_output) - jsonw_end_object(dl->jw); + close_json_object(); else pr_out_fmsg_group_end(dl); } @@ -6545,7 +6530,7 @@ static void pr_out_fmsg_start_array(struct dl *dl, char **name) { if (dl->json_output) { pr_out_fmsg_name(dl, name); - jsonw_start_array(dl->jw); + open_json_array(PRINT_JSON, NULL); } else { pr_out_fmsg_group_start(dl, name); } @@ -6554,7 +6539,7 @@ static void pr_out_fmsg_start_array(struct dl *dl, char **name) static void pr_out_fmsg_end_array(struct dl *dl) { if (dl->json_output) - jsonw_end_array(dl->jw); + close_json_array(PRINT_JSON, NULL); else pr_out_fmsg_group_end(dl); } @@ -7206,18 +7191,9 @@ static int dl_init(struct dl *dl) pr_err("Failed to create index map\n"); goto err_ifname_map_create; } - if (dl->json_output) { - dl->jw = jsonw_new(stdout); - if (!dl->jw) { - pr_err("Failed to create JSON writer\n"); - goto err_json_new; - } - jsonw_pretty(dl->jw, dl->pretty_output); - } + new_json_obj_plain(dl->json_output); return 0; -err_json_new: - ifname_map_fini(dl); err_ifname_map_create: mnlg_socket_close(dl->nlg); return err; @@ -7225,8 +7201,7 @@ err_ifname_map_create: static void dl_fini(struct dl *dl) { - if (dl->json_output) - jsonw_destroy(&dl->jw); + delete_json_obj_plain(); ifname_map_fini(dl); mnlg_socket_close(dl->nlg); } @@ -7333,7 +7308,7 @@ int main(int argc, char **argv) dl->json_output = true; break; case 'p': - dl->pretty_output = true; + pretty = true; break; case 'v': dl->verbose = true; From 3666eb0eb65ba0cac0121d9f6e373647cc144e1a Mon Sep 17 00:00:00 2001 From: Ron Diskin Date: Thu, 23 Jan 2020 12:32:29 +0200 Subject: [PATCH 12/18] devlink: Replace pr_out_str wrapper function with common function Replace calls for pr_out_str() and pr_out_str_value() with direct calls to common json_print library functions. Signed-off-by: Ron Diskin Reviewed-by: Moshe Shemesh Acked-by: Jiri Pirko Signed-off-by: Stephen Hemminger --- devlink/devlink.c | 292 ++++++++++++++++++++++++++-------------------- 1 file changed, 166 insertions(+), 126 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index a641c4dc..a25dd818 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -395,6 +395,17 @@ static void __pr_out_indent_newline(struct dl *dl) pr_out(" "); } +static void check_indent_newline(struct dl *dl) +{ + __pr_out_indent_newline(dl); + + if (g_indent_newline && !is_json_context()) { + printf("%s", g_indent_str); + g_indent_newline = false; + } + g_new_line_count = 0; +} + static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = { [DEVLINK_ATTR_BUS_NAME] = MNL_TYPE_NUL_STRING, [DEVLINK_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING, @@ -1865,22 +1876,12 @@ static void pr_out_port_handle_end(struct dl *dl) pr_out("\n"); } - -static void pr_out_str(struct dl *dl, const char *name, const char *val) -{ - __pr_out_indent_newline(dl); - if (dl->json_output) - print_string(PRINT_JSON, name, NULL, val); - else - pr_out("%s %s", name, val); -} - static void pr_out_bool(struct dl *dl, const char *name, bool val) { if (dl->json_output) print_bool(PRINT_JSON, name, NULL, val); else - pr_out_str(dl, name, val ? "true" : "false"); + print_string_name_value(name, val ? "true" : "false"); } static void pr_out_uint(struct dl *dl, const char *name, unsigned int val) @@ -1896,7 +1897,7 @@ static void pr_out_u64(struct dl *dl, const char *name, uint64_t val) { __pr_out_indent_newline(dl); if (val == (uint64_t) -1) - return pr_out_str(dl, name, "unlimited"); + return print_string_name_value(name, "unlimited"); if (dl->json_output) print_u64(PRINT_JSON, name, NULL, val); @@ -1953,15 +1954,6 @@ static void pr_out_binary_value(struct dl *dl, uint8_t *data, uint32_t len) __pr_out_newline(); } -static void pr_out_str_value(struct dl *dl, const char *value) -{ - __pr_out_indent_newline(dl); - if (dl->json_output) - print_string(PRINT_JSON, NULL, NULL, value); - else - pr_out("%s", value); -} - static void pr_out_name(struct dl *dl, const char *name) { __pr_out_indent_newline(dl); @@ -2156,19 +2148,24 @@ static void pr_out_eswitch(struct dl *dl, struct nlattr **tb) { __pr_out_handle_start(dl, tb, true, false); - if (tb[DEVLINK_ATTR_ESWITCH_MODE]) - pr_out_str(dl, "mode", - eswitch_mode_name(mnl_attr_get_u16(tb[DEVLINK_ATTR_ESWITCH_MODE]))); - - if (tb[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) - pr_out_str(dl, "inline-mode", - eswitch_inline_mode_name(mnl_attr_get_u8( - tb[DEVLINK_ATTR_ESWITCH_INLINE_MODE]))); - + if (tb[DEVLINK_ATTR_ESWITCH_MODE]) { + check_indent_newline(dl); + print_string(PRINT_ANY, "mode", "mode %s", + eswitch_mode_name(mnl_attr_get_u16( + tb[DEVLINK_ATTR_ESWITCH_MODE]))); + } + if (tb[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) { + check_indent_newline(dl); + print_string(PRINT_ANY, "inline-mode", "inline-mode %s", + eswitch_inline_mode_name(mnl_attr_get_u8( + tb[DEVLINK_ATTR_ESWITCH_INLINE_MODE]))); + } if (tb[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) { bool encap_mode = !!mnl_attr_get_u8(tb[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]); - pr_out_str(dl, "encap", encap_mode ? "enable" : "disable"); + check_indent_newline(dl); + print_string(PRINT_ANY, "encap", "encap %s", + encap_mode ? "enable" : "disable"); } pr_out_handle_end(dl); @@ -2355,8 +2352,10 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name, !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA])) return; - pr_out_str(dl, "cmode", - param_cmode_name(mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE]))); + check_indent_newline(dl); + print_string(PRINT_ANY, "cmode", "cmode %s", + param_cmode_name(mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE]))); + val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA]; conv_exists = param_val_conv_exists(param_val_conv, PARAM_VAL_CONV_LEN, @@ -2372,7 +2371,7 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name, &vstr); if (err) return; - pr_out_str(dl, "value", vstr); + print_string(PRINT_ANY, "value", " value %s", vstr); } else { pr_out_uint(dl, "value", mnl_attr_get_u8(val_attr)); } @@ -2386,7 +2385,7 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name, &vstr); if (err) return; - pr_out_str(dl, "value", vstr); + print_string(PRINT_ANY, "value", " value %s", vstr); } else { pr_out_uint(dl, "value", mnl_attr_get_u16(val_attr)); } @@ -2400,13 +2399,14 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name, &vstr); if (err) return; - pr_out_str(dl, "value", vstr); + print_string(PRINT_ANY, "value", " value %s", vstr); } else { pr_out_uint(dl, "value", mnl_attr_get_u32(val_attr)); } break; case MNL_TYPE_STRING: - pr_out_str(dl, "value", mnl_attr_get_str(val_attr)); + print_string(PRINT_ANY, "value", " value %s", + mnl_attr_get_str(val_attr)); break; case MNL_TYPE_FLAG: pr_out_bool(dl, "value", val_attr ? true : false); @@ -2438,12 +2438,12 @@ static void pr_out_param(struct dl *dl, struct nlattr **tb, bool array) nla_type = mnl_attr_get_u8(nla_param[DEVLINK_ATTR_PARAM_TYPE]); nla_name = mnl_attr_get_str(nla_param[DEVLINK_ATTR_PARAM_NAME]); - pr_out_str(dl, "name", nla_name); - + check_indent_newline(dl); + print_string(PRINT_ANY, "name", "name %s ", nla_name); if (!nla_param[DEVLINK_ATTR_PARAM_GENERIC]) - pr_out_str(dl, "type", "driver-specific"); + print_string(PRINT_ANY, "type", "type %s", "driver-specific"); else - pr_out_str(dl, "type", "generic"); + print_string(PRINT_ANY, "type", "type %s", "generic"); pr_out_array_start(dl, "values"); mnl_attr_for_each_nested(param_value_attr, @@ -2807,7 +2807,8 @@ static void pr_out_versions_single(struct dl *dl, const struct nlmsghdr *nlh, ver_name = mnl_attr_get_str(tb[DEVLINK_ATTR_INFO_VERSION_NAME]); ver_value = mnl_attr_get_str(tb[DEVLINK_ATTR_INFO_VERSION_VALUE]); - pr_out_str(dl, ver_name, ver_value); + check_indent_newline(dl); + print_string_name_value(ver_name, ver_value); if (!dl->json_output) __pr_out_newline(); } @@ -2827,7 +2828,9 @@ static void pr_out_info(struct dl *dl, const struct nlmsghdr *nlh, if (!dl->json_output) __pr_out_newline(); - pr_out_str(dl, "driver", mnl_attr_get_str(nla_drv)); + check_indent_newline(dl); + print_string(PRINT_ANY, "driver", "driver %s", + mnl_attr_get_str(nla_drv)); } if (tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER]) { @@ -2835,7 +2838,9 @@ static void pr_out_info(struct dl *dl, const struct nlmsghdr *nlh, if (!dl->json_output) __pr_out_newline(); - pr_out_str(dl, "serial_number", mnl_attr_get_str(nla_sn)); + check_indent_newline(dl); + print_string(PRINT_ANY, "serial_number", "serial_number %s", + mnl_attr_get_str(nla_sn)); } __pr_out_indent_dec(); @@ -3208,29 +3213,34 @@ static void pr_out_port(struct dl *dl, struct nlattr **tb) struct nlattr *dpt_attr = tb[DEVLINK_ATTR_PORT_DESIRED_TYPE]; pr_out_port_handle_start(dl, tb, false); + check_indent_newline(dl); if (pt_attr) { uint16_t port_type = mnl_attr_get_u16(pt_attr); - pr_out_str(dl, "type", port_type_name(port_type)); + print_string(PRINT_ANY, "type", "type %s", + port_type_name(port_type)); if (dpt_attr) { uint16_t des_port_type = mnl_attr_get_u16(dpt_attr); if (port_type != des_port_type) - pr_out_str(dl, "des_type", - port_type_name(des_port_type)); + print_string(PRINT_ANY, "des_type", " des_type %s", + port_type_name(des_port_type)); } } - if (tb[DEVLINK_ATTR_PORT_NETDEV_NAME]) - pr_out_str(dl, "netdev", - mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_NETDEV_NAME])); - if (tb[DEVLINK_ATTR_PORT_IBDEV_NAME]) - pr_out_str(dl, "ibdev", - mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_IBDEV_NAME])); + if (tb[DEVLINK_ATTR_PORT_NETDEV_NAME]) { + print_string(PRINT_ANY, "netdev", " netdev %s", + mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_NETDEV_NAME])); + } + if (tb[DEVLINK_ATTR_PORT_IBDEV_NAME]) { + print_string(PRINT_ANY, "ibdev", " ibdev %s", + mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_IBDEV_NAME])); + } if (tb[DEVLINK_ATTR_PORT_FLAVOUR]) { uint16_t port_flavour = mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_FLAVOUR]); - pr_out_str(dl, "flavour", port_flavour_name(port_flavour)); + print_string(PRINT_ANY, "flavour", " flavour %s", + port_flavour_name(port_flavour)); switch (port_flavour) { case DEVLINK_PORT_FLAVOUR_PCI_PF: @@ -3381,6 +3391,7 @@ static void cmd_sb_help(void) static void pr_out_sb(struct dl *dl, struct nlattr **tb) { pr_out_handle_start_arr(dl, tb); + check_indent_newline(dl); pr_out_uint(dl, "sb", mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); pr_out_uint(dl, "size", @@ -3462,12 +3473,12 @@ static void pr_out_sb_pool(struct dl *dl, struct nlattr **tb) mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); pr_out_uint(dl, "pool", mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); - pr_out_str(dl, "type", - pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]))); + print_string(PRINT_ANY, "type", " type %s", + pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]))); pr_out_uint(dl, "size", mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_SIZE])); - pr_out_str(dl, "thtype", - threshold_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]))); + print_string(PRINT_ANY, "thtype", " thtype %s", + threshold_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]))); if (tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE]) pr_out_uint(dl, "cell_size", mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE])); @@ -3652,8 +3663,8 @@ static void pr_out_sb_tc_bind(struct dl *dl, struct nlattr **tb) mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); pr_out_uint(dl, "tc", mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX])); - pr_out_str(dl, "type", - pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]))); + print_string(PRINT_ANY, "type", " type %s", + pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]))); pr_out_uint(dl, "pool", mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); pr_out_uint(dl, "threshold", @@ -4232,13 +4243,16 @@ static void pr_out_flash_update(struct dl *dl, struct nlattr **tb) { __pr_out_handle_start(dl, tb, true, false); - if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG]) - pr_out_str(dl, "msg", - mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG])); - - if (tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]) - pr_out_str(dl, "component", - mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT])); + if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG]) { + check_indent_newline(dl); + print_string(PRINT_ANY, "msg", "msg %s", + mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG])); + } + if (tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]) { + check_indent_newline(dl); + print_string(PRINT_ANY, "component", "component %s", + mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT])); + } if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE]) pr_out_u64(dl, "done", @@ -4705,13 +4719,15 @@ static void pr_out_dpipe_fields(struct dpipe_ctx *ctx, for (i = 0; i < field_count; i++) { field = &fields[i]; pr_out_entry_start(ctx->dl); - pr_out_str(ctx->dl, "name", field->name); + check_indent_newline(ctx->dl); + print_string(PRINT_ANY, "name", "name %s", field->name); if (ctx->dl->verbose) pr_out_uint(ctx->dl, "id", field->id); pr_out_uint(ctx->dl, "bitwidth", field->bitwidth); - if (field->mapping_type) - pr_out_str(ctx->dl, "mapping_type", - dpipe_field_mapping_e2s(field->mapping_type)); + if (field->mapping_type) { + print_string(PRINT_ANY, "mapping_type", " mapping_type %s", + dpipe_field_mapping_e2s(field->mapping_type)); + } pr_out_entry_end(ctx->dl); } } @@ -4721,11 +4737,11 @@ pr_out_dpipe_header(struct dpipe_ctx *ctx, struct nlattr **tb, struct dpipe_header *header, bool global) { pr_out_handle_start_arr(ctx->dl, tb); - pr_out_str(ctx->dl, "name", header->name); + check_indent_newline(ctx->dl); + print_string(PRINT_ANY, "name", "name %s", header->name); if (ctx->dl->verbose) { pr_out_uint(ctx->dl, "id", header->id); - pr_out_str(ctx->dl, "global", - global ? "true" : "false"); + print_bool(PRINT_ANY, "global", " global %s", global); } pr_out_array_start(ctx->dl, "field"); pr_out_dpipe_fields(ctx, header->fields, @@ -4949,20 +4965,21 @@ static void pr_out_dpipe_action(struct dpipe_action *action, struct dpipe_op_info *op_info = &action->info; const char *mapping; - pr_out_str(ctx->dl, "type", - dpipe_action_type_e2s(action->type)); - pr_out_str(ctx->dl, "header", - dpipe_header_id2s(ctx, op_info->header_id, - op_info->header_global)); - pr_out_str(ctx->dl, "field", - dpipe_field_id2s(ctx, op_info->header_id, - op_info->field_id, - op_info->header_global)); + check_indent_newline(ctx->dl); + print_string(PRINT_ANY, "type", "type %s", + dpipe_action_type_e2s(action->type)); + print_string(PRINT_ANY, "header", " header %s", + dpipe_header_id2s(ctx, op_info->header_id, + op_info->header_global)); + print_string(PRINT_ANY, "field", " field %s", + dpipe_field_id2s(ctx, op_info->header_id, + op_info->field_id, + op_info->header_global)); mapping = dpipe_mapping_get(ctx, op_info->header_id, op_info->field_id, op_info->header_global); if (mapping) - pr_out_str(ctx->dl, "mapping", mapping); + print_string(PRINT_ANY, "mapping", " mapping %s", mapping); } static int dpipe_action_parse(struct dpipe_action *action, struct nlattr *nl) @@ -5031,20 +5048,21 @@ static void pr_out_dpipe_match(struct dpipe_match *match, struct dpipe_op_info *op_info = &match->info; const char *mapping; - pr_out_str(ctx->dl, "type", - dpipe_match_type_e2s(match->type)); - pr_out_str(ctx->dl, "header", - dpipe_header_id2s(ctx, op_info->header_id, - op_info->header_global)); - pr_out_str(ctx->dl, "field", - dpipe_field_id2s(ctx, op_info->header_id, - op_info->field_id, - op_info->header_global)); + check_indent_newline(ctx->dl); + print_string(PRINT_ANY, "type", "type %s", + dpipe_match_type_e2s(match->type)); + print_string(PRINT_ANY, "header", " header %s", + dpipe_header_id2s(ctx, op_info->header_id, + op_info->header_global)); + print_string(PRINT_ANY, "field", " field %s", + dpipe_field_id2s(ctx, op_info->header_id, + op_info->field_id, + op_info->header_global)); mapping = dpipe_mapping_get(ctx, op_info->header_id, op_info->field_id, op_info->header_global); if (mapping) - pr_out_str(ctx->dl, "mapping", mapping); + print_string(PRINT_ANY, "mapping", " mapping %s", mapping); } static int dpipe_match_parse(struct dpipe_match *match, @@ -5149,7 +5167,8 @@ resource_path_print(struct dl *dl, struct resources *resources, path -= strlen(del); memcpy(path, del, strlen(del)); } - pr_out_str(dl, "resource_path", path); + check_indent_newline(dl); + print_string(PRINT_ANY, "resource_path", "resource_path %s", path); free(path); } @@ -5194,10 +5213,10 @@ static int dpipe_table_show(struct dpipe_ctx *ctx, struct nlattr *nl) if (!ctx->print_tables) return 0; - pr_out_str(ctx->dl, "name", table->name); + check_indent_newline(ctx->dl); + print_string(PRINT_ANY, "name", "name %s", table->name); pr_out_uint(ctx->dl, "size", size); - pr_out_str(ctx->dl, "counters_enabled", - counters_enabled ? "true" : "false"); + print_bool(PRINT_ANY, "counters_enabled", " counters_enabled %s", counters_enabled); if (resource_valid) { resource_units = mnl_attr_get_u32(nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS]); @@ -5374,7 +5393,8 @@ static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx *ctx, struct in_addr ip_addr; ip_addr.s_addr = htonl(*(uint32_t *)value); - pr_out_str(ctx->dl, dpipe_value_type_e2s(type), inet_ntoa(ip_addr)); + check_indent_newline(ctx->dl); + print_string_name_value(dpipe_value_type_e2s(type), inet_ntoa(ip_addr)); } static void @@ -5382,8 +5402,9 @@ dpipe_field_printer_ethernet_addr(struct dpipe_ctx *ctx, enum dpipe_value_type type, void *value) { - pr_out_str(ctx->dl, dpipe_value_type_e2s(type), - ether_ntoa((struct ether_addr *)value)); + check_indent_newline(ctx->dl); + print_string_name_value(dpipe_value_type_e2s(type), + ether_ntoa((struct ether_addr *)value)); } static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx *ctx, @@ -5393,7 +5414,8 @@ static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx *ctx, char str[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, value, str, INET6_ADDRSTRLEN); - pr_out_str(ctx->dl, dpipe_value_type_e2s(type), str); + check_indent_newline(ctx->dl); + print_string_name_value(dpipe_value_type_e2s(type), str); } static struct dpipe_field_printer dpipe_field_printers_ipv4[] = { @@ -5872,7 +5894,8 @@ static void resource_show(struct resource *resource, struct dl *dl = ctx->dl; bool array = false; - pr_out_str(dl, "name", resource->name); + check_indent_newline(dl); + print_string(PRINT_ANY, "name", "name %s", resource->name); if (dl->verbose) resource_path_print(dl, ctx->resources, resource->id); pr_out_u64(dl, "size", resource->size); @@ -5880,7 +5903,8 @@ static void resource_show(struct resource *resource, pr_out_u64(dl, "size_new", resource->size_new); if (resource->occ_valid) pr_out_uint(dl, "occ", resource->size_occ); - pr_out_str(dl, "unit", resource_unit_str_get(resource->unit)); + print_string(PRINT_ANY, "unit", " unit %s", + resource_unit_str_get(resource->unit)); if (resource->size_min != resource->size_max) { pr_out_uint(dl, "size_min", resource->size_min); @@ -5896,14 +5920,17 @@ static void resource_show(struct resource *resource, if (array) pr_out_array_start(dl, "dpipe_tables"); else - pr_out_str(dl, "dpipe_tables", "none"); + print_string(PRINT_ANY, "dpipe_tables", " dpipe_tables none", + "none"); list_for_each_entry(table, &ctx->tables->table_list, list) { if (table->resource_id != resource->id || !table->resource_valid) continue; pr_out_entry_start(dl); - pr_out_str(dl, "table_name", table->name); + check_indent_newline(dl); + print_string(PRINT_ANY, "table_name", "table_name %s", + table->name); pr_out_entry_end(dl); } if (array) @@ -5912,9 +5939,11 @@ static void resource_show(struct resource *resource, if (list_empty(&resource->resource_list)) return; - if (ctx->pending_change) - pr_out_str(dl, "size_valid", resource->size_valid ? - "true" : "false"); + if (ctx->pending_change) { + check_indent_newline(dl); + print_string(PRINT_ANY, "size_valid", "size_valid %s", + resource->size_valid ? "true" : "false"); + } pr_out_array_start(dl, "resources"); list_for_each_entry(child_resource, &resource->resource_list, list) { pr_out_entry_start(dl); @@ -6418,6 +6447,7 @@ static int fmsg_value_show(struct dl *dl, int type, struct nlattr *nl_data) uint8_t *data; uint32_t len; + check_indent_newline(dl); switch (type) { case MNL_TYPE_FLAG: pr_out_bool_value(dl, mnl_attr_get_u8(nl_data)); @@ -6435,7 +6465,7 @@ static int fmsg_value_show(struct dl *dl, int type, struct nlattr *nl_data) pr_out_uint64_value(dl, mnl_attr_get_u64(nl_data)); break; case MNL_TYPE_NUL_STRING: - pr_out_str_value(dl, mnl_attr_get_str(nl_data)); + print_string(PRINT_ANY, NULL, "%s", mnl_attr_get_str(nl_data)); break; case MNL_TYPE_BINARY: len = mnl_attr_get_payload_len(nl_data); @@ -6733,8 +6763,9 @@ static void pr_out_dump_reporter_format_logtime(struct dl *dl, const struct nlat out: strftime(dump_date, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%Y-%m-%d", info); strftime(dump_time, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%H:%M:%S", info); - pr_out_str(dl, "last_dump_date", dump_date); - pr_out_str(dl, "last_dump_time", dump_time); + check_indent_newline(dl); + print_string(PRINT_ANY, "last_dump_date", "last_dump_date %s", dump_date); + print_string(PRINT_ANY, "last_dump_time", " last_dump_time %s", dump_time); } static void pr_out_dump_report_timestamp(struct dl *dl, const struct nlattr *attr) @@ -6752,8 +6783,9 @@ static void pr_out_dump_report_timestamp(struct dl *dl, const struct nlattr *att strftime(dump_date, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%Y-%m-%d", tm); strftime(dump_time, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%H:%M:%S", tm); - pr_out_str(dl, "last_dump_date", dump_date); - pr_out_str(dl, "last_dump_time", dump_time); + check_indent_newline(dl); + print_string(PRINT_ANY, "last_dump_date", "last_dump_date %s", dump_date); + print_string(PRINT_ANY, "last_dump_time", " last_dump_time %s", dump_time); } static void pr_out_health(struct dl *dl, struct nlattr **tb_health) @@ -6775,14 +6807,16 @@ static void pr_out_health(struct dl *dl, struct nlattr **tb_health) pr_out_handle_start_arr(dl, tb_health); - pr_out_str(dl, "reporter", - mnl_attr_get_str(tb[DEVLINK_ATTR_HEALTH_REPORTER_NAME])); + check_indent_newline(dl); + print_string(PRINT_ANY, "reporter", "reporter %s", + mnl_attr_get_str(tb[DEVLINK_ATTR_HEALTH_REPORTER_NAME])); if (!dl->json_output) { __pr_out_newline(); __pr_out_indent_inc(); } state = mnl_attr_get_u8(tb[DEVLINK_ATTR_HEALTH_REPORTER_STATE]); - pr_out_str(dl, "state", health_state_name(state)); + check_indent_newline(dl); + print_string(PRINT_ANY, "state", "state %s", health_state_name(state)); pr_out_u64(dl, "error", mnl_attr_get_u64(tb[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT])); pr_out_u64(dl, "recover", @@ -6923,8 +6957,11 @@ static void pr_out_trap_metadata(struct dl *dl, struct nlattr *attr) struct nlattr *attr_metadata; pr_out_array_start(dl, "metadata"); - mnl_attr_for_each_nested(attr_metadata, attr) - pr_out_str_value(dl, trap_metadata_name(attr_metadata)); + mnl_attr_for_each_nested(attr_metadata, attr) { + check_indent_newline(dl); + print_string(PRINT_ANY, NULL, "%s", + trap_metadata_name(attr_metadata)); + } pr_out_array_end(dl); } @@ -6938,12 +6975,14 @@ static void pr_out_trap(struct dl *dl, struct nlattr **tb, bool array) else __pr_out_handle_start(dl, tb, true, false); - pr_out_str(dl, "name", mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_NAME])); - pr_out_str(dl, "type", trap_type_name(type)); + check_indent_newline(dl); + print_string(PRINT_ANY, "name", "name %s", + mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_NAME])); + print_string(PRINT_ANY, "type", " type %s", trap_type_name(type)); pr_out_bool(dl, "generic", !!tb[DEVLINK_ATTR_TRAP_GENERIC]); - pr_out_str(dl, "action", trap_action_name(action)); - pr_out_str(dl, "group", - mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_GROUP_NAME])); + print_string(PRINT_ANY, "action", " action %s", trap_action_name(action)); + print_string(PRINT_ANY, "group", " group %s", + mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_GROUP_NAME])); if (dl->verbose) pr_out_trap_metadata(dl, tb[DEVLINK_ATTR_TRAP_METADATA]); pr_out_stats(dl, tb[DEVLINK_ATTR_STATS]); @@ -7025,8 +7064,9 @@ static void pr_out_trap_group(struct dl *dl, struct nlattr **tb, bool array) else __pr_out_handle_start(dl, tb, true, false); - pr_out_str(dl, "name", - mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_GROUP_NAME])); + check_indent_newline(dl); + print_string(PRINT_ANY, "name", "name %s", + mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_GROUP_NAME])); pr_out_bool(dl, "generic", !!tb[DEVLINK_ATTR_TRAP_GENERIC]); pr_out_stats(dl, tb[DEVLINK_ATTR_STATS]); pr_out_handle_end(dl); From 5a71671a94572af4c4092e2d91f8d6d384345e0c Mon Sep 17 00:00:00 2001 From: Ron Diskin Date: Thu, 23 Jan 2020 12:32:30 +0200 Subject: [PATCH 13/18] devlink: Replace pr_#type_value wrapper functions with common functions Replace calls for pr_bool/uint/uint64_value with direct calls for the matching common json_print library function: print_bool(), print_uint() and print_u64() Signed-off-by: Ron Diskin Reviewed-by: Moshe Shemesh Acked-by: Jiri Pirko Signed-off-by: Stephen Hemminger --- devlink/devlink.c | 37 +++++-------------------------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index a25dd818..231a2838 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -1905,33 +1905,6 @@ static void pr_out_u64(struct dl *dl, const char *name, uint64_t val) pr_out("%s %"PRIu64, name, val); } -static void pr_out_bool_value(struct dl *dl, bool value) -{ - __pr_out_indent_newline(dl); - if (dl->json_output) - print_bool(PRINT_JSON, NULL, NULL, value); - else - pr_out("%s", value ? "true" : "false"); -} - -static void pr_out_uint_value(struct dl *dl, unsigned int value) -{ - __pr_out_indent_newline(dl); - if (dl->json_output) - print_uint(PRINT_JSON, NULL, NULL, value); - else - pr_out("%u", value); -} - -static void pr_out_uint64_value(struct dl *dl, uint64_t value) -{ - __pr_out_indent_newline(dl); - if (dl->json_output) - print_u64(PRINT_JSON, NULL, NULL, value); - else - pr_out("%"PRIu64, value); -} - static bool is_binary_eol(int i) { return !(i%16); @@ -6450,19 +6423,19 @@ static int fmsg_value_show(struct dl *dl, int type, struct nlattr *nl_data) check_indent_newline(dl); switch (type) { case MNL_TYPE_FLAG: - pr_out_bool_value(dl, mnl_attr_get_u8(nl_data)); + print_bool(PRINT_ANY, NULL, "%s", mnl_attr_get_u8(nl_data)); break; case MNL_TYPE_U8: - pr_out_uint_value(dl, mnl_attr_get_u8(nl_data)); + print_uint(PRINT_ANY, NULL, "%u", mnl_attr_get_u8(nl_data)); break; case MNL_TYPE_U16: - pr_out_uint_value(dl, mnl_attr_get_u16(nl_data)); + print_uint(PRINT_ANY, NULL, "%u", mnl_attr_get_u16(nl_data)); break; case MNL_TYPE_U32: - pr_out_uint_value(dl, mnl_attr_get_u32(nl_data)); + print_uint(PRINT_ANY, NULL, "%u", mnl_attr_get_u32(nl_data)); break; case MNL_TYPE_U64: - pr_out_uint64_value(dl, mnl_attr_get_u64(nl_data)); + print_u64(PRINT_ANY, NULL, "%"PRIu64, mnl_attr_get_u64(nl_data)); break; case MNL_TYPE_NUL_STRING: print_string(PRINT_ANY, NULL, "%s", mnl_attr_get_str(nl_data)); From ff360fe98432f05f10a7f6ad8854563b9b728df7 Mon Sep 17 00:00:00 2001 From: Ron Diskin Date: Thu, 23 Jan 2020 12:32:31 +0200 Subject: [PATCH 14/18] devlink: Replace pr_out_bool/uint() wrappers with common print functions Replace calls for pr_out_bool() and pr_out_uint() with direct calls to common json_print library function print_bool() and print_uint(). Signed-off-by: Ron Diskin Reviewed-by: Moshe Shemesh Acked-by: Jiri Pirko Signed-off-by: Stephen Hemminger --- devlink/devlink.c | 150 ++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 77 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 231a2838..73ce9865 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -1876,23 +1876,6 @@ static void pr_out_port_handle_end(struct dl *dl) pr_out("\n"); } -static void pr_out_bool(struct dl *dl, const char *name, bool val) -{ - if (dl->json_output) - print_bool(PRINT_JSON, name, NULL, val); - else - print_string_name_value(name, val ? "true" : "false"); -} - -static void pr_out_uint(struct dl *dl, const char *name, unsigned int val) -{ - __pr_out_indent_newline(dl); - if (dl->json_output) - print_uint(PRINT_JSON, name, NULL, val); - else - pr_out("%s %u", name, val); -} - static void pr_out_u64(struct dl *dl, const char *name, uint64_t val) { __pr_out_indent_newline(dl); @@ -2346,7 +2329,8 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name, return; print_string(PRINT_ANY, "value", " value %s", vstr); } else { - pr_out_uint(dl, "value", mnl_attr_get_u8(val_attr)); + print_uint(PRINT_ANY, "value", " value %u", + mnl_attr_get_u8(val_attr)); } break; case MNL_TYPE_U16: @@ -2360,7 +2344,8 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name, return; print_string(PRINT_ANY, "value", " value %s", vstr); } else { - pr_out_uint(dl, "value", mnl_attr_get_u16(val_attr)); + print_uint(PRINT_ANY, "value", " value %u", + mnl_attr_get_u16(val_attr)); } break; case MNL_TYPE_U32: @@ -2374,7 +2359,8 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name, return; print_string(PRINT_ANY, "value", " value %s", vstr); } else { - pr_out_uint(dl, "value", mnl_attr_get_u32(val_attr)); + print_uint(PRINT_ANY, "value", " value %u", + mnl_attr_get_u32(val_attr)); } break; case MNL_TYPE_STRING: @@ -2382,7 +2368,7 @@ static void pr_out_param_value(struct dl *dl, const char *nla_name, mnl_attr_get_str(val_attr)); break; case MNL_TYPE_FLAG: - pr_out_bool(dl, "value", val_attr ? true : false); + print_bool(PRINT_ANY, "value", " value %s", val_attr); break; } } @@ -2693,7 +2679,8 @@ static int cmd_dev_show_cb(const struct nlmsghdr *nlh, void *data) if (reload_failed) { __pr_out_handle_start(dl, tb, true, false); - pr_out_bool(dl, "reload_failed", true); + check_indent_newline(dl); + print_bool(PRINT_ANY, "reload_failed", "reload_failed %s", true); pr_out_handle_end(dl); } else { pr_out_handle(dl, tb); @@ -3172,11 +3159,11 @@ static void pr_out_port_pfvf_num(struct dl *dl, struct nlattr **tb) if (tb[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) { fn_num = mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]); - pr_out_uint(dl, "pfnum", fn_num); + print_uint(PRINT_ANY, "pfnum", " pfnum %u", fn_num); } if (tb[DEVLINK_ATTR_PORT_PCI_VF_NUMBER]) { fn_num = mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_PCI_VF_NUMBER]); - pr_out_uint(dl, "vfnum", fn_num); + print_uint(PRINT_ANY, "vfnum", " vfnum %u", fn_num); } } @@ -3228,11 +3215,11 @@ static void pr_out_port(struct dl *dl, struct nlattr **tb) uint32_t port_number; port_number = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_NUMBER]); - pr_out_uint(dl, "port", port_number); + print_uint(PRINT_ANY, "port", " port %u", port_number); } if (tb[DEVLINK_ATTR_PORT_SPLIT_GROUP]) - pr_out_uint(dl, "split_group", - mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_SPLIT_GROUP])); + print_uint(PRINT_ANY, "split_group", " split_group %u", + mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_SPLIT_GROUP])); pr_out_port_handle_end(dl); } @@ -3365,18 +3352,18 @@ static void pr_out_sb(struct dl *dl, struct nlattr **tb) { pr_out_handle_start_arr(dl, tb); check_indent_newline(dl); - pr_out_uint(dl, "sb", - mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); - pr_out_uint(dl, "size", - mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_SIZE])); - pr_out_uint(dl, "ing_pools", - mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT])); - pr_out_uint(dl, "eg_pools", - mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT])); - pr_out_uint(dl, "ing_tcs", - mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_TC_COUNT])); - pr_out_uint(dl, "eg_tcs", - mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_TC_COUNT])); + print_uint(PRINT_ANY, "sb", "sb %u", + mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); + print_uint(PRINT_ANY, "size", " size %u", + mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_SIZE])); + print_uint(PRINT_ANY, "ing_pools", " ing_pools %u", + mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT])); + print_uint(PRINT_ANY, "eg_pools", " eg_pools %u", + mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT])); + print_uint(PRINT_ANY, "ing_tcs", " ing_tcs %u", + mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_TC_COUNT])); + print_uint(PRINT_ANY, "eg_tcs", " eg_tcs %u", + mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_TC_COUNT])); pr_out_handle_end(dl); } @@ -3442,19 +3429,20 @@ static const char *threshold_type_name(uint8_t type) static void pr_out_sb_pool(struct dl *dl, struct nlattr **tb) { pr_out_handle_start_arr(dl, tb); - pr_out_uint(dl, "sb", - mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); - pr_out_uint(dl, "pool", - mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); + check_indent_newline(dl); + print_uint(PRINT_ANY, "sb", "sb %u", + mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); + print_uint(PRINT_ANY, "pool", " pool %u", + mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); print_string(PRINT_ANY, "type", " type %s", pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]))); - pr_out_uint(dl, "size", - mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_SIZE])); + print_uint(PRINT_ANY, "size", " size %u", + mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_SIZE])); print_string(PRINT_ANY, "thtype", " thtype %s", threshold_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]))); if (tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE]) - pr_out_uint(dl, "cell_size", - mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE])); + print_uint(PRINT_ANY, "cell_size", " cell size %u", + mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE])); pr_out_handle_end(dl); } @@ -3534,12 +3522,13 @@ static int cmd_sb_pool(struct dl *dl) static void pr_out_sb_port_pool(struct dl *dl, struct nlattr **tb) { pr_out_port_handle_start_arr(dl, tb, true); - pr_out_uint(dl, "sb", - mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); - pr_out_uint(dl, "pool", - mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); - pr_out_uint(dl, "threshold", - mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD])); + check_indent_newline(dl); + print_uint(PRINT_ANY, "sb", "sb %u", + mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); + print_uint(PRINT_ANY, "pool", " pool %u", + mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); + print_uint(PRINT_ANY, "threshold", " threshold %u", + mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD])); pr_out_port_handle_end(dl); } @@ -3632,16 +3621,17 @@ static int cmd_sb_port(struct dl *dl) static void pr_out_sb_tc_bind(struct dl *dl, struct nlattr **tb) { pr_out_port_handle_start_arr(dl, tb, true); - pr_out_uint(dl, "sb", - mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); - pr_out_uint(dl, "tc", - mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX])); + check_indent_newline(dl); + print_uint(PRINT_ANY, "sb", "sb %u", + mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); + print_uint(PRINT_ANY, "tc", " tc %u", + mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX])); print_string(PRINT_ANY, "type", " type %s", pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]))); - pr_out_uint(dl, "pool", - mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); - pr_out_uint(dl, "threshold", - mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD])); + print_uint(PRINT_ANY, "pool", " pool %u", + mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); + print_uint(PRINT_ANY, "threshold", " threshold %u", + mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD])); pr_out_port_handle_end(dl); } @@ -4695,8 +4685,8 @@ static void pr_out_dpipe_fields(struct dpipe_ctx *ctx, check_indent_newline(ctx->dl); print_string(PRINT_ANY, "name", "name %s", field->name); if (ctx->dl->verbose) - pr_out_uint(ctx->dl, "id", field->id); - pr_out_uint(ctx->dl, "bitwidth", field->bitwidth); + print_uint(PRINT_ANY, "id", " id %u", field->id); + print_uint(PRINT_ANY, "bitwidth", " bitwidth %u", field->bitwidth); if (field->mapping_type) { print_string(PRINT_ANY, "mapping_type", " mapping_type %s", dpipe_field_mapping_e2s(field->mapping_type)); @@ -4713,7 +4703,7 @@ pr_out_dpipe_header(struct dpipe_ctx *ctx, struct nlattr **tb, check_indent_newline(ctx->dl); print_string(PRINT_ANY, "name", "name %s", header->name); if (ctx->dl->verbose) { - pr_out_uint(ctx->dl, "id", header->id); + print_uint(PRINT_ANY, "id", " id %u", header->id); print_bool(PRINT_ANY, "global", " global %s", global); } pr_out_array_start(ctx->dl, "field"); @@ -5188,14 +5178,15 @@ static int dpipe_table_show(struct dpipe_ctx *ctx, struct nlattr *nl) check_indent_newline(ctx->dl); print_string(PRINT_ANY, "name", "name %s", table->name); - pr_out_uint(ctx->dl, "size", size); + print_uint(PRINT_ANY, "size", " size %u", size); print_bool(PRINT_ANY, "counters_enabled", " counters_enabled %s", counters_enabled); if (resource_valid) { resource_units = mnl_attr_get_u32(nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS]); resource_path_print(ctx->dl, ctx->resources, table->resource_id); - pr_out_uint(ctx->dl, "resource_units", resource_units); + print_uint(PRINT_ANY, "resource_units", " resource_units %u", + resource_units); } pr_out_array_start(ctx->dl, "match"); @@ -5478,7 +5469,8 @@ static void __pr_out_entry_value(struct dpipe_ctx *ctx, if (value_len == sizeof(uint32_t)) { uint32_t *value_32 = value; - pr_out_uint(ctx->dl, dpipe_value_type_e2s(type), *value_32); + check_indent_newline(ctx->dl); + print_uint_name_value(dpipe_value_type_e2s(type), *value_32); } } @@ -5499,7 +5491,8 @@ static void pr_out_dpipe_entry_value(struct dpipe_ctx *ctx, if (mapping) { value_mapping = mnl_attr_get_u32(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MAPPING]); - pr_out_uint(ctx->dl, "mapping_value", value_mapping); + check_indent_newline(ctx->dl); + print_uint(PRINT_ANY, "mapping_value", "mapping_value %u", value_mapping); } if (mask) { @@ -5616,12 +5609,13 @@ static int dpipe_entry_show(struct dpipe_ctx *ctx, struct nlattr *nl) return -EINVAL; } + check_indent_newline(ctx->dl); entry_index = mnl_attr_get_u32(nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_INDEX]); - pr_out_uint(ctx->dl, "index", entry_index); + print_uint(PRINT_ANY, "index", "index %u", entry_index); if (nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER]) { counter = mnl_attr_get_u64(nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER]); - pr_out_uint(ctx->dl, "counter", counter); + print_uint(PRINT_ANY, "counter", " counter %u", counter); } pr_out_array_start(ctx->dl, "match_value"); @@ -5875,14 +5869,16 @@ static void resource_show(struct resource *resource, if (resource->size != resource->size_new) pr_out_u64(dl, "size_new", resource->size_new); if (resource->occ_valid) - pr_out_uint(dl, "occ", resource->size_occ); + print_uint(PRINT_ANY, "occ", " occ %u", resource->size_occ); print_string(PRINT_ANY, "unit", " unit %s", resource_unit_str_get(resource->unit)); if (resource->size_min != resource->size_max) { - pr_out_uint(dl, "size_min", resource->size_min); + print_uint(PRINT_ANY, "size_min", " size_min %u", + resource->size_min); pr_out_u64(dl, "size_max", resource->size_max); - pr_out_uint(dl, "size_gran", resource->size_gran); + print_uint(PRINT_ANY, "size_gran", " size_gran %u", + resource->size_gran); } list_for_each_entry(table, &ctx->tables->table_list, list) @@ -6802,8 +6798,8 @@ static void pr_out_health(struct dl *dl, struct nlattr **tb_health) pr_out_u64(dl, "grace_period", mnl_attr_get_u64(tb[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])); if (tb[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]) - pr_out_bool(dl, "auto_recover", - mnl_attr_get_u8(tb[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])); + print_bool(PRINT_ANY, "auto_recover", " auto_recover %s", + mnl_attr_get_u8(tb[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])); __pr_out_indent_dec(); pr_out_handle_end(dl); @@ -6952,7 +6948,7 @@ static void pr_out_trap(struct dl *dl, struct nlattr **tb, bool array) print_string(PRINT_ANY, "name", "name %s", mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_NAME])); print_string(PRINT_ANY, "type", " type %s", trap_type_name(type)); - pr_out_bool(dl, "generic", !!tb[DEVLINK_ATTR_TRAP_GENERIC]); + print_bool(PRINT_ANY, "generic", " generic %s", !!tb[DEVLINK_ATTR_TRAP_GENERIC]); print_string(PRINT_ANY, "action", " action %s", trap_action_name(action)); print_string(PRINT_ANY, "group", " group %s", mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_GROUP_NAME])); @@ -7040,7 +7036,7 @@ static void pr_out_trap_group(struct dl *dl, struct nlattr **tb, bool array) check_indent_newline(dl); print_string(PRINT_ANY, "name", "name %s", mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_GROUP_NAME])); - pr_out_bool(dl, "generic", !!tb[DEVLINK_ATTR_TRAP_GENERIC]); + print_bool(PRINT_ANY, "generic", " generic %s", !!tb[DEVLINK_ATTR_TRAP_GENERIC]); pr_out_stats(dl, tb[DEVLINK_ATTR_STATS]); pr_out_handle_end(dl); } From d4df55404aec707bd55c9264c666ddb1bb05d7f1 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Mon, 27 Jan 2020 05:53:09 -0800 Subject: [PATCH 15/18] v5.5.0 --- include/SNAPSHOT.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/SNAPSHOT.h b/include/SNAPSHOT.h index b98ad502..c0fa1bb4 100644 --- a/include/SNAPSHOT.h +++ b/include/SNAPSHOT.h @@ -1 +1 @@ -static const char SNAPSHOT[] = "191125"; +static const char SNAPSHOT[] = "200127"; From aa6d6b223b0f5ac77a14a7a9ca4c97bc47d01b47 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Wed, 29 Jan 2020 05:45:53 -0800 Subject: [PATCH 16/18] uapi: update bpf.h and btf.h Upstream headers from 5.6 pre rc1 Signed-off-by: Stephen Hemminger --- include/uapi/linux/bpf.h | 72 +++++++++++++++++++++++++++++++++++++--- include/uapi/linux/btf.h | 6 ++++ 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 3e3f6e8d..2a8701bf 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -107,6 +107,10 @@ enum bpf_cmd { BPF_MAP_LOOKUP_AND_DELETE_ELEM, BPF_MAP_FREEZE, BPF_BTF_GET_NEXT_ID, + BPF_MAP_LOOKUP_BATCH, + BPF_MAP_LOOKUP_AND_DELETE_BATCH, + BPF_MAP_UPDATE_BATCH, + BPF_MAP_DELETE_BATCH, }; enum bpf_map_type { @@ -136,6 +140,7 @@ enum bpf_map_type { BPF_MAP_TYPE_STACK, BPF_MAP_TYPE_SK_STORAGE, BPF_MAP_TYPE_DEVMAP_HASH, + BPF_MAP_TYPE_STRUCT_OPS, }; /* Note that tracing related programs such as @@ -174,6 +179,8 @@ enum bpf_prog_type { BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, BPF_PROG_TYPE_CGROUP_SOCKOPT, BPF_PROG_TYPE_TRACING, + BPF_PROG_TYPE_STRUCT_OPS, + BPF_PROG_TYPE_EXT, }; enum bpf_attach_type { @@ -357,7 +364,12 @@ enum bpf_attach_type { /* Enable memory-mapping BPF map */ #define BPF_F_MMAPABLE (1U << 10) -/* flags for BPF_PROG_QUERY */ +/* Flags for BPF_PROG_QUERY. */ + +/* Query effective (directly attached + inherited from ancestor cgroups) + * programs that will be executed for events within a cgroup. + * attach_flags with this flag are returned only for directly attached programs. + */ #define BPF_F_QUERY_EFFECTIVE (1U << 0) enum bpf_stack_build_id_status { @@ -397,6 +409,10 @@ union bpf_attr { __u32 btf_fd; /* fd pointing to a BTF type data */ __u32 btf_key_type_id; /* BTF type_id of the key */ __u32 btf_value_type_id; /* BTF type_id of the value */ + __u32 btf_vmlinux_value_type_id;/* BTF type_id of a kernel- + * struct stored as the + * map value + */ }; struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */ @@ -409,6 +425,23 @@ union bpf_attr { __u64 flags; }; + struct { /* struct used by BPF_MAP_*_BATCH commands */ + __aligned_u64 in_batch; /* start batch, + * NULL to start from beginning + */ + __aligned_u64 out_batch; /* output: next start batch */ + __aligned_u64 keys; + __aligned_u64 values; + __u32 count; /* input/output: + * input: # of key/value + * elements + * output: # of filled elements + */ + __u32 map_fd; + __u64 elem_flags; + __u64 flags; + } batch; + struct { /* anonymous struct used by BPF_PROG_LOAD command */ __u32 prog_type; /* one of enum bpf_prog_type */ __u32 insn_cnt; @@ -2703,7 +2736,8 @@ union bpf_attr { * * int bpf_send_signal(u32 sig) * Description - * Send signal *sig* to the current task. + * Send signal *sig* to the process of the current task. + * The signal may be delivered to any of this process's threads. * Return * 0 on success or successfully queued. * @@ -2831,6 +2865,33 @@ union bpf_attr { * Return * On success, the strictly positive length of the string, including * the trailing NUL character. On error, a negative value. + * + * int bpf_tcp_send_ack(void *tp, u32 rcv_nxt) + * Description + * Send out a tcp-ack. *tp* is the in-kernel struct tcp_sock. + * *rcv_nxt* is the ack_seq to be sent out. + * Return + * 0 on success, or a negative error in case of failure. + * + * int bpf_send_signal_thread(u32 sig) + * Description + * Send signal *sig* to the thread corresponding to the current task. + * Return + * 0 on success or successfully queued. + * + * **-EBUSY** if work queue under nmi is full. + * + * **-EINVAL** if *sig* is invalid. + * + * **-EPERM** if no permission to send the *sig*. + * + * **-EAGAIN** if bpf program can try again. + * + * u64 bpf_jiffies64(void) + * Description + * Obtain the 64bit jiffies + * Return + * The 64 bit jiffies */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -2948,7 +3009,10 @@ union bpf_attr { FN(probe_read_user), \ FN(probe_read_kernel), \ FN(probe_read_user_str), \ - FN(probe_read_kernel_str), + FN(probe_read_kernel_str), \ + FN(tcp_send_ack), \ + FN(send_signal_thread), \ + FN(jiffies64), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -3349,7 +3413,7 @@ struct bpf_map_info { __u32 map_flags; char name[BPF_OBJ_NAME_LEN]; __u32 ifindex; - __u32 :32; + __u32 btf_vmlinux_value_type_id; __u64 netns_dev; __u64 netns_ino; __u32 btf_id; diff --git a/include/uapi/linux/btf.h b/include/uapi/linux/btf.h index 7467223d..4a42eb48 100644 --- a/include/uapi/linux/btf.h +++ b/include/uapi/linux/btf.h @@ -146,6 +146,12 @@ enum { BTF_VAR_GLOBAL_EXTERN = 2, }; +enum btf_func_linkage { + BTF_FUNC_STATIC = 0, + BTF_FUNC_GLOBAL = 1, + BTF_FUNC_EXTERN = 2, +}; + /* BTF_KIND_VAR is followed by a single "struct btf_var" to describe * additional information related to the variable such as its linkage. */ From 74da2715519aa4ff3fa4e9de6f24fa20d6e520b9 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Wed, 29 Jan 2020 05:46:43 -0800 Subject: [PATCH 17/18] uapi/pkt_sched: upstream changes from fq_pie Signed-off-by: Stephen Hemminger --- include/uapi/linux/pkt_sched.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/include/uapi/linux/pkt_sched.h b/include/uapi/linux/pkt_sched.h index bf5a5b1d..bbe791b2 100644 --- a/include/uapi/linux/pkt_sched.h +++ b/include/uapi/linux/pkt_sched.h @@ -971,6 +971,37 @@ struct tc_pie_xstats { __u32 ecn_mark; /* packets marked with ecn*/ }; +/* FQ PIE */ +enum { + TCA_FQ_PIE_UNSPEC, + TCA_FQ_PIE_LIMIT, + TCA_FQ_PIE_FLOWS, + TCA_FQ_PIE_TARGET, + TCA_FQ_PIE_TUPDATE, + TCA_FQ_PIE_ALPHA, + TCA_FQ_PIE_BETA, + TCA_FQ_PIE_QUANTUM, + TCA_FQ_PIE_MEMORY_LIMIT, + TCA_FQ_PIE_ECN_PROB, + TCA_FQ_PIE_ECN, + TCA_FQ_PIE_BYTEMODE, + TCA_FQ_PIE_DQ_RATE_ESTIMATOR, + __TCA_FQ_PIE_MAX +}; +#define TCA_FQ_PIE_MAX (__TCA_FQ_PIE_MAX - 1) + +struct tc_fq_pie_xstats { + __u32 packets_in; /* total number of packets enqueued */ + __u32 dropped; /* packets dropped due to fq_pie_action */ + __u32 overlimit; /* dropped due to lack of space in queue */ + __u32 overmemory; /* dropped due to lack of memory in queue */ + __u32 ecn_mark; /* packets marked with ecn */ + __u32 new_flow_count; /* count of new flows created by packets */ + __u32 new_flows_len; /* count of flows in new list */ + __u32 old_flows_len; /* count of flows in old list */ + __u32 memory_usage; /* total memory across all queues */ +}; + /* CBS */ struct tc_cbs_qopt { __u8 offload; From f9ed2db593e45e6e2ef7c7d0abed63dda25ef3d3 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Wed, 29 Jan 2020 05:48:19 -0800 Subject: [PATCH 18/18] uapi: updates to tcp.h, snmp.h and if_bridge.h Upstream changes Signed-off-by: Stephen Hemminger --- include/uapi/linux/if_bridge.h | 2 ++ include/uapi/linux/snmp.h | 2 ++ include/uapi/linux/tcp.h | 1 + 3 files changed, 5 insertions(+) diff --git a/include/uapi/linux/if_bridge.h b/include/uapi/linux/if_bridge.h index 18e8f3c4..e2e925ff 100644 --- a/include/uapi/linux/if_bridge.h +++ b/include/uapi/linux/if_bridge.h @@ -130,6 +130,7 @@ enum { #define BRIDGE_VLAN_INFO_RANGE_BEGIN (1<<3) /* VLAN is start of vlan range */ #define BRIDGE_VLAN_INFO_RANGE_END (1<<4) /* VLAN is end of vlan range */ #define BRIDGE_VLAN_INFO_BRENTRY (1<<5) /* Global bridge VLAN entry */ +#define BRIDGE_VLAN_INFO_ONLY_OPTS (1<<6) /* Skip create/delete/flags */ struct bridge_vlan_info { __u16 flags; @@ -190,6 +191,7 @@ enum { BRIDGE_VLANDB_ENTRY_UNSPEC, BRIDGE_VLANDB_ENTRY_INFO, BRIDGE_VLANDB_ENTRY_RANGE, + BRIDGE_VLANDB_ENTRY_STATE, __BRIDGE_VLANDB_ENTRY_MAX, }; #define BRIDGE_VLANDB_ENTRY_MAX (__BRIDGE_VLANDB_ENTRY_MAX - 1) diff --git a/include/uapi/linux/snmp.h b/include/uapi/linux/snmp.h index 7eee233e..7d91f4de 100644 --- a/include/uapi/linux/snmp.h +++ b/include/uapi/linux/snmp.h @@ -285,6 +285,8 @@ enum LINUX_MIB_TCPRCVQDROP, /* TCPRcvQDrop */ LINUX_MIB_TCPWQUEUETOOBIG, /* TCPWqueueTooBig */ LINUX_MIB_TCPFASTOPENPASSIVEALTKEY, /* TCPFastOpenPassiveAltKey */ + LINUX_MIB_TCPTIMEOUTREHASH, /* TCPTimeoutRehash */ + LINUX_MIB_TCPDUPLICATEDATAREHASH, /* TCPDuplicateDataRehash */ __LINUX_MIB_MAX }; diff --git a/include/uapi/linux/tcp.h b/include/uapi/linux/tcp.h index 0a2c423e..d79550b3 100644 --- a/include/uapi/linux/tcp.h +++ b/include/uapi/linux/tcp.h @@ -311,6 +311,7 @@ enum { TCP_NLA_DSACK_DUPS, /* DSACK blocks received */ TCP_NLA_REORD_SEEN, /* reordering events seen */ TCP_NLA_SRTT, /* smoothed RTT in usecs */ + TCP_NLA_TIMEOUT_REHASH, /* Timeout-triggered rehash attempts */ }; /* for TCP_MD5SIG socket option */