From patchwork Thu May 16 07:20:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1935820 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Xet2PjTZ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vg1jL16P1z20KF for ; Thu, 16 May 2024 17:21:10 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 759F741CA9; Thu, 16 May 2024 07:21:08 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id G8D-v1_kmqEG; Thu, 16 May 2024 07:21:06 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=2605:bc80:3010:104::8cd3:938; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org A673F41C63 Authentication-Results: smtp2.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Xet2PjTZ Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp2.osuosl.org (Postfix) with ESMTPS id A673F41C63; Thu, 16 May 2024 07:21:05 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 585E9C0077; Thu, 16 May 2024 07:21:05 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 1D5E3C007C for ; Thu, 16 May 2024 07:21:04 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id BC4CB41CAD for ; Thu, 16 May 2024 07:20:49 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id c9epKvUCqTIZ for ; Thu, 16 May 2024 07:20:46 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=170.10.129.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=jmeng@redhat.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp2.osuosl.org 3490C41CA8 Authentication-Results: smtp2.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 3490C41CA8 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp2.osuosl.org (Postfix) with ESMTPS id 3490C41CA8 for ; Thu, 16 May 2024 07:20:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715844042; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/9SVtKmKmN8Px7Z7JPWZXF8mRbAkTSExvXxmLgIqmFQ=; b=Xet2PjTZsM/BGg20kskHTDcA21eWsh9BmvLyrSWyrdi6Nl71o9cS08hpfemwkxinyYMiHO GaR2Pxp4VLojGQBxTfCYQN7mVf0hufJsqv6HMwOQ/7k67+uBwEgM9RQDQDhu57/DtO2XAf P/7XncHUZdwwzmdlqJyu3VZRsdFKU2s= Received: from mail-lj1-f200.google.com (mail-lj1-f200.google.com [209.85.208.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-695-92d2OVC1NeW8T0ijS3HT7Q-1; Thu, 16 May 2024 03:20:31 -0400 X-MC-Unique: 92d2OVC1NeW8T0ijS3HT7Q-1 Received: by mail-lj1-f200.google.com with SMTP id 38308e7fff4ca-2e289eec4d1so73462591fa.0 for ; Thu, 16 May 2024 00:20:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715844030; x=1716448830; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/9SVtKmKmN8Px7Z7JPWZXF8mRbAkTSExvXxmLgIqmFQ=; b=AojVBy3WXi2Gyr60Hn31m42aecJB89y7VRSsuENOGwKFpj/zch+rX5/tlARrKjGWiK ULNb6vIj7fEJMcIuzhxHPX2Yr6hReSK5ggmcKndmheH+LBffw3nrB0qofY5B0ZzUhpvO 0IKfL6ttVgk1+WFO/rAF8hKZ7umIEDMEYNumMPjwUEHI95x7XG/b8Bp4eDl3/iSFrwKV qMGS57rm/rCJ2Lxmuxx12/C+neVxxysBJslO94nNomDUjaFMs2BolJ9Uzm5ySMeDOZfD Jh9XH3jQLBbpnA7plQmVQPh9sblP50xnfUYxr4atgRiR5rlvA7ny9GWAqHwSKLgDQ0ld t+IA== X-Gm-Message-State: AOJu0YzV1NQ48r4CQDS03d6pyTVYTQ2eoPKxA9FD2V9yESH238R4xEO2 4WgbyJROCmD1TKtsVBi35xXHzk5cVEVRsRaYqW2ARHKvf0ipn/em2Rs8fa8aLab7lWCqoi+zs9w apM4ld1h/twP3dAXCEMmS0HrRdJRXNpAf8OSV8oGwDQbnh8dmmyEMtRXC+SA3jlkJ+qMONiapjr ptV0YCphusJyaHNMMSLmT8wIRkQRms X-Received: by 2002:a2e:958f:0:b0:2e2:9d0:9295 with SMTP id 38308e7fff4ca-2e51fd41f7emr120925631fa.8.1715844030095; Thu, 16 May 2024 00:20:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFl1AXaHX7z328NbeYIaHwxC1nCZ+bnA1hw6EkhTGEqMOTW111A2xHi1mAWuzUAIlIo3XMQiw== X-Received: by 2002:a2e:958f:0:b0:2e2:9d0:9295 with SMTP id 38308e7fff4ca-2e51fd41f7emr120924551fa.8.1715844027590; Thu, 16 May 2024 00:20:27 -0700 (PDT) Received: from positronik4lide.. ([87.122.57.114]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-41f9bc3b12fsm151059505e9.0.2024.05.16.00.20.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 00:20:27 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org, echaudro@redhat.com, ktraynor@redhat.com, aconole@redhat.com, rjarry@redhat.com Date: Thu, 16 May 2024 09:20:15 +0200 Message-Id: <20240516072015.18455-7-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240516072015.18455-1-jmeng@redhat.com> References: <20240516072015.18455-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v10 6/6] ofproto: Add JSON output for 'dpif/show' command. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Jakob Meng The 'dpif/show' command now supports machine-readable JSON output in addition to the plain-text output for humans. An example would be: ovs-appctl --format json dpif/show Reported-at: https://bugzilla.redhat.com/1824861 Signed-off-by: Jakob Meng --- NEWS | 1 + ofproto/ofproto-dpif.c | 120 +++++++++++++++++++++++++++++++++++++---- tests/pmd.at | 23 ++++++++ 3 files changed, 133 insertions(+), 11 deletions(-) diff --git a/NEWS b/NEWS index 479310d49..c434b1497 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,7 @@ Post-v3.3.0 or 'text' (by default). * Added new option [--pretty] to print JSON output in a readable fashion. * 'list-commands' now supports output in JSON format. + * 'dpif/show' now supports output in JSON format. - Python: * Added support for different output formats like 'json' to appctl.py and Python's unixctl classes. diff --git a/ofproto/ofproto-dpif.c b/ofproto/ofproto-dpif.c index 32d037be6..db0405886 100644 --- a/ofproto/ofproto-dpif.c +++ b/ofproto/ofproto-dpif.c @@ -28,6 +28,7 @@ #include "fail-open.h" #include "guarded-list.h" #include "hmapx.h" +#include "json.h" #include "lacp.h" #include "learn.h" #include "mac-learning.h" @@ -6519,8 +6520,99 @@ done: return changed; } +static struct json * +dpif_show_backer_json(struct json *backers, const struct dpif_backer *backer) +{ + struct json *json_backer = json_object_create(); + + /* Add datapath as new JSON object using its name as key. */ + json_object_put(backers, dpif_name(backer->dpif), json_backer); + + /* Add datapath's stats under "stats" key. */ + struct dpif_dp_stats dp_stats; + struct json *json_dp_stats = json_object_create(); + + json_object_put(json_backer, "stats", json_dp_stats); + dpif_get_dp_stats(backer->dpif, &dp_stats); + json_object_put_format(json_dp_stats, "n_hit", "%"PRIu64, dp_stats.n_hit); + json_object_put_format(json_dp_stats, "n_missed", "%"PRIu64, + dp_stats.n_missed); + + /* Add datapath's bridges under "bridges" key. */ + struct json *json_dp_bridges = json_object_create(); + json_object_put(json_backer, "bridges", json_dp_bridges); + + struct shash ofproto_shash; + shash_init(&ofproto_shash); + const struct shash_node **ofprotos = get_ofprotos(&ofproto_shash); + for (size_t i = 0; i < shash_count(&ofproto_shash); i++) { + struct ofproto_dpif *ofproto = ofprotos[i]->data; + + if (ofproto->backer != backer) { + continue; + } + + struct json *json_ofproto = json_object_create(); + /* Add bridge to "bridges" dictionary using its name as key. */ + json_object_put(json_dp_bridges, ofproto->up.name, json_ofproto); + + /* Add bridge ports to the current bridge dictionary. */ + const struct shash_node **ports; + ports = shash_sort(&ofproto->up.port_by_name); + for (size_t j = 0; j < shash_count(&ofproto->up.port_by_name); j++) { + const struct shash_node *port = ports[j]; + struct ofport *ofport = port->data; + + struct json * json_ofproto_port = json_object_create(); + /* Add bridge port to a bridge's dict using port name as key. */ + json_object_put(json_ofproto, netdev_get_name(ofport->netdev), + json_ofproto_port); + /* Add OpenFlow port associated with a bridge port. */ + json_object_put_format(json_ofproto_port, "ofport", "%u", + ofport->ofp_port); + + /* Add bridge port number. */ + odp_port_t odp_port = ofp_port_to_odp_port(ofproto, + ofport->ofp_port); + if (odp_port != ODPP_NONE) { + json_object_put_format(json_ofproto_port, "port_no", + "%"PRIu32, odp_port); + } else { + json_object_put_string(json_ofproto_port, "port_no", "none"); + } + + /* Add type of a bridge port. */ + json_object_put_string(json_ofproto_port, "type", + netdev_get_type(ofport->netdev)); + + /* Add config entries for a bridge port. */ + struct json *json_port_config = json_object_create(); + struct smap config; + smap_init(&config); + + json_object_put(json_ofproto_port, "config", json_port_config); + if (!netdev_get_config(ofport->netdev, &config)) { + struct smap_node *node; + + SMAP_FOR_EACH (node, &config) { + json_object_put_string(json_port_config, node->key, + node->value); + } + } + smap_destroy(&config); + + } /* End of bridge port(s). */ + + free(ports); + } /* End of bridge(s). */ + shash_destroy(&ofproto_shash); + free(ofprotos); + + return json_backer; +} + static void -dpif_show_backer(const struct dpif_backer *backer, struct ds *ds) +dpif_show_backer_text(const struct dpif_backer *backer, struct ds *ds) { const struct shash_node **ofprotos; struct dpif_dp_stats dp_stats; @@ -6587,18 +6679,24 @@ static void ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED, const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) { - struct ds ds = DS_EMPTY_INITIALIZER; - const struct shash_node **backers; - int i; + if (unixctl_command_get_output_format(conn) == UNIXCTL_OUTPUT_FMT_JSON) { + struct json *backers = json_object_create(); + const struct shash_node *backer; + SHASH_FOR_EACH (backer, &all_dpif_backers) { + dpif_show_backer_json(backers, backer->data); + } + unixctl_command_reply_json(conn, backers); + } else { + struct ds ds = DS_EMPTY_INITIALIZER; + const struct shash_node **backers = shash_sort(&all_dpif_backers); + for (int i = 0; i < shash_count(&all_dpif_backers); i++) { + dpif_show_backer_text(backers[i]->data, &ds); + } + free(backers); - backers = shash_sort(&all_dpif_backers); - for (i = 0; i < shash_count(&all_dpif_backers); i++) { - dpif_show_backer(backers[i]->data, &ds); + unixctl_command_reply(conn, ds_cstr(&ds)); + ds_destroy(&ds); } - free(backers); - - unixctl_command_reply(conn, ds_cstr(&ds)); - ds_destroy(&ds); } static void diff --git a/tests/pmd.at b/tests/pmd.at index 35a44b4df..189138f81 100644 --- a/tests/pmd.at +++ b/tests/pmd.at @@ -112,6 +112,29 @@ dummy@ovs-dummy: hit:0 missed:0 p0 1/1: (dummy-pmd: n_rxq=1, n_txq=1, numa_id=0) ]) +AT_CHECK([ovs-appctl --format json --pretty dpif/show], [0], [dnl +[{ + "dummy@ovs-dummy": { + "bridges": { + "br0": { + "br0": { + "config": { + }, + "ofport": "65534", + "port_no": "100", + "type": "dummy-internal"}, + "p0": { + "config": { + "n_rxq": "1", + "n_txq": "1", + "numa_id": "0"}, + "ofport": "1", + "port_no": "1", + "type": "dummy-pmd"}}}, + "stats": { + "n_hit": "0", + "n_missed": "0"}}}]]) + OVS_VSWITCHD_STOP AT_CLEANUP