Re: [ovs-dev] [PATCH v9 1/6] Add global option for JSON output to ovs-appctl.
On 4/12/24 09:26, jm...@redhat.com wrote: > From: Jakob Meng > > For monitoring systems such as Prometheus it would be beneficial if > OVS would expose statistics in a machine-readable format. > > This patch introduces support for different output formats to ovs-xxx > tools, in particular ovs-appctl. What other tools you plan to add support to? Database tools already have the machine-readable output from the database. appctl should generelly be used instead of ovs-dpctl. What else? I'd suggest to not try to make this generic. > The latter gains a global option > '-f,--format' which changes it to print a JSON document instead of > plain-text for humans. For example, a later patch implements support > for 'ovs-appctl --format json dpif/show'. By default, the output format > is plain-text as before. > > A new 'set-options' command has been added to lib/unixctl.c which > allows to change the output format of the commands executed afterwards > on the same socket connection. It is supposed to be run by ovs-xxx It's specific for appctl unility, nothing else is using unixctl. > tools transparently for the user when a specific output format has been > requested. > For example, when a user calls 'ovs-appctl --format json dpif/show', > then ovs-appctl will call 'set-options' to set the output format as > requested by the user and afterwards it will call the actual command > 'dpif/show'. > This ovs-appctl behaviour has been implemented in a backward compatible > way. One can use an updated client (ovs-appctl) with an old server > (ovs-vswitchd) and vice versa. Of course, JSON output only works when > both sides have been updated. > > Two access functions unixctl_command_{get,set}_output_format() and > two unixctl_command_reply_{,error_}json functions have been added to > lib/unixctl.h: > unixctl_command_get_output_format() is supposed to be used in commands > like 'dpif/show' to query the requested output format. When JSON output > has been selected, both unixctl_command_reply_{,error_}json() functions > can be used to return JSON objects to the client (ovs-appctl) instead > of plain-text with the unixctl_command_reply{,_error}() functions. > > When JSON has been requested but a command has not implemented JSON > output the plain-text output will be wrapped in a provisional JSON > document with the following structure: > > {"reply-format":"plain","reply":"$PLAIN_TEXT_HERE"} > > Thus commands which have been executed successfully will not fail when > they try to render the output at a later stage. > > In popular tools like kubectl the option for output control is usually > called '-o|--output' instead of '-f,--format'. But ovs-appctl already > has an short option '-o' which prints the available ovs-appctl options > ('--option'). The now choosen name also better alignes with > ovsdb-client where '-f,--format' controls output formatting. > > Reported-at: https://bugzilla.redhat.com/1824861 > Signed-off-by: Jakob Meng > --- > NEWS | 3 + > lib/command-line.c | 36 ++ > lib/command-line.h | 10 +++ > lib/unixctl.c | 158 - > lib/unixctl.h | 11 +++ > tests/ovs-vswitchd.at | 11 +++ > utilities/ovs-appctl.c | 98 + > 7 files changed, 277 insertions(+), 50 deletions(-) > > diff --git a/NEWS b/NEWS > index b92cec532..03ef6486b 100644 > --- a/NEWS > +++ b/NEWS > @@ -7,6 +7,9 @@ Post-v3.3.0 > - The primary development branch has been renamed from 'master' to 'main'. > The OVS tree remains hosted on GitHub. > https://github.com/openvswitch/ovs.git > + - ovs-appctl: 'o' goes before 't' or 'u', so I'd put this entry to the top. > + * Added new option [-f|--format] to choose the output format, e.g. > 'json' > + or 'text' (by default). A man page update is missing: Documentation/ref/ovs-appctl.8.rst > > > v3.3.0 - 16 Feb 2024 > diff --git a/lib/command-line.c b/lib/command-line.c > index 967f4f5d5..775e0430a 100644 > --- a/lib/command-line.c > +++ b/lib/command-line.c These changes should be moved to unixctl.[ch]. > @@ -24,6 +24,7 @@ > #include "ovs-thread.h" > #include "util.h" > #include "openvswitch/vlog.h" > +#include "openvswitch/json.h" > > VLOG_DEFINE_THIS_MODULE(command_line); > > @@ -53,6 +54,41 @@ ovs_cmdl_long_options_to_short_options(const struct option > options[]) > return xstrdup(short_options); > } > > +const char * > +ovs_output_fmt_to_string(enum ovs_output_fmt fmt) > +{ > +switch (fmt) { > +case OVS_OUTPUT_FMT_TEXT: > +return "text"; > + > +case OVS_OUTPUT_FMT_JSON: > +return "json"; > + > +default: > +return NULL; > +} > +} > + > +struct json * > +ovs_output_fmt_to_json(enum ovs_output_fmt fmt) > +{ > +const char *string = ovs_output_fmt_to_string(fmt); > +return string ? json_string_create(string) : NULL; > +} > + > +bool > +ovs_output_fmt_from_string(const cha
Re: [ovs-dev] [PATCH v9 1/6] Add global option for JSON output to ovs-appctl.
Bleep bloop. Greetings Jakob Meng, I am a robot and I have tried out your patch. Thanks for your contribution. I encountered some error that I wasn't expecting. See the details below. checkpatch: WARNING: Line lacks whitespace around operator WARNING: Line lacks whitespace around operator #583 FILE: utilities/ovs-appctl.c:145: -f, --format=FMT Output format. One of: 'json', or 'text'\n\ Lines checked: 675, Warnings: 2, Errors: 0 Please check this out. If you feel there has been an error, please email acon...@redhat.com Thanks, 0-day Robot ___ dev mailing list d...@openvswitch.org https://mail.openvswitch.org/mailman/listinfo/ovs-dev
[ovs-dev] [PATCH v9 1/6] Add global option for JSON output to ovs-appctl.
From: Jakob Meng For monitoring systems such as Prometheus it would be beneficial if OVS would expose statistics in a machine-readable format. This patch introduces support for different output formats to ovs-xxx tools, in particular ovs-appctl. The latter gains a global option '-f,--format' which changes it to print a JSON document instead of plain-text for humans. For example, a later patch implements support for 'ovs-appctl --format json dpif/show'. By default, the output format is plain-text as before. A new 'set-options' command has been added to lib/unixctl.c which allows to change the output format of the commands executed afterwards on the same socket connection. It is supposed to be run by ovs-xxx tools transparently for the user when a specific output format has been requested. For example, when a user calls 'ovs-appctl --format json dpif/show', then ovs-appctl will call 'set-options' to set the output format as requested by the user and afterwards it will call the actual command 'dpif/show'. This ovs-appctl behaviour has been implemented in a backward compatible way. One can use an updated client (ovs-appctl) with an old server (ovs-vswitchd) and vice versa. Of course, JSON output only works when both sides have been updated. Two access functions unixctl_command_{get,set}_output_format() and two unixctl_command_reply_{,error_}json functions have been added to lib/unixctl.h: unixctl_command_get_output_format() is supposed to be used in commands like 'dpif/show' to query the requested output format. When JSON output has been selected, both unixctl_command_reply_{,error_}json() functions can be used to return JSON objects to the client (ovs-appctl) instead of plain-text with the unixctl_command_reply{,_error}() functions. When JSON has been requested but a command has not implemented JSON output the plain-text output will be wrapped in a provisional JSON document with the following structure: {"reply-format":"plain","reply":"$PLAIN_TEXT_HERE"} Thus commands which have been executed successfully will not fail when they try to render the output at a later stage. In popular tools like kubectl the option for output control is usually called '-o|--output' instead of '-f,--format'. But ovs-appctl already has an short option '-o' which prints the available ovs-appctl options ('--option'). The now choosen name also better alignes with ovsdb-client where '-f,--format' controls output formatting. Reported-at: https://bugzilla.redhat.com/1824861 Signed-off-by: Jakob Meng --- NEWS | 3 + lib/command-line.c | 36 ++ lib/command-line.h | 10 +++ lib/unixctl.c | 158 - lib/unixctl.h | 11 +++ tests/ovs-vswitchd.at | 11 +++ utilities/ovs-appctl.c | 98 + 7 files changed, 277 insertions(+), 50 deletions(-) diff --git a/NEWS b/NEWS index b92cec532..03ef6486b 100644 --- a/NEWS +++ b/NEWS @@ -7,6 +7,9 @@ Post-v3.3.0 - The primary development branch has been renamed from 'master' to 'main'. The OVS tree remains hosted on GitHub. https://github.com/openvswitch/ovs.git + - ovs-appctl: + * Added new option [-f|--format] to choose the output format, e.g. 'json' + or 'text' (by default). v3.3.0 - 16 Feb 2024 diff --git a/lib/command-line.c b/lib/command-line.c index 967f4f5d5..775e0430a 100644 --- a/lib/command-line.c +++ b/lib/command-line.c @@ -24,6 +24,7 @@ #include "ovs-thread.h" #include "util.h" #include "openvswitch/vlog.h" +#include "openvswitch/json.h" VLOG_DEFINE_THIS_MODULE(command_line); @@ -53,6 +54,41 @@ ovs_cmdl_long_options_to_short_options(const struct option options[]) return xstrdup(short_options); } +const char * +ovs_output_fmt_to_string(enum ovs_output_fmt fmt) +{ +switch (fmt) { +case OVS_OUTPUT_FMT_TEXT: +return "text"; + +case OVS_OUTPUT_FMT_JSON: +return "json"; + +default: +return NULL; +} +} + +struct json * +ovs_output_fmt_to_json(enum ovs_output_fmt fmt) +{ +const char *string = ovs_output_fmt_to_string(fmt); +return string ? json_string_create(string) : NULL; +} + +bool +ovs_output_fmt_from_string(const char *string, enum ovs_output_fmt *fmt) +{ +if (!strcmp(string, "text")) { +*fmt = OVS_OUTPUT_FMT_TEXT; +} else if (!strcmp(string, "json")) { +*fmt = OVS_OUTPUT_FMT_JSON; +} else { +return false; +} +return true; +} + static char * OVS_WARN_UNUSED_RESULT build_short_options(const struct option *long_options) { diff --git a/lib/command-line.h b/lib/command-line.h index fc2a2690f..494274cec 100644 --- a/lib/command-line.h +++ b/lib/command-line.h @@ -20,6 +20,7 @@ /* Utilities for command-line parsing. */ #include "compiler.h" +#include struct option; @@ -46,6 +47,15 @@ struct ovs_cmdl_command { char *ovs_cmdl_long_options_to_short_options(const struct option *options); +enum ovs_output_fmt { +