netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2-next] ip link: add json support for tun attributes
@ 2018-02-26 10:36 Sabrina Dubroca
  2018-02-26 17:35 ` David Ahern
  0 siblings, 1 reply; 2+ messages in thread
From: Sabrina Dubroca @ 2018-02-26 10:36 UTC (permalink / raw)
  To: netdev; +Cc: dsahern, sbrivio, stephen, Sabrina Dubroca

Reported-by: Stephen Hemminger <stephen@networkplumber.org>
Fixes: 118eda77d660 ("ip link: add support to display extended tun attributes")
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Reviewed-by: Stefano Brivio <sbrivio@redhat.com>
---
 ip/iptuntap.c | 55 ++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 34 insertions(+), 21 deletions(-)

diff --git a/ip/iptuntap.c b/ip/iptuntap.c
index 07253870472f..2f49ff6d9281 100644
--- a/ip/iptuntap.c
+++ b/ip/iptuntap.c
@@ -475,9 +475,9 @@ static void print_owner(FILE *f, uid_t uid)
 	struct passwd *pw = getpwuid(uid);
 
 	if (pw)
-		fprintf(f, "user %s ", pw->pw_name);
+		print_string(PRINT_ANY, "user", "user %s ", pw->pw_name);
 	else
-		fprintf(f, "user %u ", uid);
+		print_uint(PRINT_ANY, "user", "user %u ", uid);
 }
 
 static void print_group(FILE *f, gid_t gid)
@@ -485,33 +485,54 @@ static void print_group(FILE *f, gid_t gid)
 	struct group *group = getgrgid(gid);
 
 	if (group)
-		fprintf(f, "group %s ", group->gr_name);
+		print_string(PRINT_ANY, "group", "group %s ", group->gr_name);
 	else
-		fprintf(f, "group %u ", gid);
+		print_uint(PRINT_ANY, "group", "group %u ", gid);
 }
 
 static void print_mq(FILE *f, struct rtattr *tb[])
 {
 	if (!tb[IFLA_TUN_MULTI_QUEUE] ||
-	    !rta_getattr_u8(tb[IFLA_TUN_MULTI_QUEUE]))
+	    !rta_getattr_u8(tb[IFLA_TUN_MULTI_QUEUE])) {
+		if (is_json_context())
+			print_bool(PRINT_JSON, "multi_queue", NULL, false);
 		return;
+	}
 
-	fprintf(f, "multi_queue ");
+	print_bool(PRINT_ANY, "multi_queue", "multi_queue ", true);
 
 	if (tb[IFLA_TUN_NUM_QUEUES]) {
-		fprintf(f, "numqueues %u ",
-			rta_getattr_u32(tb[IFLA_TUN_NUM_QUEUES]));
+		print_uint(PRINT_ANY, "numqueues", "numqueues %u ",
+			   rta_getattr_u32(tb[IFLA_TUN_NUM_QUEUES]));
 	}
 
 	if (tb[IFLA_TUN_NUM_DISABLED_QUEUES]) {
-		fprintf(f, "numdisabled %u ",
-			rta_getattr_u32(tb[IFLA_TUN_NUM_DISABLED_QUEUES]));
+		print_uint(PRINT_ANY, "numdisabled", "numdisabled %u ",
+			   rta_getattr_u32(tb[IFLA_TUN_NUM_DISABLED_QUEUES]));
 	}
 }
 
 static void print_onoff(FILE *f, const char *flag, __u8 val)
 {
-	fprintf(f, "%s %s ", flag, val ? "on" : "off");
+	if (is_json_context())
+		print_bool(PRINT_JSON, flag, NULL, !!val);
+	else
+		fprintf(f, "%s %s ", flag, val ? "on" : "off");
+}
+
+static void print_type(FILE *f, __u8 type)
+{
+	SPRINT_BUF(buf);
+	const char *str = buf;
+
+	if (type == IFF_TUN)
+		str = "tun";
+	else if (type == IFF_TAP)
+		str = "tap";
+	else
+		snprintf(buf, sizeof(buf), "UNKNOWN:%hhu", type);
+
+	print_string(PRINT_ANY, "type", "type %s ", str);
 }
 
 static void tun_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
@@ -519,16 +540,8 @@ static void tun_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 	if (!tb)
 		return;
 
-	if (tb[IFLA_TUN_TYPE]) {
-		__u8 type = rta_getattr_u8(tb[IFLA_TUN_TYPE]);
-
-		if (type == IFF_TUN)
-			fprintf(f, "type tun ");
-		else if (type == IFF_TAP)
-			fprintf(f, "type tap ");
-		else
-			fprintf(f, "type UNKNOWN:%hhu ", type);
-	}
+	if (tb[IFLA_TUN_TYPE])
+		print_type(f, rta_getattr_u8(tb[IFLA_TUN_TYPE]));
 
 	if (tb[IFLA_TUN_PI])
 		print_onoff(f, "pi", rta_getattr_u8(tb[IFLA_TUN_PI]));
-- 
2.16.2

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH iproute2-next] ip link: add json support for tun attributes
  2018-02-26 10:36 [PATCH iproute2-next] ip link: add json support for tun attributes Sabrina Dubroca
@ 2018-02-26 17:35 ` David Ahern
  0 siblings, 0 replies; 2+ messages in thread
From: David Ahern @ 2018-02-26 17:35 UTC (permalink / raw)
  To: Sabrina Dubroca, netdev; +Cc: sbrivio, stephen

On 2/26/18 3:36 AM, Sabrina Dubroca wrote:
> Reported-by: Stephen Hemminger <stephen@networkplumber.org>
> Fixes: 118eda77d660 ("ip link: add support to display extended tun attributes")
> Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
> Reviewed-by: Stefano Brivio <sbrivio@redhat.com>
> ---
>  ip/iptuntap.c | 55 ++++++++++++++++++++++++++++++++++---------------------
>  1 file changed, 34 insertions(+), 21 deletions(-)
> 

applied to iproute2-next. thanks for following up

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-02-26 17:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-26 10:36 [PATCH iproute2-next] ip link: add json support for tun attributes Sabrina Dubroca
2018-02-26 17:35 ` David Ahern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).