From patchwork Thu Aug 17 17:36:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Fortin X-Patchwork-Id: 802814 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=cumulusnetworks.com header.i=@cumulusnetworks.com header.b="hZz1MIv6"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xYD7j0WMKz9t4X for ; Fri, 18 Aug 2017 03:42:41 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753786AbdHQRmi (ORCPT ); Thu, 17 Aug 2017 13:42:38 -0400 Received: from mail-wr0-f179.google.com ([209.85.128.179]:33030 "EHLO mail-wr0-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753748AbdHQRmf (ORCPT ); Thu, 17 Aug 2017 13:42:35 -0400 Received: by mail-wr0-f179.google.com with SMTP id b65so50154394wrd.0 for ; Thu, 17 Aug 2017 10:42:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cumulusnetworks.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=AViSl0CuatDwMnIc61fg7w7E1iJaeIblk5tLsjyxZv0=; b=hZz1MIv6Y0bcD900y9YP4HV7gpA/wEh+vMMpUDwaGBIHNCc21OCoouVn388XE6M2gw pDuCgwTaBj2kqYQVlAMxIOuSS7iKEoMWY4/NbiI3hH9QiqBiP1D3Z2w3mMMhKWEH/iXs kmpT3QiC+VzBHKoLOsWri2cLDJ8SarYykldLQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=AViSl0CuatDwMnIc61fg7w7E1iJaeIblk5tLsjyxZv0=; b=DJPEOeQscMtk96lAJmnQ9r0st4WIy5uT69HsQrYBGJ1JPmidxykPiG1ud4aUXTx3hI sQp9OPG56rifMqU9ybgDHsfDJziFNq73oIQDGr82g0NEUjjBnNXI9B6wZu1CaNqbHGPk ppF+znVm8KR6IHxxq+0zUvy6rdBXfPVxUckN2/es+4qDJe0h+ONea+U+o0Ievtmb5/RR rD03wwUujwkuiaFhOJPZtrlUdU1CXcX4oOnGjOkWj+urBwOE749VlmRQCjLHK0S2MdrX 3lWc5BH1sHJLEY/OTv/m95TTHVhtmHwvdREuQjP8CSUE+STrWT0xef4pkCYLK/o9ZHdh wUlw== X-Gm-Message-State: AHYfb5gMImaE5v3kjyZkBIU1OBm9tinkrbzxRKbVpw7izKPDizTUGjA0 UvGRu2B3tKYOJC5U7rgSrI9a X-Received: by 10.223.135.38 with SMTP id a35mr4206784wra.78.1502991753715; Thu, 17 Aug 2017 10:42:33 -0700 (PDT) Received: from localhost.localdomain ([37.169.21.172]) by smtp.googlemail.com with ESMTPSA id k13sm3902040wrd.4.2017.08.17.10.42.30 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 17 Aug 2017 10:42:33 -0700 (PDT) From: Julien Fortin X-Google-Original-From: Julien Fortin To: netdev@vger.kernel.org Cc: roopa@cumulusnetworks.com, nikolay@cumulusnetworks.com, dsa@cumulusnetworks.com, Julien Fortin Subject: [PATCH iproute2 json v2 27/27] ip: iplink_vlan.c: add json output support Date: Thu, 17 Aug 2017 10:36:14 -0700 Message-Id: <20170817173614.54987-28-julien@cumulusnetworks.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170817173614.54987-1-julien@cumulusnetworks.com> References: <20170817173614.54987-1-julien@cumulusnetworks.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Julien Fortin Schema: { "protocol": { "type": "string", "attr": "IFLA_VLAN_PROTOCOL" }, "id": { "type": "uint", "attr": "IFLA_VLAN_ID" }, "flags": { "type": "array", "attr": "IFLA_VLAN_FLAGS", "array": [ { "type": "string" } ] }, "ingress_qos": { "type": "array", "attr": "IFLA_VLAN_INGRESS_QOS", "array": [ { "type": "dict", "dict": { "from": { "type": "uint" }, "to": { "type": "uint" } } } ] }, "egress_qos": { "type": "array", "attr": "IFLA_VLAN_EGRESS_QOS", "array": [ { "type": "dict", "dict": { "from": { "type": "uint" }, "to": { "type": "uint" } } } ] } } $ ip link add name eth0.42 link eth0 type vlan id 42 $ ip -details -json link show [{ "ifindex": 30, "ifname": "eth0.42", "link": "eth0", "flags": ["BROADCAST","MULTICAST"], "mtu": 1500, "qdisc": "noop", "operstate": "DOWN", "linkmode": "DEFAULT", "group": "default", "link_type": "ether", "address": "08:00:27:db:31:88", "broadcast": "ff:ff:ff:ff:ff:ff", "promiscuity": 0, "linkinfo": { "info_kind": "vlan", "info_data": { "protocol": "802.1Q", "id": 42, "flags": ["REORDER_HDR"] } }, "inet6_addr_gen_mode": "eui64", "num_tx_queues": 1, "num_rx_queues": 1, "gso_max_size": 65536, "gso_max_segs": 65535 } ] Signed-off-by: Julien Fortin --- ip/iplink_vlan.c | 62 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/ip/iplink_vlan.c b/ip/iplink_vlan.c index b47236d8..4d78cf9e 100644 --- a/ip/iplink_vlan.c +++ b/ip/iplink_vlan.c @@ -164,37 +164,51 @@ static int vlan_parse_opt(struct link_util *lu, int argc, char **argv, return 0; } -static void vlan_print_map(FILE *f, char *name, struct rtattr *attr) +static void vlan_print_map(FILE *f, + const char *name_json, + const char *name_fp, + struct rtattr *attr) { struct ifla_vlan_qos_mapping *m; struct rtattr *i; int rem; - fprintf(f, "\n %s { ", name); + open_json_array(PRINT_JSON, name_json); + print_string(PRINT_FP, NULL, "\n %s { ", name_fp); rem = RTA_PAYLOAD(attr); for (i = RTA_DATA(attr); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) { m = RTA_DATA(i); - fprintf(f, "%u:%u ", m->from, m->to); + + if (is_json_context()) { + open_json_object(NULL); + print_uint(PRINT_JSON, "from", NULL, m->from); + print_uint(PRINT_JSON, "to", NULL, m->to); + close_json_object(); + } else { + fprintf(f, "%u:%u ", m->from, m->to); + } } - fprintf(f, "} "); + + close_json_array(PRINT_JSON, NULL); + print_string(PRINT_FP, NULL, "%s ", "}"); } static void vlan_print_flags(FILE *fp, __u32 flags) { - fprintf(fp, "<"); -#define _PF(f) if (flags & VLAN_FLAG_##f) { \ - flags &= ~VLAN_FLAG_##f; \ - fprintf(fp, #f "%s", flags ? "," : ""); \ - } + open_json_array(PRINT_ANY, is_json_context() ? "flags" : "<"); +#define _PF(f) if (flags & VLAN_FLAG_##f) { \ + flags &= ~VLAN_FLAG_##f; \ + print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); \ + } _PF(REORDER_HDR); _PF(GVRP); _PF(MVRP); _PF(LOOSE_BINDING); #undef _PF if (flags) - fprintf(fp, "%x", flags); - fprintf(fp, "> "); + print_hex(PRINT_ANY, NULL, "%x", flags); + close_json_array(PRINT_ANY, "> "); } static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) @@ -214,13 +228,19 @@ static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) return; if (tb[IFLA_VLAN_PROTOCOL]) - fprintf(f, "protocol %s ", - ll_proto_n2a(rta_getattr_u16(tb[IFLA_VLAN_PROTOCOL]), + print_string(PRINT_ANY, + "protocol", + "protocol %s ", + ll_proto_n2a( + rta_getattr_u16(tb[IFLA_VLAN_PROTOCOL]), b1, sizeof(b1))); else - fprintf(f, "protocol 802.1q "); + print_string(PRINT_ANY, "protocol", "protocol %s ", "802.1q"); - fprintf(f, "id %u ", rta_getattr_u16(tb[IFLA_VLAN_ID])); + print_uint(PRINT_ANY, + "id", + "id %u ", + rta_getattr_u16(tb[IFLA_VLAN_ID])); if (tb[IFLA_VLAN_FLAGS]) { if (RTA_PAYLOAD(tb[IFLA_VLAN_FLAGS]) < sizeof(*flags)) @@ -229,13 +249,19 @@ static void vlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) vlan_print_flags(f, flags->flags); } if (tb[IFLA_VLAN_INGRESS_QOS]) - vlan_print_map(f, "ingress-qos-map", tb[IFLA_VLAN_INGRESS_QOS]); + vlan_print_map(f, + "ingress_qos", + "ingress-qos-map", + tb[IFLA_VLAN_INGRESS_QOS]); if (tb[IFLA_VLAN_EGRESS_QOS]) - vlan_print_map(f, "egress-qos-map", tb[IFLA_VLAN_EGRESS_QOS]); + vlan_print_map(f, + "egress_qos", + "egress-qos-map", + tb[IFLA_VLAN_EGRESS_QOS]); } static void vlan_print_help(struct link_util *lu, int argc, char **argv, - FILE *f) + FILE *f) { print_explain(f); }