From patchwork Mon Oct 7 15:58:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandra Rukomoinikova X-Patchwork-Id: 1993727 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=b+ytEMt3; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) (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 4XMkVy4v6cz1xvJ for ; Tue, 8 Oct 2024 03:04:42 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id AEA0380EAE; Mon, 7 Oct 2024 16:04:40 +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 dZhnBIx-YLp7; Mon, 7 Oct 2024 16:04:39 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=140.211.9.56; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 391CD80DB2 Authentication-Results: smtp1.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=b+ytEMt3 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id 391CD80DB2; Mon, 7 Oct 2024 16:04:39 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 1D188C08A6; Mon, 7 Oct 2024 16:04:39 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 6A106C08A3 for ; Mon, 7 Oct 2024 16:04:37 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 5848E4094C for ; Mon, 7 Oct 2024 16:04:37 +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 yxFVNA8zr8Jo for ; Mon, 7 Oct 2024 16:04:36 +0000 (UTC) X-Greylist: delayed 339 seconds by postgrey-1.37 at util1.osuosl.org; Mon, 07 Oct 2024 16:04:34 UTC DMARC-Filter: OpenDMARC Filter v1.4.2 smtp2.osuosl.org 4C86B403AA Authentication-Results: smtp2.osuosl.org; dmarc=pass (p=none dis=none) header.from=k2.cloud DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 4C86B403AA Authentication-Results: smtp2.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=b+ytEMt3 Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=109.73.14.252; helo=mail1.k2.cloud; envelope-from=arukomoinikova@k2.cloud; receiver= Received: from mail1.k2.cloud (mail1.k2.cloud [109.73.14.252]) by smtp2.osuosl.org (Postfix) with ESMTPS id 4C86B403AA for ; Mon, 7 Oct 2024 16:04:33 +0000 (UTC) From: Alexandra Rukomoinikova DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=k2.cloud; s=cloudmail; t=1728316730; bh=MWV/E6KTIHoXq0q5z0Pmh3ANle/HECv3aIBSs6WYm0I=; h=From:To:Cc:Subject:Date; b=b+ytEMt3LJbM67xl+6+k6m2RKRcygt8UB4Tr/t/3LocMfUnCSyaauWrhvt9ip2h8r fMS2XdyUn1USG82/P1V15zt4p+oTcS5qfKppomVjj1DclZG5OqPZ+lOIA7RlGRdACk JdoD4aFs+WGce/8mpTbjiwP8+/gJUaKgu2D7D8IY= To: dev@openvswitch.org Cc: Alexandra Rukomoinikova , Ivan Burnin Date: Mon, 7 Oct 2024 18:58:21 +0300 Message-Id: <20241007155822.115125-1-arukomoinikova@k2.cloud> MIME-Version: 1.0 Subject: [ovs-dev] [PATCH ovn 1/2] utilities: Moved ovn-sbctl show command from OVS. 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" Transferred all logic of the show command from ovs/lib/db-ctl-base.c to ovn-sbctl.c. This is necessary in the future for the implementation show of separate chassis. These functions cannot be imported, due to the fact that in ovs/lib/db-ctl-base.c in these functions the global for the module structure cmd_show_tabeles is used, which is imported from ovn-sbctl, for filtering individual chassis I consider it necessary to take this block separately to ovn. Signed-off-by: Alexandra Rukomoinikova Tested-by: Ivan Burnin --- utilities/ovn-sbctl.c | 172 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 1 deletion(-) diff --git a/utilities/ovn-sbctl.c b/utilities/ovn-sbctl.c index f1f8c2b42..7a88b62a3 100644 --- a/utilities/ovn-sbctl.c +++ b/utilities/ovn-sbctl.c @@ -424,6 +424,174 @@ static struct cmd_show_table cmd_show_tables[] = { {NULL, NULL, {NULL, NULL, NULL}, {NULL, NULL, NULL}}, }; +static const struct cmd_show_table * +cmd_show_find_table_by_name(const char *name) +{ + const struct cmd_show_table *show; + + for (show = cmd_show_tables; show->table; show++) { + if (!strcmp(show->table->name, name)) { + return show; + } + } + return NULL; +} + +static const struct cmd_show_table * +cmd_show_find_table_by_row(const struct ovsdb_idl_row *row) +{ + const struct cmd_show_table *show; + + for (show = cmd_show_tables; show->table; show++) { + if (show->table == row->table->class_) { + return show; + } + } + return NULL; +} + +static void +cmd_show_weak_ref(struct ctl_context *ctx, const struct cmd_show_table *show, + const struct ovsdb_idl_row *cur_row, int level) +{ + const struct ovsdb_idl_row *row_wref; + const struct ovsdb_idl_table_class *table = show->wref_table.table; + const struct ovsdb_idl_column *name_column + = show->wref_table.name_column; + const struct ovsdb_idl_column *wref_column + = show->wref_table.wref_column; + + if (!table || !name_column || !wref_column) { + return; + } + + for (row_wref = ovsdb_idl_first_row(ctx->idl, table); row_wref; + row_wref = ovsdb_idl_next_row(row_wref)) { + const struct ovsdb_datum *wref_datum + = ovsdb_idl_read(row_wref, wref_column); + /* If weak reference refers to the 'cur_row', prints it. */ + if (wref_datum->n + && uuid_equals(&cur_row->uuid, &wref_datum->keys[0].uuid)) { + const struct ovsdb_datum *name_datum + = ovsdb_idl_read(row_wref, name_column); + ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4); + ds_put_format(&ctx->output, "%s ", table->name); + ovsdb_datum_to_string(name_datum, + &name_column->type, &ctx->output); + ds_put_char(&ctx->output, '\n'); + } + } +} + +static void +cmd_show_row(struct ctl_context *ctx, const struct ovsdb_idl_row *row, + int level, struct sset *shown) +{ + const struct cmd_show_table *show = cmd_show_find_table_by_row(row); + size_t i; + + ds_put_char_multiple(&ctx->output, ' ', level * 4); + if (show && show->name_column) { + const struct ovsdb_datum *datum; + + ds_put_format(&ctx->output, "%s ", show->table->name); + datum = ovsdb_idl_read(row, show->name_column); + ovsdb_datum_to_string(datum, &show->name_column->type, &ctx->output); + } else { + ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid)); + } + ds_put_char(&ctx->output, '\n'); + + if (!show || sset_find(shown, show->table->name)) { + return; + } + + sset_add(shown, show->table->name); + for (i = 0; i < ARRAY_SIZE(show->columns); i++) { + const struct ovsdb_idl_column *column = show->columns[i]; + const struct ovsdb_datum *datum; + + if (!column) { + break; + } + + datum = ovsdb_idl_read(row, column); + if (column->type.key.type == OVSDB_TYPE_UUID && + column->type.key.uuid.refTableName) { + const struct cmd_show_table *ref_show; + size_t j; + + ref_show = cmd_show_find_table_by_name( + column->type.key.uuid.refTableName); + if (ref_show) { + for (j = 0; j < datum->n; j++) { + const struct ovsdb_idl_row *ref_row; + + ref_row = ovsdb_idl_get_row_for_uuid(ctx->idl, + ref_show->table, + &datum->keys[j].uuid); + if (ref_row) { + cmd_show_row(ctx, ref_row, level + 1, shown); + } + } + continue; + } + } + if (!ovsdb_datum_is_default(datum, &column->type)) { + ds_put_char_multiple(&ctx->output, ' ', (level + 1) * 4); + ds_put_format(&ctx->output, "%s: ", column->name); + ovsdb_datum_to_string(datum, &column->type, &ctx->output); + ds_put_char(&ctx->output, '\n'); + } + } + cmd_show_weak_ref(ctx, show, row, level); + sset_find_and_delete_assert(shown, show->table->name); +} + +static void +cmd_show(struct ctl_context *ctx) +{ + const struct ovsdb_idl_row *row; + 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)) { + cmd_show_row(ctx, row, 0, &shown); + } + + ovs_assert(sset_is_empty(&shown)); + sset_destroy(&shown); +} + +static void +pre_cmd_show(struct ctl_context *ctx) +{ + const struct cmd_show_table *show; + for (show = cmd_show_tables; show->table; show++) { + size_t i; + + ovsdb_idl_add_table(ctx->idl, show->table); + if (show->name_column) { + ovsdb_idl_add_column(ctx->idl, show->name_column); + } + for (i = 0; i < ARRAY_SIZE(show->columns); i++) { + const struct ovsdb_idl_column *column = show->columns[i]; + if (column) { + ovsdb_idl_add_column(ctx->idl, column); + } + } + if (show->wref_table.table) { + ovsdb_idl_add_table(ctx->idl, show->wref_table.table); + } + if (show->wref_table.name_column) { + ovsdb_idl_add_column(ctx->idl, show->wref_table.name_column); + } + if (show->wref_table.wref_column) { + ovsdb_idl_add_column(ctx->idl, show->wref_table.wref_column); + } + } +} + static void sbctl_init(struct ctl_context *ctx OVS_UNUSED) { @@ -1554,6 +1722,8 @@ static const struct ctl_table_class tables[SBREC_N_TABLES] = { static const struct ctl_command_syntax sbctl_commands[] = { { "init", 0, 0, "", NULL, sbctl_init, NULL, "", RW }, + {"show", 0, 0, "", pre_cmd_show, cmd_show, NULL, "", RO}, + /* Chassis commands. */ {"chassis-add", 3, 3, "CHASSIS ENCAP-TYPE ENCAP-IP", pre_get_info, cmd_chassis_add, NULL, "--may-exist", RW}, @@ -1610,7 +1780,7 @@ main(int argc, char *argv[]) .idl_class = &sbrec_idl_class, .tables = tables, - .cmd_show_table = cmd_show_tables, + .cmd_show_table = NULL, .commands = sbctl_commands, .usage = sbctl_usage,