From patchwork Tue Jul 9 07:14: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: 1958214 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=EeGay6cz; 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 4WJC131Hf0z1xr9 for ; Tue, 9 Jul 2024 17:14:47 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 2601440E62; Tue, 9 Jul 2024 07:14:45 +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 Nwkdcn8xLinW; Tue, 9 Jul 2024 07:14: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 smtp4.osuosl.org 9B91C40D97 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=EeGay6cz Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp4.osuosl.org (Postfix) with ESMTPS id 9B91C40D97; Tue, 9 Jul 2024 07:14:38 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 50DF7C0A98; Tue, 9 Jul 2024 07:14:38 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 328F6C0A9A for ; Tue, 9 Jul 2024 07:14:34 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 1264740E6A for ; Tue, 9 Jul 2024 07:14:34 +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 tO9DFvCz7lzN for ; Tue, 9 Jul 2024 07:14:31 +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 3982340E62 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 3982340E62 Authentication-Results: smtp2.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=EeGay6cz 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 3982340E62 for ; Tue, 9 Jul 2024 07:14:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720509270; 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=fNxocz4zhButGC/E1QCN1alvefXIMAGSAZNuNwQfckQ=; b=EeGay6czVz+Zu5LxPD6zfF2fZgQ8mNONgppvDNUyEnXWsQJ11QNSUt0KJ5tmYMC+f/RW5w OzoyJDMCNzIbXQMRvSymIrVnXJ34vk8TCUGIakPLsEOsCTXFu25H9O4lk7/qudvWWIzor4 goPDW8N2pCtJj61ty1ysUdk0XYpRiRo= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-354-tHUBf8nWNjWXhdHTXS8eMQ-1; Tue, 09 Jul 2024 03:14:28 -0400 X-MC-Unique: tHUBf8nWNjWXhdHTXS8eMQ-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-427261986c0so535095e9.2 for ; Tue, 09 Jul 2024 00:14:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720509267; x=1721114067; 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=fNxocz4zhButGC/E1QCN1alvefXIMAGSAZNuNwQfckQ=; b=rYGXxLjQWOKp77b9tYpGXKBUbhjDL6iIO6gDHOKZx3O38zaEP+bugwgVYT+o9WJvV8 b9gF/k+BrNMcW7JKeNnKgt7U1PrRO/WdyfUyCCXGmZrtGfUktu2hxNxPzeKbX6lAo5UP LIFi4YsXIoXz9VqYlnSD8p6HyPFxDvztZicNQovc1XSRYugXSnz+N3cbXqtcrFxSWgT6 RFnH34IgTIxHrWLHX+7JQyyYN0ee8lhfpltNHZ5gKjjiA6oVU08HFR7ENypBhlRWKoj7 Jp2ZqZessiupio8++X3fLImnRhfticfGZydS7ZblLXVpEI9GBUsH56JJIRR2yGD09eR2 twaw== X-Gm-Message-State: AOJu0Yxx262w6D+axa7sJhhCw/nCy6xxUyp4RuSNK32Fk4cB/ZxGqwhC cl9itbo3kgXaAjXB8X7AJeOMd6OPbqJ45LzMzVEDc+jLb4A6YeGdSkojvP2F1fAILu5GLEhqyae kRqRspBANwQ28rA8pZW6xYGqN7wsgTKQT+VztYGRuuAIWG6GbOJ8ueVZy6gHWMIjL5fI4ba0Ea5 Y4r56w6QZrFE3oojP8Fa3oiszj9V9D X-Received: by 2002:a05:600c:3042:b0:426:6a5e:73c5 with SMTP id 5b1f17b1804b1-426708f1035mr11188375e9.37.1720509266952; Tue, 09 Jul 2024 00:14:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IERrpVWu7h+nj0l93zsYpcf/GcP4GdnpVRJlicaQbPhauZP+Qny+M/3xY7Rg1Fm2GRJt/EidQ== X-Received: by 2002:a05:600c:3042:b0:426:6a5e:73c5 with SMTP id 5b1f17b1804b1-426708f1035mr11188065e9.37.1720509266157; Tue, 09 Jul 2024 00:14:26 -0700 (PDT) Received: from positronik4lide.redhat.com ([87.122.57.92]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-367cdfab136sm1638049f8f.98.2024.07.09.00.14.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jul 2024 00:14:25 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org Date: Tue, 9 Jul 2024 09:14:17 +0200 Message-Id: <20240709071422.11492-2-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240709071422.11492-1-jmeng@redhat.com> References: <20240709071422.11492-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v14 1/6] Add global option for JSON output to ovs-appctl. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" 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-appctl. It 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-appctl 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 a unixctl_command_reply_json function 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, the unixctl_command_reply_json() function 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":"$PLAIN_TEXT_HERE","reply-format":"plain"} Thus commands which have been executed successfully will not fail when they try to render the output at a later stage. A test for the 'version' command has been implemented which shows how the provisional JSON document looks like in practice. For a cleaner JSON document, the trailing newline has been moved from the program version string to function ovs_print_version(). This way, the plain-text output of the 'version' command has not changed. Output formatting has been moved from unixctl_client_transact() in lib/unixctl.c to utilities/ovs-appctl.c. The former merely returns the JSON objects returned from the server and the latter is now responsible for printing it properly. 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 chosen name also better aligns with ovsdb-client where '-f,--format' controls output formatting. Reported-at: https://bugzilla.redhat.com/1824861 Signed-off-by: Jakob Meng --- Documentation/ref/ovs-appctl.8.rst | 12 ++ NEWS | 3 + lib/unixctl.c | 180 ++++++++++++++++++++++------- lib/unixctl.h | 17 ++- lib/util.c | 6 +- python/ovs/unixctl/server.py | 3 - tests/appctl.py | 5 + tests/ovs-vswitchd.at | 12 ++ utilities/ovs-appctl.c | 135 +++++++++++++++++++--- 9 files changed, 305 insertions(+), 68 deletions(-) diff --git a/Documentation/ref/ovs-appctl.8.rst b/Documentation/ref/ovs-appctl.8.rst index 3ce02e984..148cc7632 100644 --- a/Documentation/ref/ovs-appctl.8.rst +++ b/Documentation/ref/ovs-appctl.8.rst @@ -8,6 +8,7 @@ Synopsis ``ovs-appctl`` [``--target=`` | ``-t`` ] [``--timeout=`` | ``-T`` ] +[``--format=`` | ``-f`` ] [...] ``ovs-appctl --help`` @@ -67,6 +68,17 @@ In normal use only a single option is accepted: runtime to approximately seconds. If the timeout expires, ``ovs-appctl`` exits with a ``SIGALRM`` signal. +* ``-f `` or ``--format=`` + + Tells ``ovs-appctl`` which output format to use. By default, or with a + of ``text``, ``ovs-appctl`` will print plain-text for humans. + When is ``json``, ``ovs-appctl`` will return a JSON document. + When ``json`` is 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"} + Common Commands =============== diff --git a/NEWS b/NEWS index e0359b759..f182647c7 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,9 @@ Post-v3.3.0 -------------------- - Option '--mlockall' now only locks memory pages on fault, if possible. This also makes it compatible with vHost Post-copy Live Migration. + - ovs-appctl: + * Added new option [-f|--format] to choose the output format, e.g. 'json' + or 'text' (by default). - Userspace datapath: * Conntrack now supports 'random' flag for selecting ports in a range while natting and 'persistent' flag for selection of the IP address diff --git a/lib/unixctl.c b/lib/unixctl.c index 103357ee9..e7ce77e2c 100644 --- a/lib/unixctl.c +++ b/lib/unixctl.c @@ -17,7 +17,9 @@ #include #include "unixctl.h" #include +#include #include +#include "command-line.h" #include "coverage.h" #include "dirs.h" #include "openvswitch/dynamic-string.h" @@ -50,6 +52,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 unixctl_output_fmt fmt; /* Output format of current connection. */ }; /* Server for control connection. */ @@ -63,6 +67,30 @@ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); static struct shash commands = SHASH_INITIALIZER(&commands); +const char * +unixctl_output_fmt_to_string(enum unixctl_output_fmt fmt) +{ + switch (fmt) { + case UNIXCTL_OUTPUT_FMT_TEXT: return "text"; + case UNIXCTL_OUTPUT_FMT_JSON: return "json"; + default: return ""; + } +} + +bool +unixctl_output_fmt_from_string(const char *string, + enum unixctl_output_fmt *fmt) +{ + if (!strcasecmp(string, "text")) { + *fmt = UNIXCTL_OUTPUT_FMT_TEXT; + } else if (!strcasecmp(string, "json")) { + *fmt = UNIXCTL_OUTPUT_FMT_JSON; + } else { + return false; + } + return true; +} + static void unixctl_list_commands(struct unixctl_conn *conn, int argc OVS_UNUSED, const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) @@ -94,6 +122,52 @@ 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) +{ + struct ovs_cmdl_parsed_option *parsed_options = NULL; + size_t n_parsed_options; + char *error = NULL; + + static const struct option options[] = { + {"format", required_argument, NULL, 'f'}, + {NULL, 0, NULL, 0}, + }; + + error = ovs_cmdl_parse_all(argc--, (char **) (argv++), options, + &parsed_options, &n_parsed_options); + if (error) { + goto error; + } + + for (size_t i = 0; i < n_parsed_options; i++) { + struct ovs_cmdl_parsed_option *parsed_option = &parsed_options[i]; + + switch (parsed_option->o->val) { + case 'f': + if (!unixctl_output_fmt_from_string(parsed_option->arg, + &conn->fmt)) { + error = xasprintf("option format has invalid value %s", + parsed_option->arg); + goto error; + } + break; + + default: + OVS_NOT_REACHED(); + } + } + + unixctl_command_reply(conn, NULL); + free(parsed_options); + return; +error: + unixctl_command_reply_error(conn, error); + free(error); + free(parsed_options); +} + /* 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 +202,35 @@ unixctl_command_register(const char *name, const char *usage, shash_add(&commands, name, command); } +enum unixctl_output_fmt +unixctl_command_get_output_format(struct unixctl_conn *conn) +{ + return conn->fmt; +} + +/* Takes ownership of the 'body'. */ static void unixctl_command_reply__(struct unixctl_conn *conn, - bool success, const char *body) + bool success, struct json *body) { - struct json *body_json; struct jsonrpc_msg *reply; COVERAGE_INC(unixctl_replied); ovs_assert(conn->request_id); - if (!body) { - body = ""; - } - - if (body[0] && body[strlen(body) - 1] != '\n') { - body_json = json_string_create_nocopy(xasprintf("%s\n", body)); - } else { - body_json = json_string_create(body); - } - 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, JSSF_SORT); + VLOG_DBG("replying with %s, id=%s: \"%s\"", - success ? "success" : "error", id, body); + success ? "success" : "error", id, msg); + free(msg); free(id); } @@ -169,23 +242,52 @@ 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. */ + * client indicating the command was processed successfully. 'result' should + * be plain-text; use unixctl_command_reply_json() to return a JSON document + * when JSON output has been requested. Only one call to + * 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); + struct json *json_result = json_string_create(result ? result : ""); + + if (conn->fmt == UNIXCTL_OUTPUT_FMT_JSON) { + /* Wrap plain-text reply in provisional JSON document when JSON output + * has been requested. */ + struct json *json_reply = json_object_create(); + + json_object_put_string(json_reply, "reply-format", "plain"); + json_object_put(json_reply, "reply", json_result); + + json_result = json_reply; + } + + unixctl_command_reply__(conn, true, json_result); +} + +/* Replies to the active unixctl connection 'conn'. 'body' is sent to the + * client indicating the command was processed successfully. Use this function + * when JSON output has been requested; otherwise use unixctl_command_reply() + * for plain-text output. Only one call to unixctl_command_reply*() functions + * may be made per request. + * + * Takes ownership of the 'body'. */ +void +unixctl_command_reply_json(struct unixctl_conn *conn, struct json *body) +{ + ovs_assert(conn->fmt == UNIXCTL_OUTPUT_FMT_JSON); + 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. 'error' should + * be plain-text. Only one call to 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, error); + unixctl_command_reply__(conn, false, + json_string_create(error ? error : "")); } /* Creates a unixctl server listening on 'path', which for POSIX may be: @@ -250,6 +352,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]", 1, 2, + unixctl_set_options, NULL); struct unixctl_server *server = xmalloc(sizeof *server); server->listener = listener; @@ -381,6 +485,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 = UNIXCTL_OUTPUT_FMT_TEXT; } else if (error == EAGAIN) { break; } else { @@ -483,7 +588,7 @@ 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[], struct json **result, struct json **err) { struct jsonrpc_msg *request, *reply; struct json **json_args, *params; @@ -506,24 +611,15 @@ 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)); - } else { - VLOG_WARN("%s: unexpected error 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)); - error = EINVAL; - } + if (reply->result && reply->error) { + VLOG_WARN("unexpected response when communicating with %s: %s\n %s", + jsonrpc_get_name(client), + json_to_string(reply->result, JSSF_SORT), + json_to_string(reply->error, JSSF_SORT)); + error = EINVAL; + } else { + *result = json_nullable_clone(reply->result); + *err = json_nullable_clone(reply->error); } jsonrpc_msg_destroy(reply); diff --git a/lib/unixctl.h b/lib/unixctl.h index 4562dbc49..1965f100d 100644 --- a/lib/unixctl.h +++ b/lib/unixctl.h @@ -17,10 +17,21 @@ #ifndef UNIXCTL_H #define UNIXCTL_H 1 +#include + #ifdef __cplusplus extern "C" { #endif +struct json; +enum unixctl_output_fmt { + UNIXCTL_OUTPUT_FMT_TEXT = 1 << 0, + UNIXCTL_OUTPUT_FMT_JSON = 1 << 1, +}; + +const char *unixctl_output_fmt_to_string(enum unixctl_output_fmt); +bool unixctl_output_fmt_from_string(const char *, enum unixctl_output_fmt *); + /* Server for Unix domain socket control connection. */ struct unixctl_server; int unixctl_server_create(const char *path, struct unixctl_server **); @@ -36,7 +47,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[], - char **result, char **error); + struct json **result, struct json **error); /* Command registration. */ struct unixctl_conn; @@ -45,8 +56,12 @@ 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 unixctl_output_fmt unixctl_command_get_output_format( + struct unixctl_conn *); void unixctl_command_reply_error(struct unixctl_conn *, const char *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/lib/util.c b/lib/util.c index 3a6351a2f..84e8c4966 100644 --- a/lib/util.c +++ b/lib/util.c @@ -619,11 +619,11 @@ ovs_set_program_name(const char *argv0, const char *version) free(program_version); if (!strcmp(version, VERSION)) { - program_version = xasprintf("%s (Open vSwitch) "VERSION"\n", + program_version = xasprintf("%s (Open vSwitch) "VERSION, program_name); } else { program_version = xasprintf("%s %s\n" - "Open vSwitch Library "VERSION"\n", + "Open vSwitch Library "VERSION, program_name, version); } } @@ -760,7 +760,7 @@ ovs_get_program_name(void) void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp) { - printf("%s", program_version); + printf("%s\n", program_version); if (min_ofp || max_ofp) { printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp); } diff --git a/python/ovs/unixctl/server.py b/python/ovs/unixctl/server.py index b9cb52fad..d24a7092c 100644 --- a/python/ovs/unixctl/server.py +++ b/python/ovs/unixctl/server.py @@ -87,9 +87,6 @@ class UnixctlConnection(object): if body is None: body = "" - if body and not body.endswith("\n"): - body += "\n" - if success: reply = Message.create_reply(body, self._request_id) else: diff --git a/tests/appctl.py b/tests/appctl.py index b85b364fa..e5cc28138 100644 --- a/tests/appctl.py +++ b/tests/appctl.py @@ -63,11 +63,16 @@ def main(): ovs.util.ovs_fatal(err_no, "%s: transaction error" % target) elif error is not None: sys.stderr.write(error) + if error and not error.endswith("\n"): + sys.stderr.write("\n") + ovs.util.ovs_error(0, "%s: server returned an error" % target) sys.exit(2) else: assert result is not None sys.stdout.write(result) + if result and not result.endswith("\n"): + sys.stdout.write("\n") if __name__ == '__main__': diff --git a/tests/ovs-vswitchd.at b/tests/ovs-vswitchd.at index 977b2eba1..b1ae1ae1e 100644 --- a/tests/ovs-vswitchd.at +++ b/tests/ovs-vswitchd.at @@ -265,3 +265,15 @@ 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":"$ovs_version","reply-format":"plain"} +]) + +AT_CLEANUP diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c index ba0c172e6..721698755 100644 --- a/utilities/ovs-appctl.c +++ b/utilities/ovs-appctl.c @@ -26,57 +26,105 @@ #include "daemon.h" #include "dirs.h" #include "openvswitch/dynamic-string.h" +#include "openvswitch/json.h" #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 unixctl_output_fmt format; + char *target; +}; + +static struct cmdl_args *cmdl_args_create(void); +static struct cmdl_args *parse_command_line(int argc, char *argv[]); static struct jsonrpc *connect_to_target(const char *target); +static char *reply_to_string(struct json *reply, enum unixctl_output_fmt fmt); int main(int argc, char *argv[]) { - char *cmd_result, *cmd_error; + struct svec opt_argv = SVEC_EMPTY_INITIALIZER; + struct json *cmd_result, *cmd_error; struct jsonrpc *client; + struct cmdl_args *args; char *cmd, **cmd_argv; - const char *target; + char *msg = NULL; 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 != UNIXCTL_OUTPUT_FMT_TEXT) { + svec_add(&opt_argv, "--format"); + svec_add(&opt_argv, unixctl_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, + &cmd_result, &cmd_error); + + if (error) { + ovs_fatal(error, "%s: transaction error", args->target); + } + + if (cmd_error) { + jsonrpc_close(client); + msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT); + fputs(msg, stderr); + free(msg); + ovs_error(0, "%s: server returned an error", args->target); + exit(2); + } + + json_destroy(cmd_result); + json_destroy(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); 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); + msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT); + fputs(msg, stderr); + free(msg); + ovs_error(0, "%s: server returned an error", args->target); exit(2); } else if (cmd_result) { - fputs(cmd_result, stdout); + msg = reply_to_string(cmd_result, args->format); + fputs(msg, stdout); + free(msg); } else { OVS_NOT_REACHED(); } jsonrpc_close(client); - free(cmd_result); - free(cmd_error); + json_destroy(cmd_result); + json_destroy(cmd_error); + free(args); return 0; } @@ -101,13 +149,26 @@ 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\ + (default: text)\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 = UNIXCTL_OUTPUT_FMT_TEXT; + args->target = NULL; + + return args; +} + +static struct cmdl_args * parse_command_line(int argc, char *argv[]) { enum { @@ -117,6 +178,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 +188,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 +202,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 = optarg; break; case 'e': @@ -157,6 +218,12 @@ parse_command_line(int argc, char *argv[]) } break; + case 'f': + if (!unixctl_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 +261,10 @@ parse_command_line(int argc, char *argv[]) "(use --help for help)"); } - return target ? target : "ovs-vswitchd"; + if (!args->target) { + args->target = "ovs-vswitchd"; + } + return args; } static struct jsonrpc * @@ -236,3 +306,30 @@ connect_to_target(const char *target) return client; } +/* The caller is responsible for freeing the returned string, with free(), when + * it is no longer needed. */ +static char * +reply_to_string(struct json *reply, enum unixctl_output_fmt fmt) +{ + ovs_assert(reply); + + if (fmt == UNIXCTL_OUTPUT_FMT_TEXT && reply->type != JSON_STRING) { + ovs_error(0, "Unexpected reply type in JSON rpc reply: %s", + json_type_to_string(reply->type)); + exit(2); + } + + struct ds ds = DS_EMPTY_INITIALIZER; + + if (fmt == UNIXCTL_OUTPUT_FMT_TEXT) { + ds_put_cstr(&ds, json_string(reply)); + } else { + json_to_ds(reply, JSSF_SORT, &ds); + } + + if (ds_last(&ds) != EOF && ds_last(&ds) != '\n') { + ds_put_char(&ds, '\n'); + } + + return ds_steal_cstr(&ds); +} From patchwork Tue Jul 9 07:14: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: 1958211 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=Da4a8w9m; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::136; helo=smtp3.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WJC0x3Rw2z1xr9 for ; Tue, 9 Jul 2024 17:14:41 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 62DEC60ED8; Tue, 9 Jul 2024 07:14: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 JUeDzVZhUT65; Tue, 9 Jul 2024 07:14:35 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=2605:bc80:3010:104::8cd3:938; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 7501C60ECE Authentication-Results: smtp3.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=Da4a8w9m Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp3.osuosl.org (Postfix) with ESMTPS id 7501C60ECE; Tue, 9 Jul 2024 07:14:35 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 2B314C0A99; Tue, 9 Jul 2024 07:14:34 +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 69635C0A96 for ; Tue, 9 Jul 2024 07:14:32 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 65F5760AC5 for ; Tue, 9 Jul 2024 07:14:32 +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 Zz_bROjYQHYf for ; Tue, 9 Jul 2024 07:14:31 +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 17BAE607B3 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 17BAE607B3 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 17BAE607B3 for ; Tue, 9 Jul 2024 07:14:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720509270; 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=FLkZ88337xxXbwYHRDOIQSqV4GmjIHUMbLz2pWrQrSY=; b=Da4a8w9mqnfn+wYD74MpsM7L/N3mwO7/YW2nTZ3e6ttm8Yx+c9v0OFEMyQEhn+Sht3KOGk 1fjFhkiWCMGfahfsSgfXKb1GiMruXUl+x3WzQ3sMoHB6PBze/MAN2XqqdixueMYWd7M+fH 6tZpBl6joV0KL6jzAQN6ianX/JCLhvg= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-671-WZHCjFOdNBmHMYLKvidt_A-1; Tue, 09 Jul 2024 03:14:28 -0400 X-MC-Unique: WZHCjFOdNBmHMYLKvidt_A-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-42490ae735dso48633905e9.0 for ; Tue, 09 Jul 2024 00:14:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720509267; x=1721114067; 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=FLkZ88337xxXbwYHRDOIQSqV4GmjIHUMbLz2pWrQrSY=; b=Y0+sq5UJEtDP9yZH4EPmfL85NuStf2W299fEzRzUMCPL/1FvRG9pIb0hxuMd6IxrV/ IezlB+IRMW9Ly4tjTjbAF154CTmo6DzO9n6GdqBV4/JRmPTysaymCSznCozLe2l4mhHu cysGPLP31ptNCvVvKiz2l3gxJ1o7amku3P5AFbFA55TVyURURZAYt5AmF7kiXp8xNbGQ npUB1Jz90haqvods53KER13YzRpx5vc0fz0Bdrx0fzPXxCjqrWGJHXqnB+i/JT+epo4K TO2hjDofIo8wIPQWX6FGGhq/cXs3lZhq+qFKCO3TKXN5ABCN+XVog3YFw75NZxbVsi8P 9EMQ== X-Gm-Message-State: AOJu0YxCDBXTGZ4XiTfcTj2p3FJbbNUfe/SWTpmPSgQ1rvl+7a66Q+HA 1+N2EU93PG4abnhSvf79Br53Zv6Dr9o76Dc5XBm3XSkyyiKveAsFA3HZEl7qGscB09ppFczvO54 c2uzj1LsDPU4RCG3aE91hRe49LyuuJ85jFT87vDsfLS33nj2ZLWH1SfImE0iRwSvswjOQCde8DV bPD1PmzJXPMgjFg+zd0+2b8XCzNrAs X-Received: by 2002:a5d:64c4:0:b0:366:efbd:8aa3 with SMTP id ffacd0b85a97d-367cea46767mr1884267f8f.2.1720509267334; Tue, 09 Jul 2024 00:14:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFIrAXP69e7fngM7SSH9VBP3oakvHjNtN3KduUK2wN1KG3cBxo5yYpCyTHsIZdM1VdYb6xYVQ== X-Received: by 2002:a5d:64c4:0:b0:366:efbd:8aa3 with SMTP id ffacd0b85a97d-367cea46767mr1884243f8f.2.1720509266903; Tue, 09 Jul 2024 00:14:26 -0700 (PDT) Received: from positronik4lide.redhat.com ([87.122.57.92]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-367cdfab136sm1638049f8f.98.2024.07.09.00.14.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jul 2024 00:14:26 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org Date: Tue, 9 Jul 2024 09:14:18 +0200 Message-Id: <20240709071422.11492-3-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240709071422.11492-1-jmeng@redhat.com> References: <20240709071422.11492-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v14 2/6] python: Add option for JSON output to unixctl classes and appctl.py. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Jakob Meng This patch introduces support for different output formats to Python Unixctl* classes and appctl.py, similar to what the previous commit did for ovs-appctl. 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/__init__.py | 8 +++++ python/ovs/unixctl/client.py | 5 ++-- python/ovs/unixctl/server.py | 53 +++++++++++++++++++++++++++++----- tests/appctl.py | 39 ++++++++++++++++++++----- tests/unixctl-py.at | 7 +++++ 6 files changed, 98 insertions(+), 16 deletions(-) diff --git a/NEWS b/NEWS index f182647c7..c750ebae2 100644 --- a/NEWS +++ b/NEWS @@ -19,6 +19,8 @@ Post-v3.3.0 per interface 'options:dpdk-lsc-interrupt' to 'false'. - Python: * Added custom transaction support to the Idl via add_op(). + * Added support for different output formats like 'json' to Python's + unixctl classes. v3.3.0 - 16 Feb 2024 diff --git a/python/ovs/unixctl/__init__.py b/python/ovs/unixctl/__init__.py index 8ee312943..b05f3df72 100644 --- a/python/ovs/unixctl/__init__.py +++ b/python/ovs/unixctl/__init__.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import enum import sys import ovs.util @@ -19,6 +20,13 @@ import ovs.util commands = {} +@enum.unique +# FIXME: Use @enum.verify(enum.NAMED_FLAGS) from Python 3.11 when available. +class UnixctlOutputFormat(enum.IntFlag): + TEXT = 1 << 0 + JSON = 1 << 1 + + class _UnixctlCommand(object): def __init__(self, usage, min_args, max_args, callback, aux): self.usage = usage diff --git a/python/ovs/unixctl/client.py b/python/ovs/unixctl/client.py index 8283f99bb..8a6fcb1b9 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 @@ -41,10 +42,10 @@ class UnixctlClient(object): return error, None, None if reply.error is not None: - return 0, str(reply.error), None + return 0, reply.error, None else: assert reply.result is not None - return 0, None, str(reply.result) + return 0, None, reply.result def close(self): self._conn.close() diff --git a/python/ovs/unixctl/server.py b/python/ovs/unixctl/server.py index d24a7092c..9a58a38d5 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.unixctl.UnixctlOutputFormat.TEXT def run(self): self._rpc.run() @@ -63,10 +65,29 @@ class UnixctlConnection(object): return error def reply(self, body): - self._reply_impl(True, body) + assert body is None or isinstance(body, str) + + if body is None: + body = "" + + if self._fmt == ovs.unixctl.UnixctlOutputFormat.JSON: + body = { + "reply-format": "plain", + "reply": body + } + + return self._reply_impl_json(True, body) + + def reply_json(self, body): + self._reply_impl_json(True, body) def reply_error(self, body): - self._reply_impl(False, body) + assert body is None or isinstance(body, str) + + if body is None: + body = "" + + return self._reply_impl_json(False, body) # Called only by unixctl classes. def _close(self): @@ -78,15 +99,11 @@ class UnixctlConnection(object): if not self._rpc.get_backlog(): self._rpc.recv_wait(poller) - def _reply_impl(self, success, body): + def _reply_impl_json(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 success: reply = Message.create_reply(body, self._request_id) else: @@ -133,6 +150,25 @@ 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.unixctl.UnixctlOutputFormat], + type=str.lower) + + try: + args = parser.parse_args(args=argv) + except argparse.ArgumentError as e: + conn.reply_error(str(e)) + return + + conn._fmt = ovs.unixctl.UnixctlOutputFormat[args.format.upper()] + conn.reply(None) + + class UnixctlServer(object): def __init__(self, listener): assert isinstance(listener, ovs.stream.PassiveStream) @@ -207,4 +243,7 @@ class UnixctlServer(object): ovs.unixctl.command_register("version", "", 0, 0, _unixctl_version, version) + ovs.unixctl.command_register("set-options", "[--format text|json]", 1, + 2, _unixctl_set_options, None) + return 0, UnixctlServer(listener) diff --git a/tests/appctl.py b/tests/appctl.py index e5cc28138..4aca7efbc 100644 --- a/tests/appctl.py +++ b/tests/appctl.py @@ -37,6 +37,18 @@ def connect_to_target(target): return client +def reply_to_string(reply, fmt=ovs.unixctl.UnixctlOutputFormat.TEXT): + if fmt == ovs.unixctl.UnixctlOutputFormat.TEXT: + body = str(reply) + else: + body = ovs.json.to_string(reply) + + if body and not body.endswith("\n"): + body += "\n" + + return body + + def main(): parser = argparse.ArgumentParser(description="Python Implementation of" " ovs-appctl.") @@ -49,30 +61,43 @@ 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.unixctl.UnixctlOutputFormat], + type=str.lower) args = parser.parse_args() signal_alarm(int(args.timeout) if args.timeout else None) ovs.vlog.Vlog.init() target = args.target + format = ovs.unixctl.UnixctlOutputFormat[args.format.upper()] client = connect_to_target(target) + + if format != ovs.unixctl.UnixctlOutputFormat.TEXT: + err_no, error, _ = client.transact( + "set-options", ["--format", args.format]) + + if err_no: + ovs.util.ovs_fatal(err_no, "%s: transaction error" % target) + elif error is not None: + sys.stderr.write(reply_to_string(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) client.close() if err_no: ovs.util.ovs_fatal(err_no, "%s: transaction error" % target) elif error is not None: - sys.stderr.write(error) - if error and not error.endswith("\n"): - sys.stderr.write("\n") - + sys.stderr.write(reply_to_string(error)) ovs.util.ovs_error(0, "%s: server returned an error" % target) sys.exit(2) else: assert result is not None - sys.stdout.write(result) - if result and not result.endswith("\n"): - sys.stdout.write("\n") + sys.stdout.write(reply_to_string(result, format)) if __name__ == '__main__': diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at index 724006118..f4a664dc0 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,12 @@ 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)","reply-format":"plain"} +]) +AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format JSON version], [0], [dnl +{"reply":"$(cat expout)","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 Tue Jul 9 07:14:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1958213 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=Xfmmzz+Z; 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 4WJC110RVSz1xr9 for ; Tue, 9 Jul 2024 17:14:44 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id BF2EE40E99; Tue, 9 Jul 2024 07:14: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 vfDXVmxwwqZX; Tue, 9 Jul 2024 07:14:36 +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 smtp2.osuosl.org 4E51040E6A 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=Xfmmzz+Z Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp2.osuosl.org (Postfix) with ESMTPS id 4E51040E6A; Tue, 9 Jul 2024 07:14:35 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id A1E6EC0A98; Tue, 9 Jul 2024 07:14:35 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 16979C0A99 for ; Tue, 9 Jul 2024 07:14:33 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id E8A26814B5 for ; Tue, 9 Jul 2024 07:14:32 +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 EXa5CpheapYM for ; Tue, 9 Jul 2024 07:14:32 +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 AD1A4814A9 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 AD1A4814A9 Authentication-Results: smtp1.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=Xfmmzz+Z 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 AD1A4814A9 for ; Tue, 9 Jul 2024 07:14:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720509270; 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=AKw2+/eWghh43qSdAvLTTP848g7PfX0Eq9FFGLUFGaI=; b=Xfmmzz+ZY92gkToYm4vIrbi3hcxPt4bdrkTt+d+MhV6KOCuXuFVjKJG+1LPyzEkSS+wL93 pEkELQG0NqLRyaWiiQrI4ILgjdlkFw1IaBlWKIMqxk9kheRNw2qDcQDr1jrlBJ/wjtzz+0 KfLH5R4rZjdTe6fXSnFYqHIEYkqonOE= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-518-fzMPysRYN1GJrkKVnHLg8g-1; Tue, 09 Jul 2024 03:14:29 -0400 X-MC-Unique: fzMPysRYN1GJrkKVnHLg8g-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-36785e6c1e6so3412461f8f.3 for ; Tue, 09 Jul 2024 00:14:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720509268; x=1721114068; 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=AKw2+/eWghh43qSdAvLTTP848g7PfX0Eq9FFGLUFGaI=; b=ZEreTIkBKE3CNo1xF2V1aY0NKEHcl+ahCvlg+1YMuX8FsjLSJxZ/F8TnO+zhhAUANN eRA15ULb6LaeDIww5AVUm9JneVcRwyWbsDa55NbqJuB6D8jttOa3q1bAKXZYJo/CZl5c k2QJReXxrMueTNM5LtkoHsqPq22tfzlZBGgMwWV/vOV8iv1dlgx/shXZMnOlajuTArzH g+GRw66gCR70evTL96MeZK0rtSzrFUlIvs4jidBhfavPoQd9J27Rxs5SXYzS64Ea2v5+ VdemI2StQSuWieBo5wgu+m944Ya10b0BoxdLJRmHFHXWZlJ0FCXQ6vOTo/KDop97cpF9 807Q== X-Gm-Message-State: AOJu0Ywyb4Yk0IwOzOcpupLGPZwimikAfYUY84S15dWKCXedS0a185pl R2mZ6Qy67WDF54Y6sH84rd88BzE+X8/NK8wdC28ePDk40p9K8RAacKci/r2q7sd5PRL1Hj+ivu+ tijHkQBAVkk0mZpueDnZC80iMOc1voUcFbaHXU/h+0I82+wJTDElRwjKGwoqzPv04l0h/zv2TYG rdo0MVacLj965tzm7wx3wiSPe8hdpJ X-Received: by 2002:a05:6000:e90:b0:367:9748:ee7f with SMTP id ffacd0b85a97d-367cead931emr1141509f8f.65.1720509268034; Tue, 09 Jul 2024 00:14:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG+3NZw5MSR2MNVR6ClodKWml7MISSVFfbOlrh2Wumv0J1913Ljj5DioD/Acem+crW+4Jfe4w== X-Received: by 2002:a05:6000:e90:b0:367:9748:ee7f with SMTP id ffacd0b85a97d-367cead931emr1141491f8f.65.1720509267644; Tue, 09 Jul 2024 00:14:27 -0700 (PDT) Received: from positronik4lide.redhat.com ([87.122.57.92]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-367cdfab136sm1638049f8f.98.2024.07.09.00.14.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jul 2024 00:14:27 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org Date: Tue, 9 Jul 2024 09:14:19 +0200 Message-Id: <20240709071422.11492-4-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240709071422.11492-1-jmeng@redhat.com> References: <20240709071422.11492-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v14 3/6] appctl: Add option '--pretty' for pretty-printing JSON output. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" 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 --- Documentation/ref/ovs-appctl.8.rst | 8 +++++++ NEWS | 1 + tests/ovs-vswitchd.at | 6 ++++++ utilities/ovs-appctl.c | 34 ++++++++++++++++++++++++------ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/Documentation/ref/ovs-appctl.8.rst b/Documentation/ref/ovs-appctl.8.rst index 148cc7632..7054cf559 100644 --- a/Documentation/ref/ovs-appctl.8.rst +++ b/Documentation/ref/ovs-appctl.8.rst @@ -9,6 +9,7 @@ Synopsis [``--target=`` | ``-t`` ] [``--timeout=`` | ``-T`` ] [``--format=`` | ``-f`` ] +[``--pretty``] [...] ``ovs-appctl --help`` @@ -79,6 +80,13 @@ In normal use only a single option is accepted: {"reply-format":"plain","reply":"$PLAIN_TEXT_HERE"} +* ``--pretty`` + + By default, JSON output is printed as compactly as possible. This option + causes JSON in output to be printed in a more readable fashion. For + example, members of objects and elements of arrays are printed one + per line, with indentation. Requires ``--format=json``. + Common Commands =============== diff --git a/NEWS b/NEWS index c750ebae2..d903d2f74 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,7 @@ 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. - Userspace datapath: * Conntrack now supports 'random' flag for selecting ports in a range while natting and 'persistent' flag for selection of the IP address diff --git a/tests/ovs-vswitchd.at b/tests/ovs-vswitchd.at index b1ae1ae1e..0f7a6085e 100644 --- a/tests/ovs-vswitchd.at +++ b/tests/ovs-vswitchd.at @@ -276,4 +276,10 @@ AT_CHECK_UNQUOTED([ovs-appctl --format json version], [0], [dnl {"reply":"$ovs_version","reply-format":"plain"} ]) +AT_CHECK_UNQUOTED([ovs-appctl --format json --pretty version], [0], [dnl +{ + "reply": "$ovs_version", + "reply-format": "plain"} +]) + AT_CLEANUP diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c index 721698755..682ee100c 100644 --- a/utilities/ovs-appctl.c +++ b/utilities/ovs-appctl.c @@ -40,13 +40,15 @@ static void usage(void); /* Parsed command line args. */ struct cmdl_args { enum unixctl_output_fmt format; + unsigned int format_flags; char *target; }; static struct cmdl_args *cmdl_args_create(void); static struct cmdl_args *parse_command_line(int argc, char *argv[]); static struct jsonrpc *connect_to_target(const char *target); -static char *reply_to_string(struct json *reply, enum unixctl_output_fmt fmt); +static char *reply_to_string(struct json *reply, enum unixctl_output_fmt fmt, + unsigned int fmt_flags); int main(int argc, char *argv[]) @@ -84,7 +86,7 @@ main(int argc, char *argv[]) if (cmd_error) { jsonrpc_close(client); - msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT); + msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT, 0); fputs(msg, stderr); free(msg); ovs_error(0, "%s: server returned an error", args->target); @@ -108,13 +110,13 @@ main(int argc, char *argv[]) if (cmd_error) { jsonrpc_close(client); - msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT); + msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT, 0); fputs(msg, stderr); free(msg); ovs_error(0, "%s: server returned an error", args->target); exit(2); } else if (cmd_result) { - msg = reply_to_string(cmd_result, args->format); + msg = reply_to_string(cmd_result, args->format, args->format_flags); fputs(msg, stdout); free(msg); } else { @@ -151,6 +153,8 @@ 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\ (default: text)\n\ + --pretty Format the output in a more readable fashion.\n\ + Requires: --format=json.\n\ -h, --help Print this helpful information\n\ -V, --version Display ovs-appctl version information\n", program_name, program_name); @@ -163,6 +167,7 @@ cmdl_args_create(void) struct cmdl_args *args = xmalloc(sizeof *args); args->format = UNIXCTL_OUTPUT_FMT_TEXT; + args->format_flags = 0; args->target = NULL; return args; @@ -173,7 +178,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'}, @@ -181,6 +187,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, @@ -190,6 +197,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; @@ -232,6 +240,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); @@ -261,6 +273,13 @@ parse_command_line(int argc, char *argv[]) "(use --help for help)"); } + if (pretty) { + if (args->format != UNIXCTL_OUTPUT_FMT_JSON) { + ovs_fatal(0, "--pretty is supported with --format json only"); + } + args->format_flags |= JSSF_PRETTY; + } + if (!args->target) { args->target = "ovs-vswitchd"; } @@ -309,7 +328,8 @@ connect_to_target(const char *target) /* The caller is responsible for freeing the returned string, with free(), when * it is no longer needed. */ static char * -reply_to_string(struct json *reply, enum unixctl_output_fmt fmt) +reply_to_string(struct json *reply, enum unixctl_output_fmt fmt, + unsigned int fmt_flags) { ovs_assert(reply); @@ -324,7 +344,7 @@ reply_to_string(struct json *reply, enum unixctl_output_fmt fmt) if (fmt == UNIXCTL_OUTPUT_FMT_TEXT) { ds_put_cstr(&ds, json_string(reply)); } else { - json_to_ds(reply, JSSF_SORT, &ds); + json_to_ds(reply, JSSF_SORT | fmt_flags, &ds); } if (ds_last(&ds) != EOF && ds_last(&ds) != '\n') { From patchwork Tue Jul 9 07:14:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1958212 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=UDdwyBKb; 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 4WJC0z184vz1xr9 for ; Tue, 9 Jul 2024 17:14:42 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 8B1A940E37; Tue, 9 Jul 2024 07:14:40 +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 FAOkCbOfa4QG; Tue, 9 Jul 2024 07:14:38 +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 0E7E440D8B 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=UDdwyBKb Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp4.osuosl.org (Postfix) with ESMTPS id 0E7E440D8B; Tue, 9 Jul 2024 07:14:37 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 0063CC0A98; Tue, 9 Jul 2024 07:14:37 +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 598BBC0A97 for ; Tue, 9 Jul 2024 07:14:33 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 476FF60AC5 for ; Tue, 9 Jul 2024 07:14:33 +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 M_u9OtZXJRPS for ; Tue, 9 Jul 2024 07:14:32 +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 5F17360AB3 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 5F17360AB3 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=UDdwyBKb 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 5F17360AB3 for ; Tue, 9 Jul 2024 07:14:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720509271; 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=YhLoEfs5WzTlhOTSWFNMeuTuT8ahSTLIwPCc9WW5Qi4=; b=UDdwyBKblhRcMNSSsWFZLtQFaOas1XysbV2y/sXmEOlSf4+YSn5Os/+3eypvydKXiAAtCy iuDlNjNKEqpjlTCemzeirnoshU0pNTykgFmRkZlTf/k7fbzgZo4S0gn2gYGfvBeVEaKBNj rswbDcd9PZkzwuvSY/quJFSyYGH2e+U= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-689-6ApZzUfOOFaAnXspxqaqxg-1; Tue, 09 Jul 2024 03:14:30 -0400 X-MC-Unique: 6ApZzUfOOFaAnXspxqaqxg-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-367a064472aso3236544f8f.1 for ; Tue, 09 Jul 2024 00:14:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720509269; x=1721114069; 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=YhLoEfs5WzTlhOTSWFNMeuTuT8ahSTLIwPCc9WW5Qi4=; b=EDH0eMsHH+QG++v2kSbchmL1jnjZK09yaycjF18af0EfBGWn4yXk4ti9bbCg69gqt5 KP9lnyFUSRHurtuJ6fnTBTfsQ1CHtuWxmvkhUKjeWWv+6dMVF+085wpd3VWR+GuTcgCg biyPbIIR8Bdv/QHEMFwFvxmspDl0hrcRlH3Fxeqx3AiH1eKftNu4XivKNR82oZ3BCqjP gUw2ynWy9yL63QdhzCI6PL/+wBUW60hZBGxnlxHqYsIzpLZdvadewo/x043euPG9dh9O ptRlyDz5kJTyqwkJnz0m5ECDyifB+15/pvLI3lTeBgcZ0JY6qBAsD5Wr4fb2VjPy6ybp r8Sw== X-Gm-Message-State: AOJu0YzqD78UXNNPxWgBNZux0oGB7Bz5EC+JKx1Nibi0s7svM+p9i6Ur FqR65PLnRWmbhGy7Mfphs+xmwOJYujqxWwHQ+AfydfQqPAkKpoFfOmLYR2+XrToXmQA+9BVzkC7 aXzHnI7bL6wlQ0oS+5F+NGreCUXQHau5nZ1MwqsmMWDBrD4Rjl16tvyObW/z2LYuoaYU/6zMk2Y yu2jS/412UpxVZoqCM98udC0Yjtd9l X-Received: by 2002:a05:6000:d92:b0:367:8847:5beb with SMTP id ffacd0b85a97d-367cea965c4mr1087801f8f.33.1720509268933; Tue, 09 Jul 2024 00:14:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE0Z6jMMDiZj+ZannjYKA0owa3hMx783kUffFsW8OZWA5Yd5PUtjhDzbBd3XE+ko6FaFZ9zyg== X-Received: by 2002:a05:6000:d92:b0:367:8847:5beb with SMTP id ffacd0b85a97d-367cea965c4mr1087784f8f.33.1720509268586; Tue, 09 Jul 2024 00:14:28 -0700 (PDT) Received: from positronik4lide.redhat.com ([87.122.57.92]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-367cdfab136sm1638049f8f.98.2024.07.09.00.14.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jul 2024 00:14:28 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org Date: Tue, 9 Jul 2024 09:14:20 +0200 Message-Id: <20240709071422.11492-5-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240709071422.11492-1-jmeng@redhat.com> References: <20240709071422.11492-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v14 4/6] python: Add option for pretty-printing JSON output to appctl.py. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" 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 --- tests/appctl.py | 15 ++++++++++++--- tests/unixctl-py.at | 6 ++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/appctl.py b/tests/appctl.py index 4aca7efbc..5f4b2754a 100644 --- a/tests/appctl.py +++ b/tests/appctl.py @@ -37,11 +37,12 @@ def connect_to_target(target): return client -def reply_to_string(reply, fmt=ovs.unixctl.UnixctlOutputFormat.TEXT): +def reply_to_string(reply, fmt=ovs.unixctl.UnixctlOutputFormat.TEXT, + fmt_flags={}): if fmt == ovs.unixctl.UnixctlOutputFormat.TEXT: body = str(reply) else: - body = ovs.json.to_string(reply) + body = ovs.json.to_string(reply, **fmt_flags) if body and not body.endswith("\n"): body += "\n" @@ -66,13 +67,21 @@ def main(): choices=[fmt.name.lower() for fmt in ovs.unixctl.UnixctlOutputFormat], type=str.lower) + parser.add_argument("--pretty", action="store_true", + help="Format the output in a more readable fashion." + " Requires: --format json.") args = parser.parse_args() + if (args.format != ovs.unixctl.UnixctlOutputFormat.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.unixctl.UnixctlOutputFormat[args.format.upper()] + format_flags = dict(pretty=True) if args.pretty else {} client = connect_to_target(target) if format != ovs.unixctl.UnixctlOutputFormat.TEXT: @@ -97,7 +106,7 @@ def main(): sys.exit(2) else: assert result is not None - sys.stdout.write(reply_to_string(result, format)) + sys.stdout.write(reply_to_string(result, format, format_flags)) if __name__ == '__main__': diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at index f4a664dc0..ae8bd5ad1 100644 --- a/tests/unixctl-py.at +++ b/tests/unixctl-py.at @@ -119,6 +119,12 @@ AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format json version], [0], [ AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format JSON version], [0], [dnl {"reply":"$(cat expout)","reply-format":"plain"} ]) +AT_CHECK_UNQUOTED([PYAPPCTL_PY -t test-unixctl.py --format json --pretty version], [0], [dnl +{ + "reply":"$(cat expout)", + "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 Tue Jul 9 07:14:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1958215 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=Ks1LAMir; 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 4WJC1357r0z1ySg for ; Tue, 9 Jul 2024 17:14:47 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id F3D278183D; Tue, 9 Jul 2024 07:14:45 +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 goNTcC7eeaQp; Tue, 9 Jul 2024 07:14:43 +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 32B14814B6 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=Ks1LAMir Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id 32B14814B6; Tue, 9 Jul 2024 07:14:42 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 36A7AC0AA3; Tue, 9 Jul 2024 07:14:40 +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 65DE9C0A9C for ; Tue, 9 Jul 2024 07:14:34 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 4F715814A9 for ; Tue, 9 Jul 2024 07:14:34 +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 Y2lsO_KJ5yFO for ; Tue, 9 Jul 2024 07:14:33 +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 2218A814B5 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 2218A814B5 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 2218A814B5 for ; Tue, 9 Jul 2024 07:14:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720509272; 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=S4xDlJelwCMb2BeELvl6yfzFa7ytNzjNZuJkhmbUJ84=; b=Ks1LAMird/sVjT+szYNfV7Ow597t0Dr0wWE+08bxyKbqN8YyvBmMeKKjYFGgnEgM+V14us I0n5sYLqBRr/3BcP9CfNRGFnBrsxUOPxu0rdBU1eVadFjSW8sPkluAV1EBrNqTILoRI17e keFmxshZZfGPrKp6kRvNbAW/aRqaiW8= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-537-CrTkR_K6M8SKmsRrMYflLw-1; Tue, 09 Jul 2024 03:14:30 -0400 X-MC-Unique: CrTkR_K6M8SKmsRrMYflLw-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-36787ba7ad4so3534285f8f.3 for ; Tue, 09 Jul 2024 00:14:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720509269; x=1721114069; 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=S4xDlJelwCMb2BeELvl6yfzFa7ytNzjNZuJkhmbUJ84=; b=LgywAywrNnNqyrSezSSLX+et/AwUJHIZNxeMNWq2ZGqJzPn+M+dX9dI5A1xj73vH0P UXqWzCTqU+2xBQg934mrHvNFTS7o064YXYRZELsbFjiIYYojUZ3AMFPoNtB3g9JMURvj OmgLM0LcBLqthP0epfUGAcx04qLV5WuujyavcV4Hu9ueMFY8GBJAlwdQAK2SndObzXOb 1nGvdbZXRk9vRoej8B4sJzNWVYL9Zwtmaj1tWjUrHFYYMB9CB8aF5MVmEwkbQdh5Z15Y EuJYhBHEFpvEUz0EAU4GJe0Qtu+7q2Nh5AwtPjqzY4DHsg0PW1rpSQEjXhSyFT0x+DYi sU2A== X-Gm-Message-State: AOJu0YwIvXd+Fm+C+ugB8bXCJdE1vZ5m8MUqVke4E1snwEI14TdS75yd bfCthn2K5j2a4KW9bS6lj6AyODNUUaE3OMabrqzxIWxQHaSYqd2yuqNjtKA/bcy82Z2f7lTl+xb Qg1TRcZaH4uDEe/QlsQjvMwuaYSqNm2BHKkc6uwFcpsI8qd2gC9iQmGUOl5v2RpMW6Y4CBKn/iv IpE/bLDAm017jO013ExZXzPvQD9M2G X-Received: by 2002:a05:6000:1844:b0:366:e9b8:784f with SMTP id ffacd0b85a97d-367cea96425mr1441595f8f.32.1720509269661; Tue, 09 Jul 2024 00:14:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFBMTPtZy8yM/a9lBeuYJCPiQVpREx3I0ClItGSn65KELXCKpT0QOH3DQKyx6w9cNz8MfNdoA== X-Received: by 2002:a05:6000:1844:b0:366:e9b8:784f with SMTP id ffacd0b85a97d-367cea96425mr1441579f8f.32.1720509269342; Tue, 09 Jul 2024 00:14:29 -0700 (PDT) Received: from positronik4lide.redhat.com ([87.122.57.92]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-367cdfab136sm1638049f8f.98.2024.07.09.00.14.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jul 2024 00:14:28 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org Date: Tue, 9 Jul 2024 09:14:21 +0200 Message-Id: <20240709071422.11492-6-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240709071422.11492-1-jmeng@redhat.com> References: <20240709071422.11492-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v14 5/6] vswitchd: Add JSON output for 'list-commands' command. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" 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 | 44 +++++++++++++++++++++++++++++-------------- tests/ovs-vswitchd.at | 15 +++++++++++++++ 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index d903d2f74..feebae86e 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,7 @@ Post-v3.3.0 * 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. + * 'list-commands' now supports output in JSON format. - Userspace datapath: * Conntrack now supports 'random' flag for selecting ports in a range while natting and 'persistent' flag for selection of the IP address diff --git a/lib/unixctl.c b/lib/unixctl.c index e7ce77e2c..c060e8659 100644 --- a/lib/unixctl.c +++ b/lib/unixctl.c @@ -95,24 +95,40 @@ 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) == UNIXCTL_OUTPUT_FMT_JSON) { + struct json *json_commands = json_object_create(); + 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) { + json_object_put_string(json_commands, node->name, + command->usage); + } } - } - 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 0f7a6085e..730363e83 100644 --- a/tests/ovs-vswitchd.at +++ b/tests/ovs-vswitchd.at @@ -283,3 +283,18 @@ AT_CHECK_UNQUOTED([ovs-appctl --format json --pretty version], [0], [dnl ]) 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]) + +# Check that ovs-appctl prints a single line with a trailing newline. +AT_CHECK([wc -l stdout], [0], [1 stdout +]) + +# Check that ovs-appctl prints a JSON document. +AT_CHECK([ovstest test-json stdout], [0], [ignore]) + +AT_CLEANUP From patchwork Tue Jul 9 07:14:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1958216 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=TVvLJo2C; 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 4WJC193d2qz1xr9 for ; Tue, 9 Jul 2024 17:14:53 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 20AB1817A1; Tue, 9 Jul 2024 07:14:51 +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 zBtWw4QgDlyW; Tue, 9 Jul 2024 07:14:48 +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 EDB6681774 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=TVvLJo2C Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id EDB6681774; Tue, 9 Jul 2024 07:14:44 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 9ED6EC0A97; Tue, 9 Jul 2024 07:14:44 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 678F9C0A9B for ; Tue, 9 Jul 2024 07:14:36 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 3FC19815CE for ; Tue, 9 Jul 2024 07:14:36 +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 Hg2jSL0c9JcQ for ; Tue, 9 Jul 2024 07:14:34 +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 65A598161D 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 65A598161D 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 65A598161D for ; Tue, 9 Jul 2024 07:14:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720509273; 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=wGoUGlPVe2Vey78sLQF33iGkxIQW9u8G4InajdOKXFw=; b=TVvLJo2CvKEpxObyez3u5GgsmHucqrtCfnDNuDxUaxzoCqyv2SCnmKludYO5r3A++GT1eO Z4CEGqToYCor147ZO1mIHyVwrJ2RC+dyt7rrYMscKUdsL4ybFczqZudr+KQFuobaCjQsOF bq3Ar7VbN779QNzRnVFmi2nnfs+SXnk= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-606-BbNrobeYMhyCA4JLu_9D-g-1; Tue, 09 Jul 2024 03:14:31 -0400 X-MC-Unique: BbNrobeYMhyCA4JLu_9D-g-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3678f403afaso2831258f8f.0 for ; Tue, 09 Jul 2024 00:14:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720509270; x=1721114070; 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=wGoUGlPVe2Vey78sLQF33iGkxIQW9u8G4InajdOKXFw=; b=H50sIPFtxl8DPoZOgFQ4pj+4+6vYk03EpwrlH8pUFNnqq83YZHSBD+wvLE8z4azb3o Q3pd0Pl2/01cjNLLtSP7PtXdPd6rbaB7AWbQcN03aEK7h7+YUoERcE4Hzv1bK89agJhu Odaz1HvzPto+NOkZjK9xwXtVZJrTMsrZn45nYdReUP7P0OgmBUWBeI12sRPGXfjCV7rK s49rkGMsGExJcGOfgEhcVh5+BryTBJHVIuYBEu8d4ckHjwkth2IiozfDEPB95KGbXmO8 280EOuKAo1WjDwWCeHfpHKgaXJmC1JgJhWiqOuL7lnQWnp9Cu6F6mq21I8Bk0OqQQAvR JNWQ== X-Gm-Message-State: AOJu0YxxWHOabtF44aeAtChDLwHH4JiovfhNx1TVjIMZmXkpXkOO0xQu vV8x2MoPeqI2DqqHfqcrSNJS8Ct6W/k5GWYJwKXUQg3WWbWRs9c4kf0bLhVVtma+D2GUzXAEbw4 n0DhAqYVM+85s7tuURhM4nLKMGPXmc+oz2VpVkhIkp5K7bf4RJiOUU24utdwmS8U5o9uyfkjm5Q H0ZHrP6zqgKcrJX49XZ+D2tvyiCByy X-Received: by 2002:a5d:4947:0:b0:367:8f29:f7b8 with SMTP id ffacd0b85a97d-367ceacb45fmr1329768f8f.51.1720509270608; Tue, 09 Jul 2024 00:14:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGtsUFE2DIYmwNV+I6P9OidM9MykbT82v0lc819I4gt3hsSjaxRH+xGOZzaa6wzGE2/R0+++A== X-Received: by 2002:a5d:4947:0:b0:367:8f29:f7b8 with SMTP id ffacd0b85a97d-367ceacb45fmr1329748f8f.51.1720509270239; Tue, 09 Jul 2024 00:14:30 -0700 (PDT) Received: from positronik4lide.redhat.com ([87.122.57.92]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-367cdfab136sm1638049f8f.98.2024.07.09.00.14.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jul 2024 00:14:29 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org Date: Tue, 9 Jul 2024 09:14:22 +0200 Message-Id: <20240709071422.11492-7-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240709071422.11492-1-jmeng@redhat.com> References: <20240709071422.11492-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v14 6/6] ofproto: Add JSON output for 'dpif/show' command. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" 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 | 2 +- ofproto/ofproto-dpif.c | 126 ++++++++++++++++++++++++++++++++++++----- tests/ofproto-dpif.at | 40 +++++++++++++ 3 files changed, 153 insertions(+), 15 deletions(-) diff --git a/NEWS b/NEWS index feebae86e..d18693315 100644 --- a/NEWS +++ b/NEWS @@ -6,7 +6,7 @@ Post-v3.3.0 * 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. - * 'list-commands' now supports output in JSON format. + * 'dpif/show' and 'list-commands' now support output in JSON format. - Userspace datapath: * Conntrack now supports 'random' flag for selecting ports in a range while natting and 'persistent' flag for selection of the IP address diff --git a/ofproto/ofproto-dpif.c b/ofproto/ofproto-dpif.c index fcd7cd753..87dfb0043 100644 --- a/ofproto/ofproto-dpif.c +++ b/ofproto/ofproto-dpif.c @@ -28,6 +28,7 @@ #include "fail-open.h" #include "guarded-list.h" #include "hmapx.h" +#include "openvswitch/json.h" #include "lacp.h" #include "learn.h" #include "mac-learning.h" @@ -6519,19 +6520,108 @@ done: return changed; } -static void -dpif_show_backer(const struct dpif_backer *backer, struct ds *ds) +static struct json * +dpif_show_backer_json(struct json *backers, const struct dpif_backer *backer) { + struct json *json_backer = json_object_create(); + + /* Add datapath as new JSON object using its name as key. */ + json_object_put(backers, dpif_name(backer->dpif), json_backer); + + /* Add datapath's stats under "stats" key. */ + struct json *json_dp_stats = json_object_create(); + struct dpif_dp_stats dp_stats; + + dpif_get_dp_stats(backer->dpif, &dp_stats); + json_object_put_format(json_dp_stats, "hit", "%"PRIu64, dp_stats.n_hit); + json_object_put_format(json_dp_stats, "missed", "%"PRIu64, + dp_stats.n_missed); + json_object_put(json_backer, "stats", json_dp_stats); + + /* Add datapath's bridges under "bridges" key. */ + struct json *json_dp_bridges = json_object_create(); + + struct shash ofproto_shash = SHASH_INITIALIZER(&ofproto_shash); + free(get_ofprotos(&ofproto_shash)); + + struct shash_node *node; + SHASH_FOR_EACH (node, &ofproto_shash) { + struct ofproto_dpif *ofproto = node->data; + + if (ofproto->backer != backer) { + continue; + } + + /* Add bridge to "bridges" dictionary using its name as key. */ + struct json *json_ofproto = json_object_create(); + + /* Add bridge ports to the current bridge dictionary. */ + const struct shash_node *port; + SHASH_FOR_EACH (port, &ofproto->up.port_by_name) { + /* Add bridge port to a bridge's dict using port name as key. */ + struct json *json_ofproto_port = json_object_create(); + struct ofport *ofport = port->data; + + /* Add OpenFlow port associated with a bridge port. */ + json_object_put_format(json_ofproto_port, "ofport", "%"PRIu32, + ofport->ofp_port); + + /* Add bridge port number. */ + 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, "port_no", + "%"PRIu32, odp_port); + } else { + json_object_put_string(json_ofproto_port, "port_no", "none"); + } + + /* Add type of a bridge port. */ + json_object_put_string(json_ofproto_port, "type", + netdev_get_type(ofport->netdev)); + + /* Add config entries for a bridge port. */ + + struct smap config = SMAP_INITIALIZER(&config); + + if (!netdev_get_config(ofport->netdev, &config) + && smap_count(&config)) { + struct json *json_port_config = json_object_create(); + struct smap_node *cfg_node; + + SMAP_FOR_EACH (cfg_node, &config) { + json_object_put_string(json_port_config, cfg_node->key, + cfg_node->value); + } + json_object_put(json_ofproto_port, "config", json_port_config); + } + smap_destroy(&config); + + json_object_put(json_ofproto, netdev_get_name(ofport->netdev), + json_ofproto_port); + } /* End of bridge port(s). */ + + json_object_put(json_dp_bridges, ofproto->up.name, json_ofproto); + } /* End of bridge(s). */ + + shash_destroy(&ofproto_shash); + + json_object_put(json_backer, "bridges", json_dp_bridges); + return json_backer; +} + +static void +dpif_show_backer_text(const struct dpif_backer *backer, struct ds *ds) +{ + struct shash ofproto_shash = SHASH_INITIALIZER(&ofproto_shash); const struct shash_node **ofprotos; struct dpif_dp_stats dp_stats; - struct shash ofproto_shash; size_t i; dpif_get_dp_stats(backer->dpif, &dp_stats); ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n", dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed); - shash_init(&ofproto_shash); ofprotos = get_ofprotos(&ofproto_shash); for (i = 0; i < shash_count(&ofproto_shash); i++) { struct ofproto_dpif *ofproto = ofprotos[i]->data; @@ -6587,18 +6677,26 @@ 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) == UNIXCTL_OUTPUT_FMT_JSON) { + struct json *backers = json_object_create(); + const struct shash_node *backer; - backers = shash_sort(&all_dpif_backers); - for (i = 0; i < shash_count(&all_dpif_backers); i++) { - dpif_show_backer(backers[i]->data, &ds); + SHASH_FOR_EACH (backer, &all_dpif_backers) { + dpif_show_backer_json(backers, backer->data); + } + unixctl_command_reply_json(conn, backers); + } else { + const struct shash_node **backers = shash_sort(&all_dpif_backers); + struct ds ds = DS_EMPTY_INITIALIZER; + + for (int i = 0; i < shash_count(&all_dpif_backers); i++) { + dpif_show_backer_text(backers[i]->data, &ds); + } + free(backers); + + 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/ofproto-dpif.at b/tests/ofproto-dpif.at index 0b23fd6c5..30ef0468c 100644 --- a/tests/ofproto-dpif.at +++ b/tests/ofproto-dpif.at @@ -8879,6 +8879,46 @@ dummy@ovs-dummy: hit:0 missed:0 br1 65534/101: (dummy-internal) p3 3/3: (dummy) ]) + +AT_CHECK([ovs-appctl --format json --pretty dpif/show], [0], [dnl +[{ + "dummy@ovs-dummy": { + "bridges": { + "br0": { + "br0": { + "ofport": "65534", + "port_no": "100", + "type": "dummy-internal"}, + "p1": { + "config": { + "n_rxq": "1", + "n_txq": "1", + "numa_id": "0"}, + "ofport": "1", + "port_no": "1", + "type": "dummy-pmd"}, + "p2": { + "config": { + "n_rxq": "1", + "n_txq": "1", + "numa_id": "0"}, + "ofport": "2", + "port_no": "2", + "type": "dummy-pmd"}}, + "br1": { + "br1": { + "ofport": "65534", + "port_no": "101", + "type": "dummy-internal"}, + "p3": { + "ofport": "3", + "port_no": "3", + "type": "dummy"}}}, + "stats": { + "hit": "0", + "missed": "0"}}}] +]) + OVS_VSWITCHD_STOP AT_CLEANUP