From patchwork Tue May 20 17:16:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rukomoinikova Aleksandra X-Patchwork-Id: 2088306 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=k2.cloud header.i=@k2.cloud header.a=rsa-sha256 header.s=cloudmail header.b=oQfN2/14; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.136; helo=smtp3.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136]) (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 4b21SH4KkFz1yDL for ; Wed, 21 May 2025 03:16:47 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 675666106B; Tue, 20 May 2025 17:17:01 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id vhi7Va-zYY4j; Tue, 20 May 2025 17:17:00 +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 smtp3.osuosl.org 3E43460A44 Authentication-Results: smtp3.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key, unprotected) header.d=k2.cloud header.i=@k2.cloud header.a=rsa-sha256 header.s=cloudmail header.b=oQfN2/14 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp3.osuosl.org (Postfix) with ESMTPS id 3E43460A44; Tue, 20 May 2025 17:17:00 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 07DE5C003A; Tue, 20 May 2025 17:17:00 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) by lists.linuxfoundation.org (Postfix) with ESMTP id D4289C0004 for ; Tue, 20 May 2025 17:16:58 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id CC3AE81BB2 for ; Tue, 20 May 2025 17:16:58 +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 qLbpku1bP_JI for ; Tue, 20 May 2025 17:16:58 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=109.73.14.254; helo=mail3.k2.cloud; envelope-from=arukomoinikova@k2.cloud; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp1.osuosl.org A486981069 Authentication-Results: smtp1.osuosl.org; dmarc=pass (p=none dis=none) header.from=k2.cloud DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org A486981069 Authentication-Results: smtp1.osuosl.org; dkim=pass (1024-bit key, unprotected) header.d=k2.cloud header.i=@k2.cloud header.a=rsa-sha256 header.s=cloudmail header.b=oQfN2/14 Received: from mail3.k2.cloud (mail3.k2.cloud [109.73.14.254]) by smtp1.osuosl.org (Postfix) with ESMTPS id A486981069 for ; Tue, 20 May 2025 17:16:57 +0000 (UTC) From: Alexandra Rukomoinikova DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=k2.cloud; s=cloudmail; t=1747761413; bh=KZDA3B427hXbj5biEO3dNeq711QivpTN7EoR7kLtfhQ=; h=From:To:Cc:Subject:Date; b=oQfN2/14DuLX8KkXtmmiV0Z+jU/FkORlqWwuf5tUJPfLOqoFSl5Uodg5Iq5CgXw6N 9expSc8yiu7lCgnTXbD7/1FwMtv3i0NbH1D4S/G329Mpxh9zdpVhMJDzYYAXQgpmmm o+cDghzkuzhXLlSP/rCN+iePqP6iimU7qYYrJJPY= To: dev@openvswitch.org Cc: Alexandra Rukomoinikova Date: Tue, 20 May 2025 20:16:48 +0300 Message-Id: <20250520171649.39875-1-arukomoinikova@k2.cloud> MIME-Version: 1.0 Subject: [ovs-dev] [PATCH v2] lib: Added filter option in show command. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" Added the ability to filter the output of the show command using the filter option. Signed-off-by: Alexandra Rukomoinikova --- v1 --> v2 : did as Ilya said. --- lib/db-ctl-base.c | 75 +++++++++++++++++++++++++++++++++++++++++++++- tests/ovs-vsctl.at | 28 +++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c index 1f157e46c..45d2d51c0 100644 --- a/lib/db-ctl-base.c +++ b/lib/db-ctl-base.c @@ -2243,19 +2243,92 @@ cmd_show_row(struct ctl_context *ctx, const struct ovsdb_idl_row *row, sset_find_and_delete_assert(shown, show->table->name); } +struct output_filter { + char **filters; + size_t n_filters; +}; + +static void +filter_row(struct ctl_context *ctx, + struct output_filter *filter, + size_t old_lenght) +{ + bool filter_match = false; + + for (size_t i = 0; i < filter->n_filters; i++) { + if (strstr(&ctx->output.string[old_lenght], filter->filters[i])) { + filter_match = true; + } + } + + if (!filter_match) { + ds_truncate(&ctx->output, old_lenght); + } +} + +static struct output_filter * +init_filters(struct ctl_context *ctx) +{ + char *filter_str = shash_find_data(&ctx->options, "--filter"); + if (!filter_str || !*filter_str) { + return NULL; + } + + size_t count = 1; + for (const char *p = filter_str; *p; p++) { + if (*p == '|') { + count++; + } + } + + struct output_filter *filter = xmalloc(sizeof(struct output_filter)); + filter->filters = xmalloc(count * (sizeof(char *))); + filter->n_filters = count; + + char *ptr = NULL; + char *token = strtok_r(filter_str, "|", &ptr); + size_t idx = 0; + + while (token && idx < count) { + filter->filters[idx++] = xstrdup(token); + token = strtok_r(NULL, "|", &ptr); + } + + return filter; +} + +static void +free_filters(struct output_filter *filter) +{ + if (!filter) { + return; + } + for (size_t i = 0; i < filter->n_filters; i++) { + free(filter->filters[i]); + } + free(filter->filters); + free(filter); +} + static void cmd_show(struct ctl_context *ctx) { const struct ovsdb_idl_row *row; + struct output_filter *filter = init_filters(ctx); struct sset shown = SSET_INITIALIZER(&shown); for (row = ovsdb_idl_first_row(ctx->idl, cmd_show_tables[0].table); row; row = ovsdb_idl_next_row(row)) { + size_t lenght_before = ctx->output.length; cmd_show_row(ctx, row, 0, &shown); + if (filter) { + filter_row(ctx, filter, lenght_before); + } } ovs_assert(sset_is_empty(&shown)); sset_destroy(&shown); + free_filters(filter); } @@ -2548,7 +2621,7 @@ ctl_init__(const struct ovsdb_idl_class *idl_class_, cmd_show_tables = cmd_show_tables_; if (cmd_show_tables) { static const struct ctl_command_syntax show = - {"show", 0, 0, "", pre_cmd_show, cmd_show, NULL, "", RO}; + {"show", 0, 0, "", pre_cmd_show, cmd_show, NULL, "--filter=", RO}; ctl_register_command(&show); } } diff --git a/tests/ovs-vsctl.at b/tests/ovs-vsctl.at index e488e292d..010889789 100644 --- a/tests/ovs-vsctl.at +++ b/tests/ovs-vsctl.at @@ -1831,3 +1831,31 @@ AT_CHECK([ovs-vsctl --no-wait --bare --columns _uuid,name list bridge tst1], [0] OVS_VSCTL_CLEANUP AT_CLEANUP + +AT_SETUP([ovs-vsctl filter option usage]) +AT_KEYWORDS([ovs-vsctl filter option]) + +OVS_VSWITCHD_START([dnl + add-port br0 p1 -- set Interface p1 type=internal ofport_request=1 -- \ + add-port br0 p2 -- set Interface p2 type=internal ofport_request=2 -- \ + add-port br0 p3 -- set Interface p3 type=internal ofport_request=3 +]) + +AT_CHECK([ovs-vsctl --filter='p1' show | grep Port | sort], [0], [dnl + Port br0 + Port p1 + Port p2 + Port p3 +]) + +AT_CHECK([ovs-vsctl --filter='test' show | grep Port | sort], [0], []) + +AT_CHECK([ovs-vsctl --filter='Bridge' show | grep Port | sort], [0], [dnl + Port br0 + Port p1 + Port p2 + Port p3 +]) + +OVS_VSCTL_CLEANUP +AT_CLEANUP \ No newline at end of file