From patchwork Fri Apr 12 07:26:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922881 X-Patchwork-Delegate: i.maximets@samsung.com 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=B1as/J8+; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::137; helo=smtp4.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp4.osuosl.org (smtp4.osuosl.org [IPv6:2605:bc80:3010::137]) (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 4VG7Rt6Ym2z1yYM for ; Fri, 12 Apr 2024 17:27:06 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 334E341FE1; Fri, 12 Apr 2024 07:27:05 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id IIXPNRu8MYv0; Fri, 12 Apr 2024 07:27:03 +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 smtp4.osuosl.org 6AF6841FDF Authentication-Results: smtp4.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=B1as/J8+ Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp4.osuosl.org (Postfix) with ESMTPS id 6AF6841FDF; Fri, 12 Apr 2024 07:27:03 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 0073DC0DD5; Fri, 12 Apr 2024 07:27:02 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id A382DC0077 for ; Fri, 12 Apr 2024 07:27:01 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 0B2B08261C for ; Fri, 12 Apr 2024 07:27:01 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id rEeQqUVt5QBy for ; Fri, 12 Apr 2024 07:26:57 +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 smtp1.osuosl.org 0E4B282532 Authentication-Results: smtp1.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 0E4B282532 Authentication-Results: smtp1.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=B1as/J8+ Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp1.osuosl.org (Postfix) with ESMTPS id 0E4B282532 for ; Fri, 12 Apr 2024 07:26:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712906814; 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=y4hMASHJtIQxCn5UniRnsjcT+BJ49KfhcbQloUU0xZE=; b=B1as/J8+lfO+fkv553yFWyHWSGmZsOJC58FzhsU2Zkhka77R63Iqf/pRALdDCReE7/QgsY Hub/PfuM7Qw+qUb1C1aoh6RUF9AR9id/IPAygHWoURieuBgiCS75bQEkn30br5wi+o9U0L ak+DYZgneAd9hhaO2FhVptYgGVzwVes= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-147-TJcPLsrBP8Gomx_CzW0txg-1; Fri, 12 Apr 2024 03:26:52 -0400 X-MC-Unique: TJcPLsrBP8Gomx_CzW0txg-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a51b00fc137so42180866b.0 for ; Fri, 12 Apr 2024 00:26:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712906811; x=1713511611; 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=y4hMASHJtIQxCn5UniRnsjcT+BJ49KfhcbQloUU0xZE=; b=mEpenY7yYV1dzguQYf/QJtRA653jUnqM22ATMZ/dQtIOSTVnB/6HbYYQ9VkmF/pxKm FSRkP2g3RQuBipJTrrSCZEKX+rxMDdPfOxMFYt8J52Adq+kUiDAMizKRrtN1OkDVFdP9 bLa8F1VwLBmR3lszj+pRoI/39riTVI5iRbqQz1Z6Qjz+3ddDIzruq8NVjiibYfwt08DW JLU6UbNfEWn2jiH1MHzPQC6CuGpC1+BWzqLEDV/otqkkeRfVxdB2Y5rOScrladcaitvK EYy8LyEZIqfPYm21uHIoVHbXYBnfoG9Uyl8ZFjwu9hj9YJgNiZxOPfrdqqWNNEh4eykE sdRQ== X-Gm-Message-State: AOJu0Yz1+QjzTTg7VvZe+Zdc/bBrGb/Ur4jU7M8eBGq83MvqxgWVtzHr 2IZ3SDowf+YN2bk6wSJQ2SQ1W9xWI9dQSDOyxJ9iokOHkhArbU1lGFl0XswsMFmIl4XibX6JJYL TJftg0sMw57C5/O+BztKjifKjb8EybF0IimR+8zomqrcaKw7SXP/HmUcsx3BgHHbzAOJz/EH9zu WLZLZogOEm55O29aDaGT4hvnj2jgFe X-Received: by 2002:a17:906:ee87:b0:a52:3d27:2040 with SMTP id wt7-20020a170906ee8700b00a523d272040mr154604ejb.38.1712906811725; Fri, 12 Apr 2024 00:26:51 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHQPPt67r+UXy/Zh9lrKHmKQ8fAeepPsFIT199gzFuJR+ug86XmhUB+xxQt7U6LOaYUGoS7NQ== X-Received: by 2002:a17:906:ee87:b0:a52:3d27:2040 with SMTP id wt7-20020a170906ee8700b00a523d272040mr154582ejb.38.1712906811325; Fri, 12 Apr 2024 00:26:51 -0700 (PDT) Received: from positronik4lide.. ([87.122.58.150]) by smtp.gmail.com with ESMTPSA id t2-20020a1709064f0200b00a51dd500071sm1502373eju.169.2024.04.12.00.26.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 00:26:51 -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: Fri, 12 Apr 2024 09:26:38 +0200 Message-Id: <20240412072638.712622-7-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240412072638.712622-1-jmeng@redhat.com> References: <20240412072638.712622-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v9 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 | 124 +++++++++++++++++++++++++++++++++++++---- tests/pmd.at | 28 ++++++++++ 3 files changed, 142 insertions(+), 11 deletions(-) diff --git a/NEWS b/NEWS index af83623b3..97b30233c 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,7 @@ Post-v3.3.0 E.g. members of objects and elements of arrays are printed one per line, with indentation. * 'list-commands' now supports output in JSON format. + * 'dpif/show' now supports output in JSON format. - Python: * Added support for choosing the output format, e.g. 'json' or 'text'. * Added new option [--pretty] to print JSON output in a readable fashion. diff --git a/ofproto/ofproto-dpif.c b/ofproto/ofproto-dpif.c index 32d037be6..67e1df550 100644 --- a/ofproto/ofproto-dpif.c +++ b/ofproto/ofproto-dpif.c @@ -6519,8 +6519,104 @@ done: return changed; } +static struct json * +dpif_show_backer_json(const struct dpif_backer *backer) +{ + struct json *json_backer = json_object_create(); + + /* Add dpif name to JSON. */ + json_object_put_string(json_backer, "name", dpif_name(backer->dpif)); + + /* Add dpif stats to JSON. */ + struct dpif_dp_stats dp_stats; + struct json *json_dp_stats = json_object_create(); + + 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); + json_object_put(json_backer, "stats", json_dp_stats); + + /* Add ofprotos to JSON. */ + struct json *json_ofprotos = json_array_create_empty(); + 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 ofproto name to JSON array. */ + json_object_put_string(json_ofproto, "name", ofproto->up.name); + + /* Add ofproto ports to JSON array. */ + struct json *json_ofproto_ports = json_array_create_empty(); + 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 ofproto port netdev name to JSON sub array. */ + json_object_put_string(json_ofproto_port, "netdev_name", + netdev_get_name(ofport->netdev)); + /* Add ofproto port ofp port to JSON sub array. */ + json_object_put_format(json_ofproto_port, "ofp_port", "%u", + ofport->ofp_port); + + /* Add ofproto port odp port to JSON sub array. */ + 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, "odp_port", + "%"PRIu32, odp_port); + } else { + json_object_put_string(json_ofproto_port, "odp_port", "none"); + } + + /* Add ofproto port netdev type to JSON sub array. */ + json_object_put_string(json_ofproto_port, "netdev_type", + netdev_get_type(ofport->netdev)); + + /* Add ofproto port config to JSON sub array. */ + struct json *json_port_config = json_object_create(); + struct smap config; + smap_init(&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); + json_object_put(json_ofproto_port, "netdev_config", + json_port_config); + + json_array_add(json_ofproto_ports, json_ofproto_port); + } /* End of ofproto port(s). */ + + free(ports); + json_object_put(json_ofproto, "ports", json_ofproto_ports); + + json_array_add(json_ofprotos, json_ofproto); + } /* End of ofproto(s). */ + shash_destroy(&ofproto_shash); + free(ofprotos); + + json_object_put(json_backer, "ofprotos", json_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 +6683,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) == OVS_OUTPUT_FMT_JSON) { + struct json *backers = json_array_create_empty(); + const struct shash_node *backer; + SHASH_FOR_EACH (backer, &all_dpif_backers) { + json_array_add(backers, dpif_show_backer_json(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..cff80da15 100644 --- a/tests/pmd.at +++ b/tests/pmd.at @@ -112,6 +112,34 @@ 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 +[[ + { + "name": "dummy@ovs-dummy", + "ofprotos": [ + { + "name": "br0", + "ports": [ + { + "netdev_config": { + }, + "netdev_name": "br0", + "netdev_type": "dummy-internal", + "odp_port": "100", + "ofp_port": "65534"}, + { + "netdev_config": { + "n_rxq": "1", + "n_txq": "1", + "numa_id": "0"}, + "netdev_name": "p0", + "netdev_type": "dummy-pmd", + "odp_port": "1", + "ofp_port": "1"}]}], + "stats": { + "n_hit": "0", + "n_missed": "0"}}]]]) + OVS_VSWITCHD_STOP AT_CLEANUP