From patchwork Thu Apr 11 14:47:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922613 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=Vx32uDj2; 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 4VFjGv6qW1z1yYB for ; Fri, 12 Apr 2024 00:47:51 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id A8D82822C6; Thu, 11 Apr 2024 14:47:48 +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 xfISp8j1NPO5; Thu, 11 Apr 2024 14:47:41 +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 F405082297 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=Vx32uDj2 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id F405082297; Thu, 11 Apr 2024 14:47:40 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 988CBC0DD2; Thu, 11 Apr 2024 14:47:40 +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 4D17BC0037 for ; Thu, 11 Apr 2024 14:47:38 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 2987041EDB for ; Thu, 11 Apr 2024 14:47:38 +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 0u0gucqujlCr for ; Thu, 11 Apr 2024 14:47:36 +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 0C27A41EE7 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 0C27A41EE7 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=Vx32uDj2 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 0C27A41EE7 for ; Thu, 11 Apr 2024 14:47:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712846854; 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=Vx32uDj2NkRc3ifwXZlIt1OeI91isavYVlRorMxzM0ZSiIatkNI8Vyqf2+aLlTSwvztjEh ISnoYLPpFeJV49BnR8YvS8OTOmVOxem+XYkq5Qs/pWZQ19+sXbRlZesENhCA7llKVM+QTz 5Cd6yEtQSVZFfp0IaQWTgGVD6qDPl20= 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-518-WEbEBwzcPpmPlZ0xaLKxFw-1; Thu, 11 Apr 2024 10:47:32 -0400 X-MC-Unique: WEbEBwzcPpmPlZ0xaLKxFw-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a51b00fc137so387508666b.0 for ; Thu, 11 Apr 2024 07:47:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712846851; x=1713451651; 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=wajdX7IUCvpmUHK7A5fPCDATbSzFO8XXyA6xDP0TFPOBtRxLANQE0Q9em2XWCmaCdb 3heMas+RhUjlpc0m4P6SeonAvU5c0ZzhDd3XA73ZT/ehukbhRhHuo575DmFCz+Vrl5W8 GRYkDeAToLTlPBUq/K3CglPq3CRMcrEbDRo8aYqwzWcly7sNoXggG/19Pad8aaP1rTuO ByHB1CpW714vIENp4PSNz+N/HNW4xMMkLsEgufIJ4QtRkPvxnZbq/v7iWp0kV5IReViQ WKQeYDnUmz1dvelqrP2J784pcC5dn+Eryn9rh1QowTPIqU+LO4M6cTB8ORHZ03TeV6s2 qeVg== X-Gm-Message-State: AOJu0YzM81L+U4fU6yVF6av0+dIuK4lHHoubOqPOctgRFchQkaGnYtCK e1/ACgRqD5jxjvzMAHqSp23lp7RCILc3JmKRkixeNIEMrv3z3gBdsRZ1njCJQAlF38JG7kUQAqZ 5J1s0VSuj8lIo7wJ/3V4d582I3HA/esW8msK3hQe4gxEHD9U64Q4WgjLNb4baClj7o6a3L1O4kQ xoFFdmr1qr9wtpne8sHL2iJerDVf5B X-Received: by 2002:a17:907:86a8:b0:a51:e05b:a166 with SMTP id qa40-20020a17090786a800b00a51e05ba166mr5708875ejc.11.1712846851349; Thu, 11 Apr 2024 07:47:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGhR34jlE8QrauyJnWANlWYT/Et0veZ93trW8QlayXs8RHRIsqZyTLALjNIIPSpdVnK4BM+5A== X-Received: by 2002:a17:907:86a8:b0:a51:e05b:a166 with SMTP id qa40-20020a17090786a800b00a51e05ba166mr5708836ejc.11.1712846850836; Thu, 11 Apr 2024 07:47:30 -0700 (PDT) Received: from positronik4lide.. ([87.122.56.49]) by smtp.gmail.com with ESMTPSA id lo17-20020a170906fa1100b00a466af74ef2sm816835ejb.2.2024.04.11.07.47.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 07:47:30 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org, echaudro@redhat.com, ktraynor@redhat.com, aconole@redhat.com, rjarry@redhat.com Date: Thu, 11 Apr 2024 16:47:13 +0200 Message-Id: <20240411144718.1921869-2-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240411144718.1921869-1-jmeng@redhat.com> References: <20240411144718.1921869-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v8 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 Thu Apr 11 14:47:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922611 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=YpHRcRxT; 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 4VFjGm4r27z1yYB for ; Fri, 12 Apr 2024 00:47:44 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id AE7A141AB3; Thu, 11 Apr 2024 14:47:41 +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 dO2GKjimMjSw; Thu, 11 Apr 2024 14:47:39 +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 1FCDF41A4B 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=YpHRcRxT Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp2.osuosl.org (Postfix) with ESMTPS id 1FCDF41A4B; Thu, 11 Apr 2024 14:47:39 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 870AFC0DD5; Thu, 11 Apr 2024 14:47:38 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 60F5FC0037 for ; Thu, 11 Apr 2024 14:47:37 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 4F70C405EE for ; Thu, 11 Apr 2024 14:47: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 2bU7IxfpvqHd for ; Thu, 11 Apr 2024 14:47:36 +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 smtp2.osuosl.org 2064140191 Authentication-Results: smtp2.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 2064140191 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp2.osuosl.org (Postfix) with ESMTPS id 2064140191 for ; Thu, 11 Apr 2024 14:47:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712846854; 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=YpHRcRxTyEAoP9W4S7GW//qADjF2JXnkFbctrW7azhVRUbSQC25FORuvLql/x3XOTRr2iN Am6QdF07q7yRktjJFK0uB02Agw3nscGMgANfZf3PrHje8/jxYeFf0w8QsB10KsZ+Ay4a1B fr0l1SbYezz/YUvkdpWlli3Ij6bGVAI= 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-524-RuHCa7QaND29hiZ-AWNwlA-1; Thu, 11 Apr 2024 10:47:33 -0400 X-MC-Unique: RuHCa7QaND29hiZ-AWNwlA-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a467a6d4e3eso382862766b.3 for ; Thu, 11 Apr 2024 07:47:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712846852; x=1713451652; 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=sI/2h2e70Zz/EOHKV8FyvVKLu3VxVd3Fhto4xYSgEVteIQF63p9F21pUoQHCnzvrCE 3nIjfwKEhMLzL9o+7aZpdssx5G6haWLU42vhiVA+C5EwN78fIysLaOromg6+s/ngu26j fiA6VH5s0w67L51x93b5MGO1QUBxmCVXLPqPQ9ZzzO/ZNWHz7QU00C2g+x07mmx06Rtg 7n88vSJivJVyhaU9Ja+rlIEu76mDzktMdF5aRmlZBKVIGVFhFPFGSgSUT5V5idmxjik3 ybFTsoLFo35vrBizsOUNhDfLXNWfCd9OkPWju8iiCH1ji9AHdgiWH7f9pAtKszxjENYr bl3g== X-Gm-Message-State: AOJu0YynpqkDMxqNjlIHpTY+tzYyY8Fua8kFQ0kaukrB8YFcjr9B+Ydi xWvGxficXnndo9Y0WZTgBu/cFXF/JNrWuReyTun1wuONjrLW16oWvG7NCy4zpqZt9pFIgJvWTyz jZtfqKANXmNCHttbyZRKE7BMemdH9D+yIgSuNMrcu/2t5F4qv0bfgnlnEUnhrqQCLTuTGLowxy6 JD+0CjOreo+xJ/o3+7OAfD+K3D+5KM X-Received: by 2002:a17:906:af1b:b0:a52:17f:e693 with SMTP id lx27-20020a170906af1b00b00a52017fe693mr3467101ejb.18.1712846852366; Thu, 11 Apr 2024 07:47:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHX6vuX6oWgMBHyO2Q7ci2T7gn9K5rPj4kHwfKYBBHFob2xtBNVPX4SQiwpe92xbiNgAfylKQ== X-Received: by 2002:a17:906:af1b:b0:a52:17f:e693 with SMTP id lx27-20020a170906af1b00b00a52017fe693mr3467079ejb.18.1712846851774; Thu, 11 Apr 2024 07:47:31 -0700 (PDT) Received: from positronik4lide.. ([87.122.56.49]) by smtp.gmail.com with ESMTPSA id lo17-20020a170906fa1100b00a466af74ef2sm816835ejb.2.2024.04.11.07.47.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 07:47:31 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org, echaudro@redhat.com, ktraynor@redhat.com, aconole@redhat.com, rjarry@redhat.com Date: Thu, 11 Apr 2024 16:47:14 +0200 Message-Id: <20240411144718.1921869-3-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240411144718.1921869-1-jmeng@redhat.com> References: <20240411144718.1921869-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v8 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 Thu Apr 11 14:47:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922612 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=E+PoVKHG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.133; helo=smtp2.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VFjGq5DDHz1yYB for ; Fri, 12 Apr 2024 00:47:47 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id F23CC41ACC; Thu, 11 Apr 2024 14:47:43 +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 mY3PoKVtvbJU; Thu, 11 Apr 2024 14:47:40 +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 E548641A6E 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=E+PoVKHG Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp2.osuosl.org (Postfix) with ESMTPS id E548641A6E; Thu, 11 Apr 2024 14:47:39 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id AFB17C0DD2; Thu, 11 Apr 2024 14:47:39 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 09579C0077 for ; Thu, 11 Apr 2024 14:47:38 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id E7725612C4 for ; Thu, 11 Apr 2024 14:47:37 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id M31DOX_JYNeD for ; Thu, 11 Apr 2024 14:47:37 +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 smtp3.osuosl.org EBC10612C2 Authentication-Results: smtp3.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org EBC10612C2 Authentication-Results: smtp3.osuosl.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=E+PoVKHG Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp3.osuosl.org (Postfix) with ESMTPS id EBC10612C2 for ; Thu, 11 Apr 2024 14:47:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712846855; 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=GqOpqFhwTbw+r61xgEEyjyFODlKUU/aWzxF2GsJiV1s=; b=E+PoVKHGWcaO8SInAjVD7gvM06YJuByWm+uN7wZPFlajuAq9Tr2ymKPijOKptdqYC4A/Fo Oc4gUrgC5uZJnPRWihxZobb4vLPfnpbgaReGgZ8wzmeLz7EUS960lJlb+WnNoEqSXtNZoM GLF9ZQZF6uMnEImimuy5b7yLFdOjjLI= 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-283-_twzjfhgPfuGdDvX5WA59w-1; Thu, 11 Apr 2024 10:47:34 -0400 X-MC-Unique: _twzjfhgPfuGdDvX5WA59w-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a51ad5289acso388777166b.1 for ; Thu, 11 Apr 2024 07:47:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712846853; x=1713451653; 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=GqOpqFhwTbw+r61xgEEyjyFODlKUU/aWzxF2GsJiV1s=; b=G42haX+p/ITJ3N4s6E2OoYc1SXCXesp1QG3Rlhea4lxx5CMt0XCQUKu8jy6cKh8iSQ 3eH6v+klX/PoVfziTmlOAox9s0dlehfaBaA1Q64ClSUHACH+nD9m6NaAUups/sVBVqd5 mnXEubPi2kF5op//M44l+oycFwQqlD8FjpbeSNrX9PHU1T6lGUhiDsXjjGfyN0QY6YvX wvScy9kybTBD1bLNKcs1IgNYp9i72ahEcYZyNZtAgKkeqMWQtYOK8az/KZBaKsZxZLjz 4qlHije+vUe9vRPjCiPyUb9/slshY7pSpsdMW2zYP2LLOSeuMGDReFLzdGFY3oSfV5UV LJBA== X-Gm-Message-State: AOJu0Yw41CSTerSO0bzju0m3+PXqK9W1PnZkP8249LN+KmKs3eU9+pXv MeeOb9FEoD4n04P2zSeqGMwM+3y4KITuuZlm33qm074ncpthNeA69rcx03XXkIiutQic8evHk4g fLw9eFFhZqTlktVo1euVNgOBYWzrCGexdqbf05TWDUsiKAGu3a+Z1eHhY7FkvDTwVu2xp6jTBfO 5ztwl5KBd9FdlpMsrxfkj0vtwyZuE3 X-Received: by 2002:a17:906:50b:b0:a52:2a91:eebd with SMTP id j11-20020a170906050b00b00a522a91eebdmr728224eja.75.1712846852952; Thu, 11 Apr 2024 07:47:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHg2BBWGCva3bP2uVSNUwWuXEyXN3cH75u/9q74uXxTZvT5KkUIM5iwBeUNXlIwoJwh5Dimew== X-Received: by 2002:a17:906:50b:b0:a52:2a91:eebd with SMTP id j11-20020a170906050b00b00a522a91eebdmr728206eja.75.1712846852603; Thu, 11 Apr 2024 07:47:32 -0700 (PDT) Received: from positronik4lide.. ([87.122.56.49]) by smtp.gmail.com with ESMTPSA id lo17-20020a170906fa1100b00a466af74ef2sm816835ejb.2.2024.04.11.07.47.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 07:47:32 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org, echaudro@redhat.com, ktraynor@redhat.com, aconole@redhat.com, rjarry@redhat.com Date: Thu, 11 Apr 2024 16:47:15 +0200 Message-Id: <20240411144718.1921869-4-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240411144718.1921869-1-jmeng@redhat.com> References: <20240411144718.1921869-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v8 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..4bb6bbe7f 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, 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..c49f0c658 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, 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..9872d5bde 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; + 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 Thu Apr 11 14:47:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922614 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=dnPXxAne; 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 4VFjGy4d2Nz1yYB for ; Fri, 12 Apr 2024 00:47:54 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 0648A82297; Thu, 11 Apr 2024 14:47:49 +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 vdfYKTv_XANQ; Thu, 11 Apr 2024 14:47:45 +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 0175D822A8 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=dnPXxAne Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id 0175D822A8; Thu, 11 Apr 2024 14:47:42 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 909BBC0DDD; Thu, 11 Apr 2024 14:47:41 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 78963C0037 for ; Thu, 11 Apr 2024 14:47:39 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 52D86612CD for ; Thu, 11 Apr 2024 14:47:39 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id nKV_9H8rqe3B for ; Thu, 11 Apr 2024 14:47:38 +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 smtp3.osuosl.org 1D166612C2 Authentication-Results: smtp3.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 1D166612C2 Authentication-Results: smtp3.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dnPXxAne Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp3.osuosl.org (Postfix) with ESMTPS id 1D166612C2 for ; Thu, 11 Apr 2024 14:47:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712846856; 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=N95rzvKn1XT5nspI6nElZAfk86lbBgbN7haAk2Hd9jg=; b=dnPXxAneUuwqvsogS7G9vfsrJ7Fk974Z4SLryHM2Q3H28pzlPrC+CCWlYC0UEdQQN8I4+N qxrV+KrEoq6OPz5X5G8877NkaKBTfUs04szzmkIJkxRdZLV7ClyIDns/jPczR8uRa+teMo EWfg42+Pi3fN4tKfqycCBKntuMj1syg= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-550-6OtMywb_M6aTjbcb6m4zmw-1; Thu, 11 Apr 2024 10:47:35 -0400 X-MC-Unique: 6OtMywb_M6aTjbcb6m4zmw-1 Received: by mail-ed1-f69.google.com with SMTP id 4fb4d7f45d1cf-5684c1abc7fso3608551a12.3 for ; Thu, 11 Apr 2024 07:47:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712846854; x=1713451654; 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=N95rzvKn1XT5nspI6nElZAfk86lbBgbN7haAk2Hd9jg=; b=DzYSEU5k/+Kg5cG7sSsSusZp7rwlEAge8wU5QXWsTqQosyo3tbiM21a0hTjYcTwevP 7sb0PjU/HsuLYpSqXh4cqUid4VKUOd6A3IYrUl+8P+8KYOdWFCh7mWda8M4aw2vmAZmb aymErlYk8qoBIugOIhOH0ox/7L8TcPcVow57qwzVxj7G+KTq6YSVaxcYmZapri5hss46 FTzvBQOr3VuSxGOIk6koMkuHZY109t9snYGBhJgVQkTDbw5Y1AoMSy/UE/3hPBfWsLHM lASXs2Ki0iqLslDyFcyJFrNHKpE58VGqS3ls9Y9w8L9xWrl2SMk+66KAXqYl/X1KooD6 jR2A== X-Gm-Message-State: AOJu0Yw6ULQKQmP7Y8LBAPvlkIMyR9Jla8+66H0qWybCsn9Zrf1rhH10 p/Wb8dQkhbsAWzojweHWsJgBGo/N8LFK4R59j6bNFZad9PSzzsm77n9H7Tc5pfe8MInNoFGNq0F n6LOCcBLc4oJWPglyn+1BCQZXlojswwX96IKS4cb2GW4iaCg1HvFtL+ih2/YS+yspA6ppZNWKDy xclfQmVMVTQ/zomR6Hsq56SrvmKKRw X-Received: by 2002:a17:907:2087:b0:a51:9421:8273 with SMTP id pv7-20020a170907208700b00a5194218273mr3680392ejb.62.1712846854379; Thu, 11 Apr 2024 07:47:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IES5NGSHdSYXvc1hu5OW58nuasnw+c/CxW3pc/AYd0y17G1Fbikxgv0THQneX9qfeOFr4DQ9Q== X-Received: by 2002:a17:907:2087:b0:a51:9421:8273 with SMTP id pv7-20020a170907208700b00a5194218273mr3680374ejb.62.1712846854019; Thu, 11 Apr 2024 07:47:34 -0700 (PDT) Received: from positronik4lide.. ([87.122.56.49]) by smtp.gmail.com with ESMTPSA id lo17-20020a170906fa1100b00a466af74ef2sm816835ejb.2.2024.04.11.07.47.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 07:47:33 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org, echaudro@redhat.com, ktraynor@redhat.com, aconole@redhat.com, rjarry@redhat.com Date: Thu, 11 Apr 2024 16:47:16 +0200 Message-Id: <20240411144718.1921869-5-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240411144718.1921869-1-jmeng@redhat.com> References: <20240411144718.1921869-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v8 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..bd9953e75 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 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 Thu Apr 11 14:47:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922616 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=bhGkSLv8; 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 4VFjJK6mY1z1yYB for ; Fri, 12 Apr 2024 00:49:04 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 08B8680FB3; Thu, 11 Apr 2024 14:49:02 +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 px_T4uA8A79a; Thu, 11 Apr 2024 14:49: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 C443280F7E 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=bhGkSLv8 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id C443280F7E; Thu, 11 Apr 2024 14:49:00 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id A3ADFC0077; Thu, 11 Apr 2024 14:49:00 +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 A2E1EC0037 for ; Thu, 11 Apr 2024 14:48:58 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id E9A7B41F67 for ; Thu, 11 Apr 2024 14:48:07 +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 z8a2ZcfBrpWr for ; Thu, 11 Apr 2024 14:48:06 +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 12B6341F46 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 12B6341F46 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=bhGkSLv8 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 12B6341F46 for ; Thu, 11 Apr 2024 14:48:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712846884; 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=iXPCsmFDwYfu1E99ZDI4H1ahYkCGElTk2SFuUGhprd8=; b=bhGkSLv84sR8yrsWPsWVq/ShX3xIkTDXyGOOQDUQ47gExO8r7q9xrUy+qEOBaKSyS+NoHB gyA0DL9WNkva1E43o5CFiJw11FCG5aajmFDKfFGBhzriN3S1XqXpxyMVgyDgJhoekZcfO1 agjXSFgZPnwd1/LG1mkLVhvOxKBlt/s= 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-460-rhLQULrfMPqHe-irsg4SRA-1; Thu, 11 Apr 2024 10:47:53 -0400 X-MC-Unique: rhLQULrfMPqHe-irsg4SRA-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a467a6d4e3eso382865666b.3 for ; Thu, 11 Apr 2024 07:47:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712846855; x=1713451655; 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=iXPCsmFDwYfu1E99ZDI4H1ahYkCGElTk2SFuUGhprd8=; b=oAg23d0LpTSDMlQriflE7its1Jh6aOJSV7ckKjGpgwgKwzYl3T+JkTOw8WYizhE+Wu UA8qrCaJmcTuB3HQhgRCllSOL+9HcSQ/4xSqb4y/0Ab5tntVGn7mz1zF9cgIj6wq3xEe zjoovvqo2pyV9VQGnIc/WZxtM201Jy6pAqcxwswTlZWtNW08cT1rUkCtMuwwQEGF65hP uWnh2mmRLMLShAOt83jLCnuj13/EVQ2f+6oB4+M2W2gY768P+OFnYGe8RHmLOiCVFt43 5NY83Yfd3Rl58WaUhoU5IB02hRfGmtKtwOhpdruj/TVcRG3THBxDIuV7DY/kvxj0WBQW 1SXA== X-Gm-Message-State: AOJu0Yw5Z6dRU13ncmDh5GED+PdB98loGWvFpq/J3wfzBXng0NFhgiag cIxt1MPCx/tcPL1f3NFmS/o8jlzP4iqdi+sg5pUrkLjz0aBmTqlthw85o4nrE0kpTdZmHFj+OA3 Pgt66sGkuJCZYZN2kdCohOzMMLjC+g/Jqu2ftv2kUIhcqzlUfaoLvHsLWWJemmGVuiZRnkD3oTa uM9TnfqYt5NfXdEnQ1RqaeWckckwl8 X-Received: by 2002:a17:906:f44:b0:a47:5182:3b83 with SMTP id h4-20020a1709060f4400b00a4751823b83mr3089149ejj.61.1712846855194; Thu, 11 Apr 2024 07:47:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFo0RYitTXXnZl8hDKYMckhXmOaonVTZbUOObvyybeTKJDLAVUI4K3NnZRtRxQyPsqGJPhBKQ== X-Received: by 2002:a17:906:f44:b0:a47:5182:3b83 with SMTP id h4-20020a1709060f4400b00a4751823b83mr3089132ejj.61.1712846854859; Thu, 11 Apr 2024 07:47:34 -0700 (PDT) Received: from positronik4lide.. ([87.122.56.49]) by smtp.gmail.com with ESMTPSA id lo17-20020a170906fa1100b00a466af74ef2sm816835ejb.2.2024.04.11.07.47.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 07:47:34 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org, echaudro@redhat.com, ktraynor@redhat.com, aconole@redhat.com, rjarry@redhat.com Date: Thu, 11 Apr 2024 16:47:17 +0200 Message-Id: <20240411144718.1921869-6-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240411144718.1921869-1-jmeng@redhat.com> References: <20240411144718.1921869-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v8 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 4bb6bbe7f..3c611a21d 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 Thu Apr 11 14:47:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922615 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=T49QUjU9; 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 4VFjH15kn5z1yYB for ; Fri, 12 Apr 2024 00:47:57 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 8DBFF41F2C; Thu, 11 Apr 2024 14:47:48 +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 quq39ilUuTtx; Thu, 11 Apr 2024 14:47:46 +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 C036841F11 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=T49QUjU9 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp4.osuosl.org (Postfix) with ESMTPS id C036841F11; Thu, 11 Apr 2024 14:47:45 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 96596C0077; Thu, 11 Apr 2024 14:47:45 +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 5C79AC0037 for ; Thu, 11 Apr 2024 14:47:44 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 0760782280 for ; Thu, 11 Apr 2024 14:47:44 +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 rtS_QGw3KJdg for ; Thu, 11 Apr 2024 14:47:40 +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 smtp1.osuosl.org 67CE78228F 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 67CE78228F 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=T49QUjU9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp1.osuosl.org (Postfix) with ESMTPS id 67CE78228F for ; Thu, 11 Apr 2024 14:47:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712846859; 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=T49QUjU9UkBvrXeEQ6PafH/drATESeOWcLrg/eincT7cD4kXTaaFaHhtbFkK1OCWFPi8gR AXpHXanmmVrmRnHSGWUtls/3kjPmLTZ7A/oRBIgvGuGlAlAyoW/Y3zM+zODZCp6PcTPMmQ 6Tg31bRw23/Yh8SCNfyLBF/6mzv53+Y= 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-68-eAzmKw7TPh6u3naf0vfS1w-1; Thu, 11 Apr 2024 10:47:37 -0400 X-MC-Unique: eAzmKw7TPh6u3naf0vfS1w-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a467a6d4e3eso382866666b.3 for ; Thu, 11 Apr 2024 07:47:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712846856; x=1713451656; 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=Mr2OkmGa+4Bh+uoMqi+1MaPLaMH7iVebERRccPBGdUHgd3mBHc2VMirNlM+5uCaTeY vsMwWEzIRTJayrDmFrE5mhLHtKZ2OOZlAKI0khoigrlppmRcIp26dwCpLAEpfYSG0oPo H2nxtsTvXrKWeuPqeClFhTFfU522SbHkbVCaRhNfOFpj7LJHqdjFvE9bEZ8J7xSY150G S4+m6VMOHaNKnTfye4VXztirzBMyw2AOar5LPr8VMvgABr9SFYmwyPS+rAEp6CWwOEJ5 iJuMlpEv+obaQG1//H80Xq9QRtI3uCFDnjcF4uQHmK8UUAwy5d23+wSsKB9bn0mJs/Ps 00BQ== X-Gm-Message-State: AOJu0YzJOzfo56otqHxrg013imA68nMfITg0OnybVTzkJ2SnRu2zcC3F Ss2SOmBXaU78HnB/ATBUFwNdS0/6AqSk6HiupXxVUGOMUOjgtZvyNFRxqp2aqNTFofokrvzlmXK wbM9nyaM9LGK1py8QBj1TlkHWtW61avCj4hd/dZiLZE4XCtGFoFjMJOgvJmyrI/X8hnr7/07eaQ wcWnXb8niCn321/YlFlncV6A3oel5L X-Received: by 2002:a17:906:244d:b0:a4e:67c9:8047 with SMTP id a13-20020a170906244d00b00a4e67c98047mr2937467ejb.32.1712846856011; Thu, 11 Apr 2024 07:47:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEngMB6Wdz0lTOGODRE2jP2+RTLfDo1ieYLPPUViDSuz6CW0Od2lXGNtHfOytIMR3uBpMgUcg== X-Received: by 2002:a17:906:244d:b0:a4e:67c9:8047 with SMTP id a13-20020a170906244d00b00a4e67c98047mr2937450ejb.32.1712846855629; Thu, 11 Apr 2024 07:47:35 -0700 (PDT) Received: from positronik4lide.. ([87.122.56.49]) by smtp.gmail.com with ESMTPSA id lo17-20020a170906fa1100b00a466af74ef2sm816835ejb.2.2024.04.11.07.47.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 07:47:35 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org, echaudro@redhat.com, ktraynor@redhat.com, aconole@redhat.com, rjarry@redhat.com Date: Thu, 11 Apr 2024 16:47:18 +0200 Message-Id: <20240411144718.1921869-7-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240411144718.1921869-1-jmeng@redhat.com> References: <20240411144718.1921869-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v8 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