2009-07-08 13:19:16 -07:00
|
|
|
|
/*
|
2016-03-07 15:13:15 -08:00
|
|
|
|
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Nicira, Inc.
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include "unixctl.h"
|
|
|
|
|
#include <errno.h>
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
#include <getopt.h>
|
2009-07-08 13:19:16 -07:00
|
|
|
|
#include <unistd.h>
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
#include "command-line.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
|
#include "coverage.h"
|
|
|
|
|
#include "dirs.h"
|
2016-03-03 10:20:46 -08:00
|
|
|
|
#include "openvswitch/dynamic-string.h"
|
2016-07-12 16:37:34 -05:00
|
|
|
|
#include "openvswitch/json.h"
|
2012-02-14 20:53:59 -08:00
|
|
|
|
#include "jsonrpc.h"
|
2016-03-25 14:10:21 -07:00
|
|
|
|
#include "openvswitch/list.h"
|
2017-11-03 13:53:53 +08:00
|
|
|
|
#include "openvswitch/poll-loop.h"
|
2016-07-12 16:37:34 -05:00
|
|
|
|
#include "openvswitch/shash.h"
|
2012-02-14 20:53:59 -08:00
|
|
|
|
#include "stream.h"
|
2014-03-28 08:19:59 -07:00
|
|
|
|
#include "stream-provider.h"
|
2010-05-25 15:49:26 -07:00
|
|
|
|
#include "svec.h"
|
2014-12-15 14:10:38 +01:00
|
|
|
|
#include "openvswitch/vlog.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2010-10-19 14:47:01 -07:00
|
|
|
|
VLOG_DEFINE_THIS_MODULE(unixctl);
|
coverage: Make the coverage counters catalog program-specific.
Until now, the collection of coverage counters supported by a given OVS
program was not specific to that program. That means that, for example,
even though ovs-dpctl does not have anything to do with mac_learning, it
still has a coverage counter for it. This is confusing, at best.
This commit fixes the problem on some systems, in particular on ones that
use GCC and the GNU linker. It uses the feature of the GNU linker
described in its manual as:
If an orphaned section's name is representable as a C identifier then
the linker will automatically see PROVIDE two symbols: __start_SECNAME
and __end_SECNAME, where SECNAME is the name of the section. These
indicate the start address and end address of the orphaned section
respectively.
Systems that don't support these features retain the earlier behavior.
This commit also fixes the annoyance that files that include coverage
counters must be listed on COVERAGE_FILES in lib/automake.mk.
This commit also fixes the annoyance that modifying any source file that
includes a coverage counter caused all programs that link against
libopenvswitch.a to relink, even programs that the source file was not
linked into. For example, modifying ofproto/ofproto.c (which includes
coverage counters) caused tests/test-aes128 to relink, even though
test-aes128 does not link again ofproto.o.
2010-11-01 14:14:27 -07:00
|
|
|
|
|
|
|
|
|
COVERAGE_DEFINE(unixctl_received);
|
|
|
|
|
COVERAGE_DEFINE(unixctl_replied);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
|
|
struct unixctl_command {
|
2011-12-02 15:29:19 -08:00
|
|
|
|
const char *usage;
|
|
|
|
|
int min_args, max_args;
|
2009-10-29 15:20:21 -07:00
|
|
|
|
unixctl_cb_func *cb;
|
|
|
|
|
void *aux;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct unixctl_conn {
|
2014-12-15 14:10:38 +01:00
|
|
|
|
struct ovs_list node;
|
2012-02-14 20:53:59 -08:00
|
|
|
|
struct jsonrpc *rpc;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
/* 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. */
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
|
|
|
|
|
enum unixctl_output_fmt fmt; /* Output format of current connection. */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Server for control connection. */
|
|
|
|
|
struct unixctl_server {
|
2012-02-14 20:53:59 -08:00
|
|
|
|
struct pstream *listener;
|
2014-12-15 14:10:38 +01:00
|
|
|
|
struct ovs_list conns;
|
2018-07-24 10:45:25 -07:00
|
|
|
|
char *path;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
|
|
|
|
|
|
|
|
|
|
static struct shash commands = SHASH_INITIALIZER(&commands);
|
|
|
|
|
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
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 "<unknown>";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
|
static void
|
2014-10-17 11:11:36 -07:00
|
|
|
|
unixctl_list_commands(struct unixctl_conn *conn, int argc OVS_UNUSED,
|
|
|
|
|
const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
2024-07-09 09:14:21 +02:00
|
|
|
|
if (unixctl_command_get_output_format(conn) == UNIXCTL_OUTPUT_FMT_JSON) {
|
|
|
|
|
struct json *json_commands = json_object_create();
|
|
|
|
|
const struct shash_node *node;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2024-07-09 09:14:21 +02:00
|
|
|
|
SHASH_FOR_EACH (node, &commands) {
|
|
|
|
|
const struct unixctl_command *command = node->data;
|
2010-05-25 15:49:26 -07:00
|
|
|
|
|
2024-07-09 09:14:21 +02:00
|
|
|
|
if (command->usage) {
|
|
|
|
|
json_object_put_string(json_commands, node->name,
|
|
|
|
|
command->usage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
ds_put_cstr(&ds, "The available commands are:\n");
|
2012-02-14 20:53:59 -08:00
|
|
|
|
|
2024-07-09 09:14:21 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
2020-06-17 14:16:08 -07:00
|
|
|
|
}
|
2024-07-09 09:14:21 +02:00
|
|
|
|
free(nodes);
|
2010-05-25 15:49:26 -07:00
|
|
|
|
|
2024-07-09 09:14:21 +02:00
|
|
|
|
unixctl_command_reply(conn, ds_cstr(&ds));
|
|
|
|
|
ds_destroy(&ds);
|
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2011-08-01 21:18:00 -07:00
|
|
|
|
static void
|
2011-12-02 15:29:19 -08:00
|
|
|
|
unixctl_version(struct unixctl_conn *conn, int argc OVS_UNUSED,
|
|
|
|
|
const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
|
2011-08-01 21:18:00 -07:00
|
|
|
|
{
|
2014-11-24 12:49:01 +01:00
|
|
|
|
unixctl_command_reply(conn, ovs_get_program_version());
|
2011-08-01 21:18:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-02 15:29:19 -08:00
|
|
|
|
/* 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
|
2020-06-17 14:16:08 -07:00
|
|
|
|
* "list-commands" output. (If 'usage' is NULL, then the command is hidden.)
|
2011-12-02 15:29:19 -08:00
|
|
|
|
*
|
2012-09-06 15:41:11 -07:00
|
|
|
|
* 'cb' is called when the command is received. It is passed an array
|
|
|
|
|
* containing the command name and arguments, plus a copy of 'aux'. Normally
|
|
|
|
|
* 'cb' should reply by calling unixctl_command_reply() or
|
|
|
|
|
* unixctl_command_reply_error() before it returns, but if the command cannot
|
|
|
|
|
* be handled immediately then it can defer the reply until later. A given
|
|
|
|
|
* connection can only process a single request at a time, so a reply must be
|
|
|
|
|
* made eventually to avoid blocking that connection. */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
void
|
2011-12-02 15:29:19 -08:00
|
|
|
|
unixctl_command_register(const char *name, const char *usage,
|
|
|
|
|
int min_args, int max_args,
|
|
|
|
|
unixctl_cb_func *cb, void *aux)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
|
|
|
|
struct unixctl_command *command;
|
2011-11-14 17:41:28 +09:00
|
|
|
|
struct unixctl_command *lookup = shash_find_data(&commands, name);
|
|
|
|
|
|
2012-11-06 13:14:55 -08:00
|
|
|
|
ovs_assert(!lookup || lookup->cb == cb);
|
2011-11-14 17:41:28 +09:00
|
|
|
|
|
|
|
|
|
if (lookup) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
|
|
command = xmalloc(sizeof *command);
|
2011-12-02 15:29:19 -08:00
|
|
|
|
command->usage = usage;
|
|
|
|
|
command->min_args = min_args;
|
|
|
|
|
command->max_args = max_args;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
command->cb = cb;
|
2009-10-29 15:20:21 -07:00
|
|
|
|
command->aux = aux;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
shash_add(&commands, name, command);
|
|
|
|
|
}
|
|
|
|
|
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
enum unixctl_output_fmt
|
|
|
|
|
unixctl_command_get_output_format(struct unixctl_conn *conn)
|
|
|
|
|
{
|
|
|
|
|
return conn->fmt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Takes ownership of the 'body'. */
|
2012-02-14 20:53:59 -08:00
|
|
|
|
static void
|
|
|
|
|
unixctl_command_reply__(struct unixctl_conn *conn,
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
bool success, struct json *body)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
2012-02-14 20:53:59 -08:00
|
|
|
|
struct jsonrpc_msg *reply;
|
|
|
|
|
|
|
|
|
|
COVERAGE_INC(unixctl_replied);
|
2012-11-06 13:14:55 -08:00
|
|
|
|
ovs_assert(conn->request_id);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
|
|
|
|
|
if (success) {
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
reply = jsonrpc_create_reply(body, conn->request_id);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
} else {
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
reply = jsonrpc_create_error(body, conn->request_id);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 15:13:15 -08:00
|
|
|
|
if (VLOG_IS_DBG_ENABLED()) {
|
|
|
|
|
char *id = json_to_string(conn->request_id, 0);
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
char *msg = json_to_string(body, JSSF_SORT);
|
|
|
|
|
|
2016-03-07 15:13:15 -08:00
|
|
|
|
VLOG_DBG("replying with %s, id=%s: \"%s\"",
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
success ? "success" : "error", id, msg);
|
|
|
|
|
free(msg);
|
2016-03-07 15:13:15 -08:00
|
|
|
|
free(id);
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
/* If jsonrpc_send() returns an error, the run loop will take care of the
|
|
|
|
|
* problem eventually. */
|
|
|
|
|
jsonrpc_send(conn->rpc, reply);
|
|
|
|
|
json_destroy(conn->request_id);
|
|
|
|
|
conn->request_id = NULL;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
/* Replies to the active unixctl connection 'conn'. 'result' is sent to the
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
* 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. */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
void
|
2012-02-14 20:53:59 -08:00
|
|
|
|
unixctl_command_reply(struct unixctl_conn *conn, const char *result)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
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);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
/* Replies to the active unixctl connection 'conn'. 'error' is sent to the
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
* 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. */
|
2012-02-14 20:53:59 -08:00
|
|
|
|
void
|
|
|
|
|
unixctl_command_reply_error(struct unixctl_conn *conn, const char *error)
|
|
|
|
|
{
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
unixctl_command_reply__(conn, false,
|
|
|
|
|
json_string_create(error ? error : ""));
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 08:19:59 -07:00
|
|
|
|
/* Creates a unixctl server listening on 'path', which for POSIX may be:
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
|
|
|
|
* - NULL, in which case <rundir>/<program>.<pid>.ctl is used.
|
|
|
|
|
*
|
|
|
|
|
* - A name that does not start with '/', in which case it is put in
|
|
|
|
|
* <rundir>.
|
|
|
|
|
*
|
|
|
|
|
* - An absolute path (starting with '/') that gives the exact name of
|
|
|
|
|
* the Unix domain socket to listen on.
|
|
|
|
|
*
|
2016-08-02 18:19:34 +00:00
|
|
|
|
* For Windows, a local named pipe is used. A file is created in 'path'
|
2014-03-28 08:19:59 -07:00
|
|
|
|
* which may be:
|
|
|
|
|
*
|
|
|
|
|
* - NULL, in which case <rundir>/<program>.ctl is used.
|
|
|
|
|
*
|
|
|
|
|
* - An absolute path that gives the name of the file.
|
|
|
|
|
*
|
|
|
|
|
* For both POSIX and Windows, if the path is "none", the function will
|
|
|
|
|
* return successfully but no socket will actually be created.
|
|
|
|
|
*
|
2009-07-08 13:19:16 -07:00
|
|
|
|
* A program that (optionally) daemonizes itself should call this function
|
|
|
|
|
* *after* daemonization, so that the socket name contains the pid of the
|
|
|
|
|
* daemon instead of the pid of the program that exited. (Otherwise,
|
2009-11-09 14:46:38 -08:00
|
|
|
|
* "ovs-appctl --target=<program>" will fail.)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
|
|
|
|
* Returns 0 if successful, otherwise a positive errno value. If successful,
|
2010-11-29 12:21:08 -08:00
|
|
|
|
* sets '*serverp' to the new unixctl_server (or to NULL if 'path' was "none"),
|
|
|
|
|
* otherwise to NULL. */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
int
|
|
|
|
|
unixctl_server_create(const char *path, struct unixctl_server **serverp)
|
|
|
|
|
{
|
2012-02-14 20:53:59 -08:00
|
|
|
|
*serverp = NULL;
|
2010-11-29 12:21:08 -08:00
|
|
|
|
if (path && !strcmp(path, "none")) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 10:45:25 -07:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
enum { WINDOWS = 1 };
|
2014-03-28 08:19:59 -07:00
|
|
|
|
#else
|
2018-07-24 10:45:25 -07:00
|
|
|
|
enum { WINDOWS = 0 };
|
2014-03-28 08:19:59 -07:00
|
|
|
|
#endif
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2018-07-24 10:45:25 -07:00
|
|
|
|
long int pid = getpid();
|
|
|
|
|
char *abs_path
|
|
|
|
|
= (path ? abs_file_name(ovs_rundir(), path)
|
|
|
|
|
: WINDOWS ? xasprintf("%s/%s.ctl", ovs_rundir(), program_name)
|
|
|
|
|
: xasprintf("%s/%s.%ld.ctl", ovs_rundir(), program_name, pid));
|
|
|
|
|
|
|
|
|
|
struct pstream *listener;
|
|
|
|
|
char *punix_path = xasprintf("punix:%s", abs_path);
|
|
|
|
|
int error = pstream_open(punix_path, &listener, 0);
|
|
|
|
|
free(punix_path);
|
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
if (error) {
|
2018-07-24 10:45:25 -07:00
|
|
|
|
ovs_error(error, "%s: could not initialize control socket", abs_path);
|
|
|
|
|
free(abs_path);
|
|
|
|
|
return error;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-17 11:11:36 -07:00
|
|
|
|
unixctl_command_register("list-commands", "", 0, 0, unixctl_list_commands,
|
|
|
|
|
NULL);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
unixctl_command_register("version", "", 0, 0, unixctl_version, NULL);
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
unixctl_command_register("set-options", "[--format text|json]", 1, 2,
|
|
|
|
|
unixctl_set_options, NULL);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2018-07-24 10:45:25 -07:00
|
|
|
|
struct unixctl_server *server = xmalloc(sizeof *server);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
server->listener = listener;
|
2018-07-24 10:45:25 -07:00
|
|
|
|
server->path = abs_path;
|
2016-03-25 14:10:22 -07:00
|
|
|
|
ovs_list_init(&server->conns);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*serverp = server;
|
2018-07-24 10:45:25 -07:00
|
|
|
|
return 0;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2012-02-14 20:53:59 -08:00
|
|
|
|
process_command(struct unixctl_conn *conn, struct jsonrpc_msg *request)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
2012-02-14 20:53:59 -08:00
|
|
|
|
char *error = NULL;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
|
|
struct unixctl_command *command;
|
2025-06-24 21:54:33 +02:00
|
|
|
|
const struct json *params;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
|
|
COVERAGE_INC(unixctl_received);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
conn->request_id = json_clone(request->id);
|
|
|
|
|
|
2016-03-07 15:13:15 -08:00
|
|
|
|
if (VLOG_IS_DBG_ENABLED()) {
|
|
|
|
|
char *params_s = json_to_string(request->params, 0);
|
|
|
|
|
char *id_s = json_to_string(request->id, 0);
|
|
|
|
|
VLOG_DBG("received request %s%s, id=%s",
|
|
|
|
|
request->method, params_s, id_s);
|
|
|
|
|
free(params_s);
|
|
|
|
|
free(id_s);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 21:54:33 +02:00
|
|
|
|
params = request->params;
|
2012-02-14 20:53:59 -08:00
|
|
|
|
command = shash_find_data(&commands, request->method);
|
|
|
|
|
if (!command) {
|
2017-10-14 12:47:46 -07:00
|
|
|
|
error = xasprintf("\"%s\" is not a valid command (use "
|
|
|
|
|
"\"list-commands\" to see a list of valid commands)",
|
|
|
|
|
request->method);
|
2025-06-24 21:54:33 +02:00
|
|
|
|
} else if (json_array_size(params) < command->min_args) {
|
2012-02-14 20:53:59 -08:00
|
|
|
|
error = xasprintf("\"%s\" command requires at least %d arguments",
|
|
|
|
|
request->method, command->min_args);
|
2025-06-24 21:54:33 +02:00
|
|
|
|
} else if (json_array_size(params) > command->max_args) {
|
2012-02-14 20:53:59 -08:00
|
|
|
|
error = xasprintf("\"%s\" command takes at most %d arguments",
|
|
|
|
|
request->method, command->max_args);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
} else {
|
2012-02-14 20:53:59 -08:00
|
|
|
|
struct svec argv = SVEC_EMPTY_INITIALIZER;
|
2025-06-24 21:54:33 +02:00
|
|
|
|
int i, n = json_array_size(params);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
|
|
|
|
|
svec_add(&argv, request->method);
|
2025-06-24 21:54:33 +02:00
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
|
const struct json *elem = json_array_at(params, i);
|
|
|
|
|
|
|
|
|
|
if (elem->type != JSON_STRING) {
|
2012-02-14 20:53:59 -08:00
|
|
|
|
error = xasprintf("\"%s\" command has non-string argument",
|
|
|
|
|
request->method);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-06-24 21:54:33 +02:00
|
|
|
|
svec_add(&argv, json_string(elem));
|
2012-02-14 20:53:59 -08:00
|
|
|
|
}
|
|
|
|
|
svec_terminate(&argv);
|
|
|
|
|
|
|
|
|
|
if (!error) {
|
2011-12-02 15:29:19 -08:00
|
|
|
|
command->cb(conn, argv.n, (const char **) argv.names,
|
|
|
|
|
command->aux);
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
svec_destroy(&argv);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
2011-12-02 15:29:19 -08:00
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
if (error) {
|
|
|
|
|
unixctl_command_reply_error(conn, error);
|
|
|
|
|
free(error);
|
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2012-02-14 20:53:59 -08:00
|
|
|
|
run_connection(struct unixctl_conn *conn)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
2012-02-14 20:53:59 -08:00
|
|
|
|
int error, i;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
jsonrpc_run(conn->rpc);
|
|
|
|
|
error = jsonrpc_get_status(conn->rpc);
|
|
|
|
|
if (error || jsonrpc_get_backlog(conn->rpc)) {
|
|
|
|
|
return error;
|
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
|
struct jsonrpc_msg *msg;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
if (error || conn->request_id) {
|
|
|
|
|
break;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
jsonrpc_recv(conn->rpc, &msg);
|
|
|
|
|
if (msg) {
|
|
|
|
|
if (msg->type == JSONRPC_REQUEST) {
|
|
|
|
|
process_command(conn, msg);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
} else {
|
2012-02-14 20:53:59 -08:00
|
|
|
|
VLOG_WARN_RL(&rl, "%s: received unexpected %s message",
|
|
|
|
|
jsonrpc_get_name(conn->rpc),
|
|
|
|
|
jsonrpc_msg_type_to_string(msg->type));
|
|
|
|
|
error = EINVAL;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
2012-02-14 20:53:59 -08:00
|
|
|
|
jsonrpc_msg_destroy(msg);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
2012-02-14 20:53:59 -08:00
|
|
|
|
error = error ? error : jsonrpc_get_status(conn->rpc);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
return error;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
kill_connection(struct unixctl_conn *conn)
|
|
|
|
|
{
|
2016-03-25 14:10:22 -07:00
|
|
|
|
ovs_list_remove(&conn->node);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
jsonrpc_close(conn->rpc);
|
|
|
|
|
json_destroy(conn->request_id);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
free(conn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
unixctl_server_run(struct unixctl_server *server)
|
|
|
|
|
{
|
2010-11-29 12:21:08 -08:00
|
|
|
|
if (!server) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-02 15:03:06 -07:00
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2012-02-14 20:53:59 -08:00
|
|
|
|
struct stream *stream;
|
|
|
|
|
int error;
|
|
|
|
|
|
|
|
|
|
error = pstream_accept(server->listener, &stream);
|
|
|
|
|
if (!error) {
|
|
|
|
|
struct unixctl_conn *conn = xzalloc(sizeof *conn);
|
2016-03-25 14:10:22 -07:00
|
|
|
|
ovs_list_push_back(&server->conns, &conn->node);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
conn->rpc = jsonrpc_open(stream);
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
conn->fmt = UNIXCTL_OUTPUT_FMT_TEXT;
|
2012-02-14 20:53:59 -08:00
|
|
|
|
} else if (error == EAGAIN) {
|
2009-07-08 13:19:16 -07:00
|
|
|
|
break;
|
2012-02-14 20:53:59 -08:00
|
|
|
|
} else {
|
|
|
|
|
VLOG_WARN_RL(&rl, "%s: accept failed: %s",
|
|
|
|
|
pstream_get_name(server->listener),
|
2013-06-24 10:54:49 -07:00
|
|
|
|
ovs_strerror(error));
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 12:56:14 +01:00
|
|
|
|
struct unixctl_conn *conn;
|
|
|
|
|
LIST_FOR_EACH_SAFE (conn, node, &server->conns) {
|
2009-07-08 13:19:16 -07:00
|
|
|
|
int error = run_connection(conn);
|
|
|
|
|
if (error && error != EAGAIN) {
|
|
|
|
|
kill_connection(conn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
unixctl_server_wait(struct unixctl_server *server)
|
|
|
|
|
{
|
|
|
|
|
struct unixctl_conn *conn;
|
|
|
|
|
|
2010-11-29 12:21:08 -08:00
|
|
|
|
if (!server) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
pstream_wait(server->listener);
|
2010-09-17 10:33:10 -07:00
|
|
|
|
LIST_FOR_EACH (conn, node, &server->conns) {
|
2012-02-14 20:53:59 -08:00
|
|
|
|
jsonrpc_wait(conn->rpc);
|
2018-11-29 10:37:02 -08:00
|
|
|
|
if (!jsonrpc_get_backlog(conn->rpc) && !conn->request_id) {
|
2012-02-14 20:53:59 -08:00
|
|
|
|
jsonrpc_recv_wait(conn->rpc);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Destroys 'server' and stops listening for connections. */
|
|
|
|
|
void
|
|
|
|
|
unixctl_server_destroy(struct unixctl_server *server)
|
|
|
|
|
{
|
|
|
|
|
if (server) {
|
2022-03-23 12:56:14 +01:00
|
|
|
|
struct unixctl_conn *conn;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2022-03-23 12:56:14 +01:00
|
|
|
|
LIST_FOR_EACH_SAFE (conn, node, &server->conns) {
|
2009-07-08 13:19:16 -07:00
|
|
|
|
kill_connection(conn);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 13:30:53 -07:00
|
|
|
|
free(server->path);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
pstream_close(server->listener);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
free(server);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-24 10:45:25 -07:00
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
unixctl_server_get_path(const struct unixctl_server *server)
|
|
|
|
|
{
|
|
|
|
|
return server ? server->path : NULL;
|
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2014-03-28 08:19:59 -07:00
|
|
|
|
/* On POSIX based systems, connects to a unixctl server socket. 'path' should
|
|
|
|
|
* be the name of a unixctl server socket. If it does not start with '/', it
|
|
|
|
|
* will be prefixed with the rundir (e.g. /usr/local/var/run/openvswitch).
|
|
|
|
|
*
|
2016-08-02 18:19:34 +00:00
|
|
|
|
* On Windows, connects to a local named pipe. A file which resides in
|
|
|
|
|
* 'path' is used to mimic the behavior of a Unix domain socket.
|
2014-03-28 08:19:59 -07:00
|
|
|
|
* 'path' should be an absolute path of the file.
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
|
|
|
|
* Returns 0 if successful, otherwise a positive errno value. If successful,
|
2012-02-14 20:53:59 -08:00
|
|
|
|
* sets '*client' to the new jsonrpc, otherwise to NULL. */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
int
|
2012-02-14 20:53:59 -08:00
|
|
|
|
unixctl_client_create(const char *path, struct jsonrpc **client)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
2012-02-14 20:53:59 -08:00
|
|
|
|
struct stream *stream;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
int error;
|
2014-03-28 08:19:59 -07:00
|
|
|
|
|
2018-07-24 09:58:56 -07:00
|
|
|
|
char *abs_path = abs_file_name(ovs_rundir(), path);
|
|
|
|
|
char *unix_path = xasprintf("unix:%s", abs_path);
|
2014-03-28 08:19:59 -07:00
|
|
|
|
|
|
|
|
|
*client = NULL;
|
|
|
|
|
|
2012-03-10 15:58:10 -08:00
|
|
|
|
error = stream_open_block(stream_open(unix_path, &stream, DSCP_DEFAULT),
|
2019-01-09 20:30:16 +03:00
|
|
|
|
-1, &stream);
|
2012-02-14 20:53:59 -08:00
|
|
|
|
free(unix_path);
|
|
|
|
|
free(abs_path);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
if (error) {
|
|
|
|
|
VLOG_WARN("failed to connect to %s", path);
|
|
|
|
|
return error;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
2012-02-14 20:53:59 -08:00
|
|
|
|
|
|
|
|
|
*client = jsonrpc_open(stream);
|
|
|
|
|
return 0;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
/* Executes 'command' on the server with an argument vector 'argv' containing
|
|
|
|
|
* 'argc' elements. If successfully communicated with the server, returns 0
|
|
|
|
|
* and sets '*result', or '*err' (not both) to the result or error the server
|
|
|
|
|
* returned. Otherwise, sets '*result' and '*err' to NULL and returns a
|
|
|
|
|
* positive errno value. The caller is responsible for freeing '*result' or
|
|
|
|
|
* '*err' if not NULL. */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
int
|
2012-02-14 20:53:59 -08:00
|
|
|
|
unixctl_client_transact(struct jsonrpc *client, const char *command, int argc,
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
char *argv[], struct json **result, struct json **err)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
2012-02-14 20:53:59 -08:00
|
|
|
|
struct jsonrpc_msg *request, *reply;
|
|
|
|
|
struct json **json_args, *params;
|
|
|
|
|
int error, i;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
*result = NULL;
|
|
|
|
|
*err = NULL;
|
|
|
|
|
|
|
|
|
|
json_args = xmalloc(argc * sizeof *json_args);
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
|
json_args[i] = json_string_create(argv[i]);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
2012-02-14 20:53:59 -08:00
|
|
|
|
params = json_array_create(json_args, argc);
|
|
|
|
|
request = jsonrpc_create_request(command, params, NULL);
|
|
|
|
|
|
|
|
|
|
error = jsonrpc_transact_block(client, request, &reply);
|
|
|
|
|
if (error) {
|
|
|
|
|
VLOG_WARN("error communicating with %s: %s", jsonrpc_get_name(client),
|
2013-01-24 13:46:23 -08:00
|
|
|
|
ovs_retval_to_string(error));
|
2012-02-14 20:53:59 -08:00
|
|
|
|
return error;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
Add global option for JSON output to ovs-appctl.
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 <code@jakobmeng.de>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
2024-07-09 09:14:17 +02:00
|
|
|
|
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);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-02-14 20:53:59 -08:00
|
|
|
|
jsonrpc_msg_destroy(reply);
|
|
|
|
|
return error;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|