From patchwork Thu Aug 17 17:36:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Fortin X-Patchwork-Id: 802806 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="RK8nuPom"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xYD6g4BtGz9t4X for ; Fri, 18 Aug 2017 03:41:47 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753619AbdHQRlq (ORCPT ); Thu, 17 Aug 2017 13:41:46 -0400 Received: from mail-wr0-f177.google.com ([209.85.128.177]:33669 "EHLO mail-wr0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753603AbdHQRlo (ORCPT ); Thu, 17 Aug 2017 13:41:44 -0400 Received: by mail-wr0-f177.google.com with SMTP id b65so50136378wrd.0 for ; Thu, 17 Aug 2017 10:41:43 -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=f4Mu3kHtv6ZQjU8+7rH6pkUOjjhp2/2m2NS/dVQYwRI=; b=RK8nuPomRfAYep+TNB0I4FHt5arPOE+PSzfVU/NVmzgrNMAJ4mDx70FgHUrdgdQLSu mDTXEl+1GYtZ4vW9e9c7RuJuA94pay++ShyBq6XvNPzf1gd7PUmHYg3uPfFcVW90qLD2 bq8Of8UQ7OdHMg97XF1YTZOWEDVGkqcMQY2Pw= 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=f4Mu3kHtv6ZQjU8+7rH6pkUOjjhp2/2m2NS/dVQYwRI=; b=kpFuN1Wloxhk94P+rMdPnm304RHj9YlO+L9clJ6MHMwAmBa/7RusBpIFL7vdmLORuG EkqFQfbSSbERr5ljwMG8TFZ8bkobH0DGEREBxv0j75wjsIAWKzjHDkgDSNPJHyfpJQzZ yk/6jlX7LwgrJHBvP1NvseQcXjD+/7kMUFItjhw+YmZpEdt9UvO8LgyUBtP9J0j9tXZ7 EnpZXiqlhj/QzT30gc9I33YeIH6gDikmdQp+E8e7cpwULtp4Gf8FgAx6ROqh+bTitAos 9uU1b06oeHxuiNiwTESefk2TQa4hBfURL2HvPPF+4Eqo6TGO8OOfIolAq5naZR0CQR77 BLOA== X-Gm-Message-State: AHYfb5ilvEPZf+v8pXVVEYi1UMXXM3+JWo0yAVIKOze81IvkCZ7/qdJG eFCifNB6V4Pv+/M9n9jF6wLA X-Received: by 10.28.212.147 with SMTP id l141mr1910660wmg.13.1502991702517; Thu, 17 Aug 2017 10:41:42 -0700 (PDT) Received: from localhost.localdomain ([37.169.21.172]) by smtp.googlemail.com with ESMTPSA id k13sm3902040wrd.4.2017.08.17.10.41.37 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 17 Aug 2017 10:41:42 -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 19/27] ip: ipmacsec.c: add json output support Date: Thu, 17 Aug 2017 10:36:06 -0700 Message-Id: <20170817173614.54987-20-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 { "sci": { "type": "string", "attr": "IFLA_MACSEC_SCI" }, "protect": { "type": "string", "attr": "IFLA_MACSEC_PROTECT" }, "cipher_suite": { "type": "string", "attr": "IFLA_MACSEC_CIPHER_SUITE" }, "icv_len": { "type": "uint", "attr": "IFLA_MACSEC_ICV_LEN" }, "encoding_sa": { "type": "uint", "attr": "IFLA_MACSEC_ENCODING_SA" }, "validation": { "type": "string", "attr": "IFLA_MACSEC_VALIDATION" }, "encrypt": { "type": "string", "attr": "IFLA_MACSEC_ENCRYPT" }, "inc_sci": { "type": "string", "attr": "IFLA_MACSEC_INC_SCI" }, "es": { "type": "string", "attr": "IFLA_MACSEC_ES" }, "scb": { "type": "string", "attr": "IFLA_MACSEC_SCB" }, "replay_protect": { "type": "string", "attr": "IFLA_MACSEC_REPLAY_PROTECT" }, "window": { "type": "int", "attr": "" } } Signed-off-by: Julien Fortin --- ip/ipmacsec.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 18 deletions(-) diff --git a/ip/ipmacsec.c b/ip/ipmacsec.c index aa89a00f..ecc371a5 100644 --- a/ip/ipmacsec.c +++ b/ip/ipmacsec.c @@ -561,9 +561,14 @@ static int validate_secy_dump(struct rtattr **attrs) static void print_flag(FILE *f, struct rtattr *attrs[], const char *desc, int field) { - if (attrs[field]) - fprintf(f, "%s %s ", desc, - values_on_off[!!rta_getattr_u8(attrs[field])]); + if (attrs[field]) { + const char *v = values_on_off[!!rta_getattr_u8(attrs[field])]; + + if (is_json_context()) + print_string(PRINT_JSON, desc, NULL, v); + else + fprintf(f, "%s %s ", desc, v); + } } #define DEFAULT_CIPHER_NAME "GCM-AES-128" @@ -1017,8 +1022,16 @@ static void macsec_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) return; if (tb[IFLA_MACSEC_SCI]) { - fprintf(f, "sci %016llx ", - ntohll(rta_getattr_u64(tb[IFLA_MACSEC_SCI]))); + if (is_json_context()) { + SPRINT_BUF(b1); + + snprintf(b1, sizeof(b1), "%016llx", + ntohll(rta_getattr_u64(tb[IFLA_MACSEC_SCI]))); + print_string(PRINT_JSON, "sci", NULL, b1); + } else { + fprintf(f, "sci %016llx ", + ntohll(rta_getattr_u64(tb[IFLA_MACSEC_SCI]))); + } } print_flag(f, tb, "protect", IFLA_MACSEC_PROTECT); @@ -1026,35 +1039,70 @@ static void macsec_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_MACSEC_CIPHER_SUITE]) { __u64 csid = rta_getattr_u64(tb[IFLA_MACSEC_CIPHER_SUITE]); - fprintf(f, "cipher %s ", cs_id_to_name(csid)); + print_string(PRINT_ANY, + "cipher_suite", + "cipher %s ", + cs_id_to_name(csid)); } if (tb[IFLA_MACSEC_ICV_LEN]) { - fprintf(f, "icvlen %hhu ", - rta_getattr_u8(tb[IFLA_MACSEC_ICV_LEN])); + if (is_json_context()) { + char b2[4]; + + snprintf(b2, sizeof(b2), "%hhu", + rta_getattr_u8(tb[IFLA_MACSEC_ICV_LEN])); + print_uint(PRINT_JSON, "icv_len", NULL, atoi(b2)); + } else { + fprintf(f, "icvlen %hhu ", + rta_getattr_u8(tb[IFLA_MACSEC_ICV_LEN])); + } } if (tb[IFLA_MACSEC_ENCODING_SA]) { - fprintf(f, "encodingsa %hhu ", - rta_getattr_u8(tb[IFLA_MACSEC_ENCODING_SA])); + if (is_json_context()) { + char b2[4]; + + snprintf(b2, sizeof(b2), "%hhu", + rta_getattr_u8(tb[IFLA_MACSEC_ENCODING_SA])); + print_uint(PRINT_JSON, "encoding_sa", NULL, atoi(b2)); + } else { + fprintf(f, "encodingsa %hhu ", + rta_getattr_u8(tb[IFLA_MACSEC_ENCODING_SA])); + } } if (tb[IFLA_MACSEC_VALIDATION]) { __u8 val = rta_getattr_u8(tb[IFLA_MACSEC_VALIDATION]); - fprintf(f, "validate %s ", VALIDATE_STR[val]); + print_string(PRINT_ANY, + "validation", + "validate %s ", + VALIDATE_STR[val]); + } + + const char *inc_sci, *es, *replay; + + if (is_json_context()) { + inc_sci = "inc_sci"; + replay = "replay_protect"; + es = "es"; + } else { + inc_sci = "send_sci"; + es = "end_station"; + replay = "replay"; } print_flag(f, tb, "encrypt", IFLA_MACSEC_ENCRYPT); - print_flag(f, tb, "send_sci", IFLA_MACSEC_INC_SCI); - print_flag(f, tb, "end_station", IFLA_MACSEC_ES); + print_flag(f, tb, inc_sci, IFLA_MACSEC_INC_SCI); + print_flag(f, tb, es, IFLA_MACSEC_ES); print_flag(f, tb, "scb", IFLA_MACSEC_SCB); + print_flag(f, tb, replay, IFLA_MACSEC_REPLAY_PROTECT); - print_flag(f, tb, "replay", IFLA_MACSEC_REPLAY_PROTECT); - if (tb[IFLA_MACSEC_WINDOW]) { - fprintf(f, "window %d ", - rta_getattr_u32(tb[IFLA_MACSEC_WINDOW])); - } + if (tb[IFLA_MACSEC_WINDOW]) + print_int(PRINT_ANY, + "window", + "window %d ", + rta_getattr_u32(tb[IFLA_MACSEC_WINDOW])); } static bool check_txsc_flags(bool es, bool scb, bool sci)