From patchwork Fri Apr 12 07:26:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922880 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=at377iDX; 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 4VG7Rp4swRz1yYX for ; Fri, 12 Apr 2024 17:27:02 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id A583841FCC; Fri, 12 Apr 2024 07:27:00 +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 xdXnQ5mZbHQ5; Fri, 12 Apr 2024 07:26:58 +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 smtp4.osuosl.org 095D941FB1 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=at377iDX Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp4.osuosl.org (Postfix) with ESMTPS id 095D941FB1; Fri, 12 Apr 2024 07:26:58 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id E8349C0077; Fri, 12 Apr 2024 07:26:57 +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 758D7C0037 for ; Fri, 12 Apr 2024 07:26:56 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 6297682542 for ; Fri, 12 Apr 2024 07:26:56 +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 7ed7wQCH1k6E for ; Fri, 12 Apr 2024 07:26:52 +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 F1B34823E0 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 F1B34823E0 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=at377iDX 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 F1B34823E0 for ; Fri, 12 Apr 2024 07:26:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712906810; 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=YT0FrrAJ72xUqigZPyv6BtfXQuc3VdPhscHoX3CEEkQ=; b=at377iDXh3X8RqGPCMXbBI38mn+IL1F6pp9stwPAZB0sKbz1EOi+o9CscSqzU13aFWWGIp ejzO+zafx0h13q1xpNoo4NzyXMntrT20SE2Ee4gJcJO9rh6BD35ucPjQ/j15/bNucrH6o5 4g54U+KuG7v0DprpmrxkuEMylaY57rs= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-507-3xZ1CLLANlSaIfgaRLTzAw-1; Fri, 12 Apr 2024 03:26:49 -0400 X-MC-Unique: 3xZ1CLLANlSaIfgaRLTzAw-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a519e10623bso40200866b.0 for ; Fri, 12 Apr 2024 00:26:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712906808; x=1713511608; 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=YT0FrrAJ72xUqigZPyv6BtfXQuc3VdPhscHoX3CEEkQ=; b=WsT/vNacNZmIocR5vgZ7TPsc6lGfBR7rXyopDoMdFb3HETHRMTkZuKrWXgo2hBWlIh Hxn6ZOsQ1s4n9WhRIvHHm1mW12LOckKsQ6FOjSV2bcYml+tjVtawG/D57+gN4bHnksFq rGXAMEDgGlM40wIy3qeQHkUfrVb3lzTQ4XXCqwqJALceyjGYiGBjrYbz45Ddtgxl4FIo bFS0Ua72p2Nk7AOhiZ/akF0U3xsegy/L5JoKlQCaHA9Kpw/OcwniyNSmN84WjAItEbuj AilYU4k+ut/9FPy4t5zRzhfSe87uoExif31+wpnFXVpZtJtpA2FcWYWRydD6pJilwSKq t2Wg== X-Gm-Message-State: AOJu0YwY+tRQaspG44vr33H0Dt538Zw2k6G7rr95SF6INxsqkW52QrZL aTI6bfxBQDN2ZkE2sW8oszs8QUVhkkOyPNzR6uUtHyoGnM4/S/yQYweZH0rnYwO9dETmdeFz+Wy EspRm4img8vFWshP3b/qD4zm8m3PkSWQJd4LEuGsfDD3nYM8dsU5kWEsslJs2NzQf9CnURrhheh bVCj9gtcZ7kjDmDY1eI5dR0MtIWj7N X-Received: by 2002:a17:906:4086:b0:a51:cab2:e55d with SMTP id u6-20020a170906408600b00a51cab2e55dmr1246438ejj.15.1712906807902; Fri, 12 Apr 2024 00:26:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFuY2OwJJ05C3Kg2rUrL+1wa9faSJXVzGJ9jCTWKyRAURoli2YW/7L6Fms4g+Xmd5NmYDi8FA== X-Received: by 2002:a17:906:4086:b0:a51:cab2:e55d with SMTP id u6-20020a170906408600b00a51cab2e55dmr1246410ejj.15.1712906807313; Fri, 12 Apr 2024 00:26:47 -0700 (PDT) Received: from positronik4lide.. ([87.122.58.150]) by smtp.gmail.com with ESMTPSA id t2-20020a1709064f0200b00a51dd500071sm1502373eju.169.2024.04.12.00.26.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 00:26:47 -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:33 +0200 Message-Id: <20240412072638.712622-2-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 1/6] Add global option for JSON output to ovs-appctl. 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 For monitoring systems such as Prometheus it would be beneficial if OVS would expose statistics in a machine-readable format. This patch introduces support for different output formats to ovs-xxx tools, in particular ovs-appctl. The latter gains a global option '-f,--format' which changes it to print a JSON document instead of plain-text for humans. For example, a later patch implements support for 'ovs-appctl --format json dpif/show'. By default, the output format is plain-text as before. A new 'set-options' command has been added to lib/unixctl.c which allows to change the output format of the commands executed afterwards on the same socket connection. It is supposed to be run by ovs-xxx tools transparently for the user when a specific output format has been requested. For example, when a user calls 'ovs-appctl --format json dpif/show', then ovs-appctl will call 'set-options' to set the output format as requested by the user and afterwards it will call the actual command 'dpif/show'. This ovs-appctl behaviour has been implemented in a backward compatible way. One can use an updated client (ovs-appctl) with an old server (ovs-vswitchd) and vice versa. Of course, JSON output only works when both sides have been updated. Two access functions unixctl_command_{get,set}_output_format() and two unixctl_command_reply_{,error_}json functions have been added to lib/unixctl.h: unixctl_command_get_output_format() is supposed to be used in commands like 'dpif/show' to query the requested output format. When JSON output has been selected, both unixctl_command_reply_{,error_}json() functions can be used to return JSON objects to the client (ovs-appctl) instead of plain-text with the unixctl_command_reply{,_error}() functions. When JSON has been requested but a command has not implemented JSON output the plain-text output will be wrapped in a provisional JSON document with the following structure: {"reply-format":"plain","reply":"$PLAIN_TEXT_HERE"} Thus commands which have been executed successfully will not fail when they try to render the output at a later stage. In popular tools like kubectl the option for output control is usually called '-o|--output' instead of '-f,--format'. But ovs-appctl already has an short option '-o' which prints the available ovs-appctl options ('--option'). The now choosen name also better alignes with ovsdb-client where '-f,--format' controls output formatting. Reported-at: https://bugzilla.redhat.com/1824861 Signed-off-by: Jakob Meng --- NEWS | 3 + lib/command-line.c | 36 ++++++++++ lib/command-line.h | 10 +++ lib/unixctl.c | 158 ++++++++++++++++++++++++++++++++--------- lib/unixctl.h | 11 +++ tests/ovs-vswitchd.at | 11 +++ utilities/ovs-appctl.c | 98 +++++++++++++++++++++---- 7 files changed, 277 insertions(+), 50 deletions(-) diff --git a/NEWS b/NEWS index b92cec532..03ef6486b 100644 --- a/NEWS +++ b/NEWS @@ -7,6 +7,9 @@ Post-v3.3.0 - The primary development branch has been renamed from 'master' to 'main'. The OVS tree remains hosted on GitHub. https://github.com/openvswitch/ovs.git + - ovs-appctl: + * Added new option [-f|--format] to choose the output format, e.g. 'json' + or 'text' (by default). v3.3.0 - 16 Feb 2024 diff --git a/lib/command-line.c b/lib/command-line.c index 967f4f5d5..775e0430a 100644 --- a/lib/command-line.c +++ b/lib/command-line.c @@ -24,6 +24,7 @@ #include "ovs-thread.h" #include "util.h" #include "openvswitch/vlog.h" +#include "openvswitch/json.h" VLOG_DEFINE_THIS_MODULE(command_line); @@ -53,6 +54,41 @@ ovs_cmdl_long_options_to_short_options(const struct option options[]) return xstrdup(short_options); } +const char * +ovs_output_fmt_to_string(enum ovs_output_fmt fmt) +{ + switch (fmt) { + case OVS_OUTPUT_FMT_TEXT: + return "text"; + + case OVS_OUTPUT_FMT_JSON: + return "json"; + + default: + return NULL; + } +} + +struct json * +ovs_output_fmt_to_json(enum ovs_output_fmt fmt) +{ + const char *string = ovs_output_fmt_to_string(fmt); + return string ? json_string_create(string) : NULL; +} + +bool +ovs_output_fmt_from_string(const char *string, enum ovs_output_fmt *fmt) +{ + if (!strcmp(string, "text")) { + *fmt = OVS_OUTPUT_FMT_TEXT; + } else if (!strcmp(string, "json")) { + *fmt = OVS_OUTPUT_FMT_JSON; + } else { + return false; + } + return true; +} + static char * OVS_WARN_UNUSED_RESULT build_short_options(const struct option *long_options) { diff --git a/lib/command-line.h b/lib/command-line.h index fc2a2690f..494274cec 100644 --- a/lib/command-line.h +++ b/lib/command-line.h @@ -20,6 +20,7 @@ /* Utilities for command-line parsing. */ #include "compiler.h" +#include struct option; @@ -46,6 +47,15 @@ struct ovs_cmdl_command { char *ovs_cmdl_long_options_to_short_options(const struct option *options); +enum ovs_output_fmt { + OVS_OUTPUT_FMT_TEXT = 1 << 0, + OVS_OUTPUT_FMT_JSON = 1 << 1 +}; + +const char *ovs_output_fmt_to_string(enum ovs_output_fmt); +struct json *ovs_output_fmt_to_json(enum ovs_output_fmt); +bool ovs_output_fmt_from_string(const char *, enum ovs_output_fmt *); + struct ovs_cmdl_parsed_option { const struct option *o; char *arg; diff --git a/lib/unixctl.c b/lib/unixctl.c index 103357ee9..18638d86a 100644 --- a/lib/unixctl.c +++ b/lib/unixctl.c @@ -21,7 +21,6 @@ #include "coverage.h" #include "dirs.h" #include "openvswitch/dynamic-string.h" -#include "openvswitch/json.h" #include "jsonrpc.h" #include "openvswitch/list.h" #include "openvswitch/poll-loop.h" @@ -50,6 +49,8 @@ struct unixctl_conn { /* Only one request can be in progress at a time. While the request is * being processed, 'request_id' is populated, otherwise it is null. */ struct json *request_id; /* ID of the currently active request. */ + + enum ovs_output_fmt fmt; }; /* Server for control connection. */ @@ -94,6 +95,39 @@ unixctl_version(struct unixctl_conn *conn, int argc OVS_UNUSED, unixctl_command_reply(conn, ovs_get_program_version()); } +static void +unixctl_set_options(struct unixctl_conn *conn, int argc, const char *argv[], + void *aux OVS_UNUSED) +{ + char * error = NULL; + + for (size_t i = 1; i < argc; i++) { + if ((i + 1) == argc) { + error = xasprintf("option requires an argument -- %s", argv[i]); + goto error; + } else if (!strcmp(argv[i], "--format")) { + /* Move index to option argument. */ + i++; + + /* Parse output format argument. */ + if (!ovs_output_fmt_from_string(argv[i], &conn->fmt)) { + error = xasprintf("option %s has invalid value %s", + argv[i - 1], argv[i]); + goto error; + } + } else { + error = xasprintf("invalid option -- %s", argv[i]); + goto error; + + } + } + unixctl_command_reply(conn, NULL); + return; +error: + unixctl_command_reply_error(conn, error); + free(error); +} + /* Registers a unixctl command with the given 'name'. 'usage' describes the * arguments to the command; it is used only for presentation to the user in * "list-commands" output. (If 'usage' is NULL, then the command is hidden.) @@ -128,36 +162,68 @@ unixctl_command_register(const char *name, const char *usage, shash_add(&commands, name, command); } -static void -unixctl_command_reply__(struct unixctl_conn *conn, - bool success, const char *body) +enum ovs_output_fmt +unixctl_command_get_output_format(struct unixctl_conn *conn) { - struct json *body_json; - struct jsonrpc_msg *reply; + return conn->fmt; +} - COVERAGE_INC(unixctl_replied); - ovs_assert(conn->request_id); +void +unixctl_command_set_output_format(struct unixctl_conn *conn, + enum ovs_output_fmt fmt) +{ + conn->fmt = fmt; +} + +static struct json * +json_reply_create__(struct unixctl_conn *conn, const char *body) +{ + struct json * json_body; if (!body) { body = ""; } if (body[0] && body[strlen(body) - 1] != '\n') { - body_json = json_string_create_nocopy(xasprintf("%s\n", body)); + json_body = json_string_create_nocopy(xasprintf("%s\n", body)); } else { - body_json = json_string_create(body); + json_body = json_string_create(body); } + if (conn->fmt == OVS_OUTPUT_FMT_TEXT) { + return json_body; + } + + struct json *json_reply = json_object_create(); + + json_object_put_string(json_reply, "reply-format", "plain"); + json_object_put(json_reply, "reply", json_body); + + return json_reply; +} + +/* Takes ownership of 'body'. */ +static void +unixctl_command_reply__(struct unixctl_conn *conn, + bool success, struct json *body) +{ + struct jsonrpc_msg *reply; + + COVERAGE_INC(unixctl_replied); + ovs_assert(conn->request_id); + if (success) { - reply = jsonrpc_create_reply(body_json, conn->request_id); + reply = jsonrpc_create_reply(body, conn->request_id); } else { - reply = jsonrpc_create_error(body_json, conn->request_id); + reply = jsonrpc_create_error(body, conn->request_id); } if (VLOG_IS_DBG_ENABLED()) { char *id = json_to_string(conn->request_id, 0); + char *msg = json_to_string(body, 0); VLOG_DBG("replying with %s, id=%s: \"%s\"", - success ? "success" : "error", id, body); + success ? "success" : "error", id, msg); + free(msg); free(id); } @@ -170,20 +236,40 @@ unixctl_command_reply__(struct unixctl_conn *conn, /* Replies to the active unixctl connection 'conn'. 'result' is sent to the * client indicating the command was processed successfully. Only one call to - * unixctl_command_reply() or unixctl_command_reply_error() may be made per - * request. */ + * the unixctl_command_reply*() functions may be made per request. */ void unixctl_command_reply(struct unixctl_conn *conn, const char *result) { - unixctl_command_reply__(conn, true, result); + unixctl_command_reply__(conn, true, json_reply_create__(conn, result)); +} + +/* Replies to the active unixctl connection 'conn'. 'result' is sent to the + * client indicating the command was processed successfully. Only one call to + * the unixctl_command_reply*() functions may be made per request. + * + * Takes ownership of 'body'. */ +void +unixctl_command_reply_json(struct unixctl_conn *conn, struct json *body) +{ + unixctl_command_reply__(conn, true, body); } /* Replies to the active unixctl connection 'conn'. 'error' is sent to the - * client indicating an error occurred processing the command. Only one call to - * unixctl_command_reply() or unixctl_command_reply_error() may be made per - * request. */ + * client indicating an error occurred processing the command. Only one call + * to the unixctl_command_reply*() functions may be made per request. */ void unixctl_command_reply_error(struct unixctl_conn *conn, const char *error) +{ + unixctl_command_reply__(conn, false, json_reply_create__(conn, error)); +} + +/* Replies to the active unixctl connection 'conn'. 'error' is sent to the + * client indicating an error occurred processing the command. Only one call + * to the unixctl_command_reply*() functions may be made per request. + * + * Takes ownership of 'error'. */ +void +unixctl_command_reply_error_json(struct unixctl_conn *conn, struct json *error) { unixctl_command_reply__(conn, false, error); } @@ -250,6 +336,8 @@ unixctl_server_create(const char *path, struct unixctl_server **serverp) unixctl_command_register("list-commands", "", 0, 0, unixctl_list_commands, NULL); unixctl_command_register("version", "", 0, 0, unixctl_version, NULL); + unixctl_command_register("set-options", "[--format text|json]", 2, 2, + unixctl_set_options, NULL); struct unixctl_server *server = xmalloc(sizeof *server); server->listener = listener; @@ -381,6 +469,7 @@ unixctl_server_run(struct unixctl_server *server) struct unixctl_conn *conn = xzalloc(sizeof *conn); ovs_list_push_back(&server->conns, &conn->node); conn->rpc = jsonrpc_open(stream); + conn->fmt = OVS_OUTPUT_FMT_TEXT; } else if (error == EAGAIN) { break; } else { @@ -483,10 +572,12 @@ unixctl_client_create(const char *path, struct jsonrpc **client) * '*err' if not NULL. */ int unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, - char *argv[], char **result, char **err) + char *argv[], enum ovs_output_fmt fmt, char **result, + char **err) { struct jsonrpc_msg *request, *reply; - struct json **json_args, *params; + struct json **json_args, *params, *output_src; + char **output_dst; int error, i; *result = NULL; @@ -506,22 +597,19 @@ unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, return error; } - if (reply->error) { - if (reply->error->type == JSON_STRING) { - *err = xstrdup(json_string(reply->error)); + output_src = reply->error ? reply->error : reply->result; + output_dst = reply->error ? err : result; + + if (output_src) { + if (fmt == OVS_OUTPUT_FMT_TEXT && output_src->type == JSON_STRING) { + *output_dst = xstrdup(json_string(output_src)); + } else if (fmt == OVS_OUTPUT_FMT_JSON) { + *output_dst = json_to_string(output_src, 0); } else { - VLOG_WARN("%s: unexpected error type in JSON RPC reply: %s", + VLOG_WARN("%s: unexpected %s type in JSON rpc reply: %s", jsonrpc_get_name(client), - json_type_to_string(reply->error->type)); - error = EINVAL; - } - } else if (reply->result) { - if (reply->result->type == JSON_STRING) { - *result = xstrdup(json_string(reply->result)); - } else { - VLOG_WARN("%s: unexpected result type in JSON rpc reply: %s", - jsonrpc_get_name(client), - json_type_to_string(reply->result->type)); + reply->error ? "error" : "result", + json_type_to_string(output_src->type)); error = EINVAL; } } diff --git a/lib/unixctl.h b/lib/unixctl.h index 4562dbc49..b30bcf092 100644 --- a/lib/unixctl.h +++ b/lib/unixctl.h @@ -17,6 +17,9 @@ #ifndef UNIXCTL_H #define UNIXCTL_H 1 +#include "openvswitch/json.h" +#include "command-line.h" + #ifdef __cplusplus extern "C" { #endif @@ -36,6 +39,7 @@ int unixctl_client_create(const char *path, struct jsonrpc **client); int unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, char *argv[], + enum ovs_output_fmt fmt, char **result, char **error); /* Command registration. */ @@ -45,8 +49,15 @@ typedef void unixctl_cb_func(struct unixctl_conn *, void unixctl_command_register(const char *name, const char *usage, int min_args, int max_args, unixctl_cb_func *cb, void *aux); +enum ovs_output_fmt unixctl_command_get_output_format(struct unixctl_conn *); +void unixctl_command_set_output_format(struct unixctl_conn *, + enum ovs_output_fmt); void unixctl_command_reply_error(struct unixctl_conn *, const char *error); +void unixctl_command_reply_error_json(struct unixctl_conn *, + struct json *error); void unixctl_command_reply(struct unixctl_conn *, const char *body); +void unixctl_command_reply_json(struct unixctl_conn *, + struct json *body); #ifdef __cplusplus } diff --git a/tests/ovs-vswitchd.at b/tests/ovs-vswitchd.at index 977b2eba1..32ca2c6e7 100644 --- a/tests/ovs-vswitchd.at +++ b/tests/ovs-vswitchd.at @@ -265,3 +265,14 @@ OFPT_FEATURES_REPLY: dpid:$orig_dpid OVS_VSWITCHD_STOP AT_CLEANUP + +AT_SETUP([ovs-vswitchd version]) +OVS_VSWITCHD_START + +AT_CHECK([ovs-appctl version], [0], [ignore]) +ovs_version=$(ovs-appctl version) + +AT_CHECK_UNQUOTED([ovs-appctl --format json version], [0], [dnl +{"reply-format":"plain","reply":"$ovs_version\n"}]) + +AT_CLEANUP diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c index ba0c172e6..4d4503597 100644 --- a/utilities/ovs-appctl.c +++ b/utilities/ovs-appctl.c @@ -29,44 +29,84 @@ #include "jsonrpc.h" #include "process.h" #include "timeval.h" +#include "svec.h" #include "unixctl.h" #include "util.h" #include "openvswitch/vlog.h" static void usage(void); -static const char *parse_command_line(int argc, char *argv[]); + +/* Parsed command line args. */ +struct cmdl_args { + enum ovs_output_fmt format; + char *target; +}; + +static struct cmdl_args *cmdl_args_create(void); +static void cmdl_args_destroy(struct cmdl_args *); +static struct cmdl_args *parse_command_line(int argc, char *argv[]); static struct jsonrpc *connect_to_target(const char *target); int main(int argc, char *argv[]) { + struct svec opt_argv = SVEC_EMPTY_INITIALIZER; char *cmd_result, *cmd_error; struct jsonrpc *client; + struct cmdl_args *args; char *cmd, **cmd_argv; - const char *target; int cmd_argc; int error; set_program_name(argv[0]); /* Parse command line and connect to target. */ - target = parse_command_line(argc, argv); - client = connect_to_target(target); + args = parse_command_line(argc, argv); + client = connect_to_target(args->target); - /* Transact request and process reply. */ + /* Transact options request (if required) and process reply */ + if (args->format != OVS_OUTPUT_FMT_TEXT) { + svec_add(&opt_argv, "--format"); + svec_add(&opt_argv, ovs_output_fmt_to_string(args->format)); + } + svec_terminate(&opt_argv); + + if (!svec_is_empty(&opt_argv)) { + error = unixctl_client_transact(client, "set-options", + opt_argv.n, opt_argv.names, + args->format, &cmd_result, + &cmd_error); + + if (error) { + ovs_fatal(error, "%s: transaction error", args->target); + } + + if (cmd_error) { + jsonrpc_close(client); + fputs(cmd_error, stderr); + ovs_error(0, "%s: server returned an error", args->target); + exit(2); + } + + free(cmd_result); + free(cmd_error); + } + svec_destroy(&opt_argv); + + /* Transact command request and process reply. */ cmd = argv[optind++]; cmd_argc = argc - optind; cmd_argv = cmd_argc ? argv + optind : NULL; error = unixctl_client_transact(client, cmd, cmd_argc, cmd_argv, - &cmd_result, &cmd_error); + args->format, &cmd_result, &cmd_error); if (error) { - ovs_fatal(error, "%s: transaction error", target); + ovs_fatal(error, "%s: transaction error", args->target); } if (cmd_error) { jsonrpc_close(client); fputs(cmd_error, stderr); - ovs_error(0, "%s: server returned an error", target); + ovs_error(0, "%s: server returned an error", args->target); exit(2); } else if (cmd_result) { fputs(cmd_result, stdout); @@ -74,6 +114,7 @@ main(int argc, char *argv[]) OVS_NOT_REACHED(); } + cmdl_args_destroy(args); jsonrpc_close(client); free(cmd_result); free(cmd_error); @@ -101,13 +142,31 @@ Common commands:\n\ vlog/reopen Make the program reopen its log file\n\ Other options:\n\ --timeout=SECS wait at most SECS seconds for a response\n\ + -f, --format=FMT Output format. One of: 'json', or 'text'\n\ + ('text', by default)\n\ -h, --help Print this helpful information\n\ -V, --version Display ovs-appctl version information\n", program_name, program_name); exit(EXIT_SUCCESS); } -static const char * +static struct cmdl_args * +cmdl_args_create(void) { + struct cmdl_args *args = xmalloc(sizeof *args); + + args->format = OVS_OUTPUT_FMT_TEXT; + args->target = NULL; + + return args; +} + +static void +cmdl_args_destroy(struct cmdl_args *args) { + free(args->target); + free(args); +} + +static struct cmdl_args * parse_command_line(int argc, char *argv[]) { enum { @@ -117,6 +176,7 @@ parse_command_line(int argc, char *argv[]) static const struct option long_options[] = { {"target", required_argument, NULL, 't'}, {"execute", no_argument, NULL, 'e'}, + {"format", required_argument, NULL, 'f'}, {"help", no_argument, NULL, 'h'}, {"option", no_argument, NULL, 'o'}, {"version", no_argument, NULL, 'V'}, @@ -126,11 +186,10 @@ parse_command_line(int argc, char *argv[]) }; char *short_options_ = ovs_cmdl_long_options_to_short_options(long_options); char *short_options = xasprintf("+%s", short_options_); - const char *target; - int e_options; + struct cmdl_args *args = cmdl_args_create(); unsigned int timeout = 0; + int e_options; - target = NULL; e_options = 0; for (;;) { int option; @@ -141,10 +200,10 @@ parse_command_line(int argc, char *argv[]) } switch (option) { case 't': - if (target) { + if (args->target) { ovs_fatal(0, "-t or --target may be specified only once"); } - target = optarg; + args->target = xstrdup(optarg); break; case 'e': @@ -157,6 +216,12 @@ parse_command_line(int argc, char *argv[]) } break; + case 'f': + if (!ovs_output_fmt_from_string(optarg, &args->format)) { + ovs_fatal(0, "value %s on -f or --format is invalid", optarg); + } + break; + case 'h': usage(); break; @@ -194,7 +259,10 @@ parse_command_line(int argc, char *argv[]) "(use --help for help)"); } - return target ? target : "ovs-vswitchd"; + if (!args->target) { + args->target = xstrdup("ovs-vswitchd"); + } + return args; } static struct jsonrpc * From patchwork Fri Apr 12 07:26:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922883 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=iSnQJe8e; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::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 4VG7Ry5DsTz1yYM for ; Fri, 12 Apr 2024 17:27:10 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id E4D8641C43; Fri, 12 Apr 2024 07:27:07 +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 suLvVpoMUA3J; Fri, 12 Apr 2024 07:27:04 +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 A2CD741C10 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=iSnQJe8e Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp2.osuosl.org (Postfix) with ESMTPS id A2CD741C10; Fri, 12 Apr 2024 07:27:02 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 1C72DC0DD6; Fri, 12 Apr 2024 07:27:02 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [IPv6:2605:bc80:3010::137]) by lists.linuxfoundation.org (Postfix) with ESMTP id 32457C0DDA for ; Fri, 12 Apr 2024 07:27:01 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id EE11F41FC6 for ; Fri, 12 Apr 2024 07:27:00 +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 5d3s7md2K6as for ; Fri, 12 Apr 2024 07:26:56 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=170.10.133.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=jmeng@redhat.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp4.osuosl.org C7DA941FAE Authentication-Results: smtp4.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org C7DA941FAE Authentication-Results: smtp4.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=iSnQJe8e Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp4.osuosl.org (Postfix) with ESMTPS id C7DA941FAE 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=1712906813; 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=afoTsioWd9nuBVbXkU1NH96NwWqgUy5DymlTooVhfKM=; b=iSnQJe8eXVR90Y2D75Hk7wT3LJSldAjM3m1kWGjVZxQev6yTLcVAVJxAPFI2YGWlZVvWsj UZ/rbpMhVk12hQFDo15vVX/cQUx+lH9a5FIJZOsI/ETSQgd7p30jMYB6MTDbOhKB1PJ/GY J7OhQ1bafxzBrxYVI9dpfNYO9SVECDI= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-550-5dBiEZMNMw-JvT6Cenc7yA-1; Fri, 12 Apr 2024 03:26:50 -0400 X-MC-Unique: 5dBiEZMNMw-JvT6Cenc7yA-1 Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-2d86787efa3so4515901fa.1 for ; Fri, 12 Apr 2024 00:26:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712906808; x=1713511608; 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=afoTsioWd9nuBVbXkU1NH96NwWqgUy5DymlTooVhfKM=; b=gDqeoNd/WGMp5/QzbUMx619XGJf//Up6XLiJlA4Tnk9kxPu94XCujYi/luDPj1SLsZ TeVJVJX5wahQWMaAE8OQ1ipVY+7mkCN8NoX3FZAVYU6Z1hqhAVrdPgfhrDhuk8Oyb6bs fmZ548Ncc4tzitp1gcS6tcOEMoIVBQjFY80zfLc+HkLYhY6ljq2OgtcybvgQ33lOzuse Nicv0PXy4ERTz2tJe53OMApCDvpWftvBBFm29tbFhDOZNcvYcRKWWSiUU1sDPISs2onP d62TTGNcAtdQhyw1j9dc8j0zHdy4t/VCPibt+k9150PrH+6HX5aqXmK6ZxSkSzVQsnAd YirA== X-Gm-Message-State: AOJu0YzXz0M6rY4h0q2Na4MeU1NKdHSax1/YUL2u9QDNEGnPsMdcGK02 WIgyBqzYX7G4I/1qMWiDQIFo+L63NWI2ClS8/M71Bg3Qs8JtLvPjgqe29RnRvN7mUSvW+4069Ix AQ9ayxlHPy59mmyMAn7M/kaHLAjjJysKzK8MtV00Kq8Gklvo1GPhu9Nld1sm1IoIXyBMvAU8Ybj BVTMqDDbvL3ka+dc6/enxl3JDjos0l X-Received: by 2002:a2e:9c93:0:b0:2d6:fa7a:a670 with SMTP id x19-20020a2e9c93000000b002d6fa7aa670mr1283114lji.33.1712906808567; Fri, 12 Apr 2024 00:26:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGWUkzpnuZb7iLnzmiqvLVt6q+crn7c8Yimj60zhsPDHVLkfK3NQTjGh82qKHJgNQB2Aw6D7g== X-Received: by 2002:a2e:9c93:0:b0:2d6:fa7a:a670 with SMTP id x19-20020a2e9c93000000b002d6fa7aa670mr1283090lji.33.1712906808103; Fri, 12 Apr 2024 00:26:48 -0700 (PDT) Received: from positronik4lide.. ([87.122.58.150]) by smtp.gmail.com with ESMTPSA id t2-20020a1709064f0200b00a51dd500071sm1502373eju.169.2024.04.12.00.26.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 00:26:47 -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:34 +0200 Message-Id: <20240412072638.712622-3-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 2/6] python: Add global option for JSON output to Python tools. 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 This patch introduces support for different output formats to the Python code, as did the previous commit for ovs-xxx tools like 'ovs-appctl --format json dpif/show'. In particular, tests/appctl.py gains a global option '-f,--format' which allows users to request JSON instead of plain-text for humans. Reported-at: https://bugzilla.redhat.com/1824861 Signed-off-by: Jakob Meng --- NEWS | 2 ++ python/ovs/unixctl/client.py | 14 ++++++++--- python/ovs/unixctl/server.py | 45 +++++++++++++++++++++++++++++++++--- python/ovs/util.py | 8 +++++++ tests/appctl.py | 19 ++++++++++++++- tests/unixctl-py.at | 3 +++ 6 files changed, 84 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 03ef6486b..f18238159 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ Post-v3.3.0 - ovs-appctl: * Added new option [-f|--format] to choose the output format, e.g. 'json' or 'text' (by default). + - Python: + * Added support for choosing the output format, e.g. 'json' or 'text'. v3.3.0 - 16 Feb 2024 diff --git a/python/ovs/unixctl/client.py b/python/ovs/unixctl/client.py index 8283f99bb..000d261c0 100644 --- a/python/ovs/unixctl/client.py +++ b/python/ovs/unixctl/client.py @@ -14,6 +14,7 @@ import os +import ovs.json import ovs.jsonrpc import ovs.stream import ovs.util @@ -26,11 +27,12 @@ class UnixctlClient(object): assert isinstance(conn, ovs.jsonrpc.Connection) self._conn = conn - def transact(self, command, argv): + def transact(self, command, argv, fmt): assert isinstance(command, str) assert isinstance(argv, list) for arg in argv: assert isinstance(arg, str) + assert isinstance(fmt, ovs.util.OutputFormat) request = ovs.jsonrpc.Message.create_request(command, argv) error, reply = self._conn.transact_block(request) @@ -40,11 +42,17 @@ class UnixctlClient(object): % (self._conn.name, os.strerror(error))) return error, None, None + def to_string(body): + if fmt == ovs.util.OutputFormat.TEXT: + return str(body) + else: + return ovs.json.to_string(body) + if reply.error is not None: - return 0, str(reply.error), None + return 0, to_string(reply.error), None else: assert reply.result is not None - return 0, None, str(reply.result) + return 0, None, to_string(reply.result) def close(self): self._conn.close() diff --git a/python/ovs/unixctl/server.py b/python/ovs/unixctl/server.py index b9cb52fad..f95317c4a 100644 --- a/python/ovs/unixctl/server.py +++ b/python/ovs/unixctl/server.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import argparse import copy import errno import os @@ -35,6 +36,7 @@ class UnixctlConnection(object): assert isinstance(rpc, ovs.jsonrpc.Connection) self._rpc = rpc self._request_id = None + self._fmt = ovs.util.OutputFormat.TEXT def run(self): self._rpc.run() @@ -65,9 +67,15 @@ class UnixctlConnection(object): def reply(self, body): self._reply_impl(True, body) + def reply_json(self, body): + self._reply_impl_json(True, body) + def reply_error(self, body): self._reply_impl(False, body) + def reply_error_json(self, body): + self._reply_impl_json(False, body) + # Called only by unixctl classes. def _close(self): self._rpc.close() @@ -79,17 +87,27 @@ class UnixctlConnection(object): self._rpc.recv_wait(poller) def _reply_impl(self, success, body): - assert isinstance(success, bool) assert body is None or isinstance(body, str) - assert self._request_id is not None - if body is None: body = "" if body and not body.endswith("\n"): body += "\n" + if self._fmt == ovs.util.OutputFormat.JSON: + body = { + "reply-format": "plain", + "reply": body + } + + return self._reply_impl_json(success, body) + + def _reply_impl_json(self, success, body): + assert isinstance(success, bool) + + assert self._request_id is not None + if success: reply = Message.create_reply(body, self._request_id) else: @@ -136,6 +154,24 @@ def _unixctl_version(conn, unused_argv, version): conn.reply(version) +def _unixctl_set_options(conn, argv, unused_aux): + assert isinstance(conn, UnixctlConnection) + + parser = argparse.ArgumentParser() + parser.add_argument("--format", default="text", + choices=[fmt.name.lower() + for fmt in ovs.util.OutputFormat]) + + try: + args = parser.parse_args(args=argv) + except argparse.ArgumentError as e: + conn.reply_error(str(e)) + return + + conn._fmt = ovs.util.OutputFormat[args.format.upper()] + conn.reply(None) + + class UnixctlServer(object): def __init__(self, listener): assert isinstance(listener, ovs.stream.PassiveStream) @@ -210,4 +246,7 @@ class UnixctlServer(object): ovs.unixctl.command_register("version", "", 0, 0, _unixctl_version, version) + ovs.unixctl.command_register("set-options", "[--format text|json]", 2, + 2, _unixctl_set_options, None) + return 0, UnixctlServer(listener) diff --git a/python/ovs/util.py b/python/ovs/util.py index 3dba022f8..272ca683d 100644 --- a/python/ovs/util.py +++ b/python/ovs/util.py @@ -15,11 +15,19 @@ import os import os.path import sys +import enum PROGRAM_NAME = os.path.basename(sys.argv[0]) EOF = -1 +@enum.unique +# FIXME: Use @enum.verify(enum.NAMED_FLAGS) from Python 3.11 when available. +class OutputFormat(enum.IntFlag): + TEXT = 1 << 0 + JSON = 1 << 1 + + def abs_file_name(dir_, file_name): """If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise, returns an absolute path to 'file_name' considering it relative diff --git a/tests/appctl.py b/tests/appctl.py index b85b364fa..7ccf34cc3 100644 --- a/tests/appctl.py +++ b/tests/appctl.py @@ -49,14 +49,31 @@ def main(): help="Arguments to the command.") parser.add_argument("-T", "--timeout", metavar="SECS", help="wait at most SECS seconds for a response") + parser.add_argument("-f", "--format", metavar="FMT", + help="Output format.", default="text", + choices=[fmt.name.lower() + for fmt in ovs.util.OutputFormat]) args = parser.parse_args() signal_alarm(int(args.timeout) if args.timeout else None) ovs.vlog.Vlog.init() target = args.target + format = ovs.util.OutputFormat[args.format.upper()] client = connect_to_target(target) - err_no, error, result = client.transact(args.command, args.argv) + + if format != ovs.util.OutputFormat.TEXT: + err_no, error, _ = client.transact( + "set-options", ["--format", args.format], format) + + if err_no: + ovs.util.ovs_fatal(err_no, "%s: transaction error" % target) + elif error is not None: + sys.stderr.write(error) + ovs.util.ovs_error(0, "%s: server returned an error" % target) + sys.exit(2) + + err_no, error, result = client.transact(args.command, args.argv, format) client.close() if err_no: diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at index 724006118..1440b30b7 100644 --- a/tests/unixctl-py.at +++ b/tests/unixctl-py.at @@ -100,6 +100,7 @@ The available commands are: exit help log [[arg ...]] + set-options [[--format text|json]] version vlog/close vlog/list @@ -112,6 +113,8 @@ AT_CHECK([PYAPPCTL_PY -t test-unixctl.py help], [0], [expout]) AT_CHECK([ovs-vsctl --version | sed 's/ovs-vsctl/test-unixctl.py/' | head -1 > expout]) AT_CHECK([APPCTL -t test-unixctl.py version], [0], [expout]) AT_CHECK([PYAPPCTL_PY -t test-unixctl.py version], [0], [expout]) +AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format json version], [0], [dnl +{"reply":"$(cat expout)\n","reply-format":"plain"}]) AT_CHECK([APPCTL -t test-unixctl.py echo robot ninja], [0], [stdout]) AT_CHECK([cat stdout | sed -e "s/u'/'/g"], [0], [dnl From patchwork Fri Apr 12 07:26:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922882 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=DIZWmWlj; 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 4VG7Rv1DSxz1yYX for ; Fri, 12 Apr 2024 17:27:06 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 51A84826DE; Fri, 12 Apr 2024 07:27:04 +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 zpts-zaN_TNR; Fri, 12 Apr 2024 07:27:00 +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 2A5C7825FE Authentication-Results: smtp1.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=DIZWmWlj Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id 2A5C7825FE; Fri, 12 Apr 2024 07:27:00 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id CFD16C0DCF; Fri, 12 Apr 2024 07:26:59 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by lists.linuxfoundation.org (Postfix) with ESMTP id ED6BAC0DD4 for ; Fri, 12 Apr 2024 07:26:58 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id D67DF41FC0 for ; Fri, 12 Apr 2024 07:26:58 +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 x-nC4J0vbwMW for ; Fri, 12 Apr 2024 07:26:54 +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 smtp4.osuosl.org 6FBAB41FAC Authentication-Results: smtp4.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org 6FBAB41FAC Authentication-Results: smtp4.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DIZWmWlj Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp4.osuosl.org (Postfix) with ESMTPS id 6FBAB41FAC 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=1712906813; 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=7DDsL+MAgdF9rRltIhoSyy7bNZBxiaem0V4ZyFXLO/o=; b=DIZWmWljzIgyZqMeQr3dybrbh5bJtyiq7Euz00/r80gGEck2Oh+ovwi/eMROOILfBBLVQ3 EUJeuAxmg7/e6JrjsyRbUQ2H2eT6FbtEF4NlVvcL4nFyweLWfGhgX230xXQPoyKTe4MaL9 tDVWues78rlZ1acASZcQM6teDC6DG/A= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-207--M1LRMb1OHGnb3Wm5sSmBQ-1; Fri, 12 Apr 2024 03:26:50 -0400 X-MC-Unique: -M1LRMb1OHGnb3Wm5sSmBQ-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a5190b1453fso34349766b.2 for ; Fri, 12 Apr 2024 00:26:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712906809; x=1713511609; 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=7DDsL+MAgdF9rRltIhoSyy7bNZBxiaem0V4ZyFXLO/o=; b=rSptQY5037/TtJ5tSjjJXp1GRNgKj/W9vnRj5clKvzCLORilAn+YWA2EpgzgCIQuKq WgEVm7mYh0h1M3LGr99Lel3ZTaY53WrXHw+fM4EcqZAzEVdtuk5VmIxCWQQWqOhnkHMO m51EEnCiW8St5l7PLyQAc8Wkorm+emMFmuTGO1ZpOT/V8+P91J0BY6FC5t4Y94jtzArE +kJuEppjq8YL8xsu9h2lU+UYmiQzZjoxF39Jaev9fAqlXkseGks/fK1dVmMofrCzHThI Hx0rTrCK1QGm5pdUtfeOJw/nKWLDeuFfi/i5xdM/EGOQ7eOt3GJsKZpDdu2A4U5UaHyJ 54bw== X-Gm-Message-State: AOJu0YzJOPgA0j70/xcN/g4arImP9aCykeTFFkinnDksdKzAkmDg9Zrm xzBmOfZm/qZJFLrAbTidzinhrT1FueORKVBKOVIZumNOU5wGhbsak3c6/sB/YMDuKCH0ejHGAnl gZKpJKARsE4+dVYaP0UbwtOK/ucY42RySidl1wPh2edtRXFmVzpY9FU6STCt4YZLmej5vW0r90e IOCU766p61fLOWT7IKgP62phL7NZZv X-Received: by 2002:a17:906:b12:b0:a52:243f:5bb7 with SMTP id u18-20020a1709060b1200b00a52243f5bb7mr859227ejg.32.1712906809427; Fri, 12 Apr 2024 00:26:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEnXi7wTmi9SP30OUq0/aWiKdYxV3WFzXmmAPvdwNO0J1kPOumuGPyIkoqWBCHuzniOm3fKpw== X-Received: by 2002:a17:906:b12:b0:a52:243f:5bb7 with SMTP id u18-20020a1709060b1200b00a52243f5bb7mr859211ejg.32.1712906809109; Fri, 12 Apr 2024 00:26:49 -0700 (PDT) Received: from positronik4lide.. ([87.122.58.150]) by smtp.gmail.com with ESMTPSA id t2-20020a1709064f0200b00a51dd500071sm1502373eju.169.2024.04.12.00.26.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 00:26:48 -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:35 +0200 Message-Id: <20240412072638.712622-4-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 3/6] appctl: Add option '--pretty' for pretty-printing JSON output. 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 With the '--pretty' option, ovs-appctl will now print JSON output in a more readable fashion, i.e. with additional line breaks, spaces and sorted dictionary keys. Signed-off-by: Jakob Meng --- NEWS | 3 +++ lib/unixctl.c | 6 +++--- lib/unixctl.h | 2 +- tests/ovs-vswitchd.at | 5 +++++ utilities/ovs-appctl.c | 32 ++++++++++++++++++++++++++++---- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index f18238159..52cadbe0d 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,9 @@ Post-v3.3.0 - ovs-appctl: * Added new option [-f|--format] to choose the output format, e.g. 'json' or 'text' (by default). + * Added new option [--pretty] to print JSON output in a readable fashion. + E.g. members of objects and elements of arrays are printed one per line, + with indentation. - Python: * Added support for choosing the output format, e.g. 'json' or 'text'. diff --git a/lib/unixctl.c b/lib/unixctl.c index 18638d86a..67cf26418 100644 --- a/lib/unixctl.c +++ b/lib/unixctl.c @@ -572,8 +572,8 @@ unixctl_client_create(const char *path, struct jsonrpc **client) * '*err' if not NULL. */ int unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, - char *argv[], enum ovs_output_fmt fmt, char **result, - char **err) + char *argv[], enum ovs_output_fmt fmt, + unsigned int fmt_flags, char **result, char **err) { struct jsonrpc_msg *request, *reply; struct json **json_args, *params, *output_src; @@ -604,7 +604,7 @@ unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, if (fmt == OVS_OUTPUT_FMT_TEXT && output_src->type == JSON_STRING) { *output_dst = xstrdup(json_string(output_src)); } else if (fmt == OVS_OUTPUT_FMT_JSON) { - *output_dst = json_to_string(output_src, 0); + *output_dst = json_to_string(output_src, fmt_flags); } else { VLOG_WARN("%s: unexpected %s type in JSON rpc reply: %s", jsonrpc_get_name(client), diff --git a/lib/unixctl.h b/lib/unixctl.h index b30bcf092..5a08a1bd1 100644 --- a/lib/unixctl.h +++ b/lib/unixctl.h @@ -39,7 +39,7 @@ int unixctl_client_create(const char *path, struct jsonrpc **client); int unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, char *argv[], - enum ovs_output_fmt fmt, + enum ovs_output_fmt fmt, unsigned int fmt_flags, char **result, char **error); /* Command registration. */ diff --git a/tests/ovs-vswitchd.at b/tests/ovs-vswitchd.at index 32ca2c6e7..5683896ef 100644 --- a/tests/ovs-vswitchd.at +++ b/tests/ovs-vswitchd.at @@ -275,4 +275,9 @@ ovs_version=$(ovs-appctl version) AT_CHECK_UNQUOTED([ovs-appctl --format json version], [0], [dnl {"reply-format":"plain","reply":"$ovs_version\n"}]) +AT_CHECK_UNQUOTED([ovs-appctl --format json --pretty version], [0], [dnl +{ + "reply": "$ovs_version\n", + "reply-format": "plain"}]) + AT_CLEANUP diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c index 4d4503597..29a0de2ee 100644 --- a/utilities/ovs-appctl.c +++ b/utilities/ovs-appctl.c @@ -26,6 +26,7 @@ #include "daemon.h" #include "dirs.h" #include "openvswitch/dynamic-string.h" +#include "openvswitch/json.h" #include "jsonrpc.h" #include "process.h" #include "timeval.h" @@ -39,6 +40,7 @@ static void usage(void); /* Parsed command line args. */ struct cmdl_args { enum ovs_output_fmt format; + unsigned int format_flags; char *target; }; @@ -74,8 +76,8 @@ main(int argc, char *argv[]) if (!svec_is_empty(&opt_argv)) { error = unixctl_client_transact(client, "set-options", opt_argv.n, opt_argv.names, - args->format, &cmd_result, - &cmd_error); + args->format, 0, + &cmd_result, &cmd_error); if (error) { ovs_fatal(error, "%s: transaction error", args->target); @@ -98,7 +100,9 @@ main(int argc, char *argv[]) cmd_argc = argc - optind; cmd_argv = cmd_argc ? argv + optind : NULL; error = unixctl_client_transact(client, cmd, cmd_argc, cmd_argv, - args->format, &cmd_result, &cmd_error); + args->format, args->format_flags, + &cmd_result, &cmd_error); + if (error) { ovs_fatal(error, "%s: transaction error", args->target); } @@ -144,6 +148,11 @@ Other options:\n\ --timeout=SECS wait at most SECS seconds for a response\n\ -f, --format=FMT Output format. One of: 'json', or 'text'\n\ ('text', by default)\n\ + --pretty By default, JSON in output is printed as compactly as\n\ + possible. This option causes JSON in output to be\n\ + printed in a more readable fashion. Members of objects\n\ + and elements of arrays are printed one per line, with\n\ + indentation.\n\ -h, --help Print this helpful information\n\ -V, --version Display ovs-appctl version information\n", program_name, program_name); @@ -155,6 +164,7 @@ cmdl_args_create(void) { struct cmdl_args *args = xmalloc(sizeof *args); args->format = OVS_OUTPUT_FMT_TEXT; + args->format_flags = 0; args->target = NULL; return args; @@ -171,7 +181,8 @@ parse_command_line(int argc, char *argv[]) { enum { OPT_START = UCHAR_MAX + 1, - VLOG_OPTION_ENUMS + OPT_PRETTY, + VLOG_OPTION_ENUMS, }; static const struct option long_options[] = { {"target", required_argument, NULL, 't'}, @@ -179,6 +190,7 @@ parse_command_line(int argc, char *argv[]) {"format", required_argument, NULL, 'f'}, {"help", no_argument, NULL, 'h'}, {"option", no_argument, NULL, 'o'}, + {"pretty", no_argument, NULL, OPT_PRETTY}, {"version", no_argument, NULL, 'V'}, {"timeout", required_argument, NULL, 'T'}, VLOG_LONG_OPTIONS, @@ -188,6 +200,7 @@ parse_command_line(int argc, char *argv[]) char *short_options = xasprintf("+%s", short_options_); struct cmdl_args *args = cmdl_args_create(); unsigned int timeout = 0; + bool pretty = false; int e_options; e_options = 0; @@ -230,6 +243,10 @@ parse_command_line(int argc, char *argv[]) ovs_cmdl_print_options(long_options); exit(EXIT_SUCCESS); + case OPT_PRETTY: + pretty = true; + break; + case 'T': if (!str_to_uint(optarg, 10, &timeout) || !timeout) { ovs_fatal(0, "value %s on -T or --timeout is invalid", optarg); @@ -259,6 +276,13 @@ parse_command_line(int argc, char *argv[]) "(use --help for help)"); } + if (pretty) { + if (args->format != OVS_OUTPUT_FMT_JSON) { + ovs_fatal(0, "--pretty is supported with --format json only"); + } + args->format_flags |= JSSF_PRETTY | JSSF_SORT; + } + if (!args->target) { args->target = xstrdup("ovs-vswitchd"); } From patchwork Fri Apr 12 07:26:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922885 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=iF81OcS7; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::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 4VG7S32sTWz1yYM for ; Fri, 12 Apr 2024 17:27:15 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id BE7D141C4C; Fri, 12 Apr 2024 07:27:13 +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 FC2GJN_53EIz; Fri, 12 Apr 2024 07:27:09 +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 2FDAC41C38 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=iF81OcS7 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp2.osuosl.org (Postfix) with ESMTPS id 2FDAC41C38; Fri, 12 Apr 2024 07:27:07 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id DBE15C0077; Fri, 12 Apr 2024 07:27:06 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by lists.linuxfoundation.org (Postfix) with ESMTP id 2BBD3C0DD8 for ; Fri, 12 Apr 2024 07:27:03 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 5588941FCE for ; Fri, 12 Apr 2024 07:27:01 +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 lvGokozb81lV for ; Fri, 12 Apr 2024 07:26:59 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=170.10.133.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=jmeng@redhat.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp4.osuosl.org B3C2541FBF Authentication-Results: smtp4.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org B3C2541FBF Authentication-Results: smtp4.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=iF81OcS7 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp4.osuosl.org (Postfix) with ESMTPS id B3C2541FBF for ; Fri, 12 Apr 2024 07:26:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712906817; 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=7nsugrdzAlDy3iJVLkyS+0u7LnlpiyKpVIX1pRnete0=; b=iF81OcS7CmydWP9grWgvXr67yiEnRG0YjFj2hd1QlW7Z8yNzmBk3sO8i3OdIalRS7ze4y4 FFrNd/wIY+zatwtXLiiRAnqL/AeUDSKWLqq+IIekSEyLhIhNRzcZ3VrturzCpT9DAiyijs oLz9kxfMZpVQaaE3pqDb+fFlasChl7U= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-414-rww2GrzZMSeoFcqbmN2TGA-1; Fri, 12 Apr 2024 03:26:51 -0400 X-MC-Unique: rww2GrzZMSeoFcqbmN2TGA-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a51beadf204so31942366b.1 for ; Fri, 12 Apr 2024 00:26:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712906810; x=1713511610; 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=7nsugrdzAlDy3iJVLkyS+0u7LnlpiyKpVIX1pRnete0=; b=K/9NPNbKORBzX8Gwcja+vTzTN6afCRySwOyMoVzlyjiHqLjAu80c7H4JCZra1iXeZh RtKjnjlynffEObxSaEnczIViSI+9yzNjQDnswfUcnvhYBP7tBFFrcJ06+ELirrI2YZY+ eNAUeYc2jz8Weq5Ot4mKNS2Fw/6BU4DXVQ1Un1BI0g2oNXkD7iNhKU6dUD/WzQ74YZd+ ckNenNxAlt0lVqZKBEBUUxvVkEwvSWv8czexbdRaRKK8RuAkvyJ4SR071oAOHs0zWUD+ z0wPc31OpOt1wrGle+MaM1azIBcsmdv5yvmwawLu/T8CHMUtDzoEDkCWUngZbMEyVqym 72ag== X-Gm-Message-State: AOJu0YytBQWcO+Ff+y63D+lERwzyaWQjs/19+Jz2FtQM6Iu0m6uo8ahb GmMwyQbt5i12MfYvYywcd/NeeyZjqhmrHeFQyqCc6tgN3CzGaQhVU79xNG9jhvqr10NGk2Nq+qi OFwU8cs7u1xHAQl+Ox9gKS9Pkt6ZwcDucTvjxdAx21HSVgxzmnjP4l+gVAby+IY5/XrTNxQzzgq ev8U0bqkadaDslLfDPHVuCInVC9HaV X-Received: by 2002:a17:907:6d1a:b0:a52:39a9:a412 with SMTP id sa26-20020a1709076d1a00b00a5239a9a412mr526878ejc.53.1712906810118; Fri, 12 Apr 2024 00:26:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGQtbC1q8+NkXwrgUiz78Lz0xrHZkvsyaruaQsLyKAmP1EYytpoSE5M9dd6m6t12MR2unGuEA== X-Received: by 2002:a17:907:6d1a:b0:a52:39a9:a412 with SMTP id sa26-20020a1709076d1a00b00a5239a9a412mr526858ejc.53.1712906809833; Fri, 12 Apr 2024 00:26:49 -0700 (PDT) Received: from positronik4lide.. ([87.122.58.150]) by smtp.gmail.com with ESMTPSA id t2-20020a1709064f0200b00a51dd500071sm1502373eju.169.2024.04.12.00.26.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 00:26:49 -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:36 +0200 Message-Id: <20240412072638.712622-5-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 4/6] python: Add option '--pretty' for pretty-printing JSON output. 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 With the '--pretty' option, appctl.py will now print JSON output in a more readable fashion, i.e. with additional line breaks, spaces and sorted dictionary keys. The pretty-printed output from appctl.py is not strictly the same as with ovs-appctl because of both use different pretty-printing implementations. Signed-off-by: Jakob Meng --- NEWS | 3 +++ python/ovs/unixctl/client.py | 4 ++-- tests/appctl.py | 16 ++++++++++++++-- tests/unixctl-py.at | 5 +++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 52cadbe0d..957351acb 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,9 @@ Post-v3.3.0 with indentation. - 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. + E.g. members of objects and elements of arrays are printed one per line, + with indentation. v3.3.0 - 16 Feb 2024 diff --git a/python/ovs/unixctl/client.py b/python/ovs/unixctl/client.py index 000d261c0..460c79e88 100644 --- a/python/ovs/unixctl/client.py +++ b/python/ovs/unixctl/client.py @@ -27,7 +27,7 @@ class UnixctlClient(object): assert isinstance(conn, ovs.jsonrpc.Connection) self._conn = conn - def transact(self, command, argv, fmt): + def transact(self, command, argv, fmt, fmt_flags): assert isinstance(command, str) assert isinstance(argv, list) for arg in argv: @@ -46,7 +46,7 @@ class UnixctlClient(object): if fmt == ovs.util.OutputFormat.TEXT: return str(body) else: - return ovs.json.to_string(body) + return ovs.json.to_string(body, **fmt_flags) if reply.error is not None: return 0, to_string(reply.error), None diff --git a/tests/appctl.py b/tests/appctl.py index 7ccf34cc3..75816231f 100644 --- a/tests/appctl.py +++ b/tests/appctl.py @@ -53,18 +53,29 @@ def main(): help="Output format.", default="text", choices=[fmt.name.lower() for fmt in ovs.util.OutputFormat]) + parser.add_argument("--pretty", action="store_true", + help="By default, JSON in output is printed as" + " compactly as possible. This option causes JSON" + " in output to be printed in a more readable" + " fashion. Members of objects and elements of" + " arrays are printed one per line, with" + " indentation.") args = parser.parse_args() + if args.format != ovs.util.OutputFormat.JSON.name.lower() and args.pretty: + ovs.util.ovs_fatal(0, "--pretty is supported with --format json only") + signal_alarm(int(args.timeout) if args.timeout else None) ovs.vlog.Vlog.init() target = args.target format = ovs.util.OutputFormat[args.format.upper()] + format_flags = dict(pretty=True, sort_keys=True) if args.pretty else {} client = connect_to_target(target) if format != ovs.util.OutputFormat.TEXT: err_no, error, _ = client.transact( - "set-options", ["--format", args.format], format) + "set-options", ["--format", args.format], format, format_flags) if err_no: ovs.util.ovs_fatal(err_no, "%s: transaction error" % target) @@ -73,7 +84,8 @@ def main(): ovs.util.ovs_error(0, "%s: server returned an error" % target) sys.exit(2) - err_no, error, result = client.transact(args.command, args.argv, format) + err_no, error, result = client.transact( + args.command, args.argv, format, format_flags) client.close() if err_no: diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at index 1440b30b7..bcea19abc 100644 --- a/tests/unixctl-py.at +++ b/tests/unixctl-py.at @@ -115,6 +115,11 @@ AT_CHECK([APPCTL -t test-unixctl.py version], [0], [expout]) AT_CHECK([PYAPPCTL_PY -t test-unixctl.py version], [0], [expout]) AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format json version], [0], [dnl {"reply":"$(cat expout)\n","reply-format":"plain"}]) +AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format json --pretty version], [0], [dnl +{ + "reply":"$(cat expout)\n", + "reply-format":"plain" +}]) AT_CHECK([APPCTL -t test-unixctl.py echo robot ninja], [0], [stdout]) AT_CHECK([cat stdout | sed -e "s/u'/'/g"], [0], [dnl From patchwork Fri Apr 12 07:26:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922884 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=DtVmO2I1; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.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 4VG7S069Qpz1yYM for ; Fri, 12 Apr 2024 17:27:12 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 96B36827CA; Fri, 12 Apr 2024 07:27:10 +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 JbtZ_3qFWEui; Fri, 12 Apr 2024 07:27:09 +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 smtp1.osuosl.org 8FDF7826F5 Authentication-Results: smtp1.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=DtVmO2I1 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTPS id 8FDF7826F5; Fri, 12 Apr 2024 07:27:04 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 0B34EC0DDA; Fri, 12 Apr 2024 07:27:04 +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 27093C0DD7 for ; Fri, 12 Apr 2024 07:27:02 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 473F782661 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 6v-OhqA2hDgW 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 CDF1E82531 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 CDF1E82531 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 CDF1E82531 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=1712906813; 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=FUX3JrAfmZIoXwguuOlggZIbHx2+QSjUz0pv3E5vtf4=; b=DtVmO2I1hhRHe8rPhHc4vQjyO0Yk2CajM+9S7zPnFarWesX6SUaHLdhm20x8COtS+3O+rL WYVmxtW5VIfGzrtyvN8NDyC14K6rCUg43PhIi8NWmFFJdNuLS5sy2540Z/OZj6E/My8AlX WJfPWq0cpFddU0Of0fqs9RU5QJyVXfw= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-81-HuDCHIyVNMmDTrKd2v8MkA-1; Fri, 12 Apr 2024 03:26:52 -0400 X-MC-Unique: HuDCHIyVNMmDTrKd2v8MkA-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a52213a0d50so21618166b.2 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=FUX3JrAfmZIoXwguuOlggZIbHx2+QSjUz0pv3E5vtf4=; b=dtsdwsEMJxo21qoNKZlJAwQyEU/3smMvbxbdVNlNSJT9EzwmQzdhxY+GzT2wRJ78Oe u2GFwSODWrqkvNzY3be7NJkMq2z7CPIsMcT2eqBmYFhRvielFzFl2XtGvzoRO8yKf6/m X1JShKohHPSuXmxtaBBZWBilaqtsnH+dzsiGoz5XUKDm2vYA2aQ6qeg7w1yMij0/oLVb sYxb2dZnx4b6MkHMM2C9TKvULsE2EN+FXC64AUV+KRETzldnUqFPoenWeJ4NyrQVPyL5 Opp1+RsyAcdMkkrLo2zKwKK+XD4j4Lczo2w0NdMj3Diaiq/Ckb5LAQNFT9fzMqRjCDTj Fztw== X-Gm-Message-State: AOJu0YwNB9te/YHuLakY8LeHh0+7g2XcAkxIUhYJmy92wULlnZD6F3So exqMMtSYmhTRp7apTcJ1lr3EseOg4DbQ0Sa4xH7t/cgD0PbrDlbUjuURV7YFHGWKX3jJjGRJ6B4 uXUwrMfIkP4GPsNJMuxIMxW+pG++XAImoGX1jVha5cmVhzCUxALrJcQYfo7Wr+wBJ2mZgmMkGpo Utj6GzJwx6G8ZeMAW4SdlXoaFjcbmZ X-Received: by 2002:a17:907:2d93:b0:a52:274e:4f63 with SMTP id gt19-20020a1709072d9300b00a52274e4f63mr1398793ejc.7.1712906810933; Fri, 12 Apr 2024 00:26:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFn7lCMm3CFUEQfFHmv6GuD/A/jIxvyYmPRMx1hbCW7V1ktxy0cS90kbNVFtkLMjZvH/UIIhw== X-Received: by 2002:a17:907:2d93:b0:a52:274e:4f63 with SMTP id gt19-20020a1709072d9300b00a52274e4f63mr1398774ejc.7.1712906810572; Fri, 12 Apr 2024 00:26:50 -0700 (PDT) Received: from positronik4lide.. ([87.122.58.150]) by smtp.gmail.com with ESMTPSA id t2-20020a1709064f0200b00a51dd500071sm1502373eju.169.2024.04.12.00.26.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 00:26:50 -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:37 +0200 Message-Id: <20240412072638.712622-6-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 5/6] vswitchd: Add JSON output for 'list-commands' 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 'list-commands' command now supports machine-readable JSON output in addition to the plain-text output for humans. Reported-at: https://bugzilla.redhat.com/1824861 Signed-off-by: Jakob Meng --- NEWS | 1 + lib/unixctl.c | 46 ++++++++++++++++++++++++++++++------------- tests/ovs-vswitchd.at | 11 +++++++++++ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index 957351acb..af83623b3 100644 --- a/NEWS +++ b/NEWS @@ -13,6 +13,7 @@ Post-v3.3.0 * Added new option [--pretty] to print JSON output in a readable fashion. E.g. members of objects and elements of arrays are printed one per line, with indentation. + * 'list-commands' 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/lib/unixctl.c b/lib/unixctl.c index 67cf26418..ca54884d0 100644 --- a/lib/unixctl.c +++ b/lib/unixctl.c @@ -68,24 +68,42 @@ static void unixctl_list_commands(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 **nodes = shash_sort(&commands); - size_t i; + if (unixctl_command_get_output_format(conn) == OVS_OUTPUT_FMT_JSON) { + struct json *json_commands = json_array_create_empty(); + const struct shash_node *node; - ds_put_cstr(&ds, "The available commands are:\n"); + SHASH_FOR_EACH (node, &commands) { + const struct unixctl_command *command = node->data; - for (i = 0; i < shash_count(&commands); i++) { - const struct shash_node *node = nodes[i]; - const struct unixctl_command *command = node->data; - - if (command->usage) { - ds_put_format(&ds, " %-23s %s\n", node->name, command->usage); + if (command->usage) { + struct json *json_command = json_object_create(); + json_object_put_string(json_command, "name", node->name); + json_object_put_string(json_command, "usage", command->usage); + json_array_add(json_commands, json_command); + } } - } - free(nodes); + unixctl_command_reply_json(conn, json_commands); + } else { + struct ds ds = DS_EMPTY_INITIALIZER; + const struct shash_node **nodes = shash_sort(&commands); + size_t i; - unixctl_command_reply(conn, ds_cstr(&ds)); - ds_destroy(&ds); + ds_put_cstr(&ds, "The available commands are:\n"); + + for (i = 0; i < shash_count(&commands); ++i) { + const struct shash_node *node = nodes[i]; + const struct unixctl_command *command = node->data; + + if (command->usage) { + ds_put_format(&ds, " %-23s %s\n", node->name, + command->usage); + } + } + free(nodes); + + unixctl_command_reply(conn, ds_cstr(&ds)); + ds_destroy(&ds); + } } static void diff --git a/tests/ovs-vswitchd.at b/tests/ovs-vswitchd.at index 5683896ef..dcda71d04 100644 --- a/tests/ovs-vswitchd.at +++ b/tests/ovs-vswitchd.at @@ -281,3 +281,14 @@ AT_CHECK_UNQUOTED([ovs-appctl --format json --pretty version], [0], [dnl "reply-format": "plain"}]) AT_CLEANUP + +AT_SETUP([ovs-vswitchd list-commands]) +OVS_VSWITCHD_START + +AT_CHECK([ovs-appctl list-commands], [0], [ignore]) +AT_CHECK([ovs-appctl --format json list-commands], [0], [stdout]) +AT_CHECK([wc -l stdout], [0], [0 stdout +]) +AT_CHECK([grep -E '^\[[\{"name":.*\}\]]$' stdout], [0], [ignore]) + +AT_CLEANUP 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