Skip to content

Commit

Permalink
dpctl: Implement 'list-commands' function.
Browse files Browse the repository at this point in the history
This commit implements the 'list-commands' command for ovs-dpctl
and ovs-appctl dpctl/* commands.  The function will print the
usage string for each subcommand.

Signed-off-by: Alex Wang <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
yew011 committed Oct 29, 2014
1 parent 91a11f5 commit 56cad66
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
81 changes: 55 additions & 26 deletions lib/dpctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@
#include "unixctl.h"
#include "util.h"

typedef int dpctl_command_handler(int argc, const char *argv[],
struct dpctl_params *);
struct dpctl_command {
const char *name;
const char *usage;
int min_args;
int max_args;
dpctl_command_handler *handler;
};
static const struct dpctl_command *get_all_dpctl_commands(void);

static void
dpctl_puts(struct dpctl_params *dpctl_p, bool error, const char *string)
{
Expand Down Expand Up @@ -984,6 +995,27 @@ dpctl_help(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
if (dpctl_p->usage) {
dpctl_p->usage(dpctl_p->aux);
}

return 0;
}

static int
dpctl_list_commands(int argc OVS_UNUSED, const char *argv[] OVS_UNUSED,
struct dpctl_params *dpctl_p)
{
struct ds ds = DS_EMPTY_INITIALIZER;
const struct dpctl_command *commands = get_all_dpctl_commands();

ds_put_cstr(&ds, "The available commands are:\n");
for (; commands->name; commands++) {
const struct dpctl_command *c = commands;

ds_put_format(&ds, " %s%-23s %s\n", dpctl_p->is_appctl ? "dpctl/" : "",
c->name, c->usage);
}
dpctl_puts(dpctl_p, false, ds.string);
ds_destroy(&ds);

return 0;
}

Expand Down Expand Up @@ -1255,37 +1287,34 @@ dpctl_normalize_actions(int argc, const char *argv[],
return error;
}

typedef int dpctl_command_handler(int argc, const char *argv[],
struct dpctl_params *);
struct dpctl_command {
const char *name;
int min_args;
int max_args;
dpctl_command_handler *handler;
};

static const struct dpctl_command all_commands[] = {
{ "add-dp", 1, INT_MAX, dpctl_add_dp },
{ "del-dp", 1, 1, dpctl_del_dp },
{ "add-if", 2, INT_MAX, dpctl_add_if },
{ "del-if", 2, INT_MAX, dpctl_del_if },
{ "set-if", 2, INT_MAX, dpctl_set_if },
{ "dump-dps", 0, 0, dpctl_dump_dps },
{ "show", 0, INT_MAX, dpctl_show },
{ "dump-flows", 0, 2, dpctl_dump_flows },
{ "add-flow", 2, 3, dpctl_add_flow },
{ "mod-flow", 2, 3, dpctl_mod_flow },
{ "del-flow", 1, 2, dpctl_del_flow },
{ "del-flows", 0, 1, dpctl_del_flows },
{ "help", 0, INT_MAX, dpctl_help },
{ "add-dp", "add-dp dp [iface...]", 1, INT_MAX, dpctl_add_dp },
{ "del-dp", "del-dp dp", 1, 1, dpctl_del_dp },
{ "add-if", "add-if dp iface...", 2, INT_MAX, dpctl_add_if },
{ "del-if", "del-if dp iface...", 2, INT_MAX, dpctl_del_if },
{ "set-if", "set-if dp iface...", 2, INT_MAX, dpctl_set_if },
{ "dump-dps", "", 0, 0, dpctl_dump_dps },
{ "show", "[dp...]", 0, INT_MAX, dpctl_show },
{ "dump-flows", "[dp]", 0, 2, dpctl_dump_flows },
{ "add-flow", "add-flow [dp] flow actions", 2, 3, dpctl_add_flow },
{ "mod-flow", "mod-flow [dp] flow actions", 2, 3, dpctl_mod_flow },
{ "del-flow", "del-flow [dp] flow", 1, 2, dpctl_del_flow },
{ "del-flows", "[dp]", 0, 1, dpctl_del_flows },
{ "help", "", 0, INT_MAX, dpctl_help },
{ "list-commands", "", 0, INT_MAX, dpctl_list_commands },

/* Undocumented commands for testing. */
{ "parse-actions", 1, INT_MAX, dpctl_parse_actions },
{ "normalize-actions", 2, INT_MAX, dpctl_normalize_actions },
{ "parse-actions", "actions", 1, INT_MAX, dpctl_parse_actions },
{ "normalize-actions", "actions", 2, INT_MAX, dpctl_normalize_actions },

{ NULL, 0, 0, NULL },
{ NULL, NULL, 0, 0, NULL },
};

static const struct dpctl_command *get_all_dpctl_commands(void)
{
return all_commands;
}

/* Runs the command designated by argv[0] within the command table specified by
* 'commands', which must be terminated by a command whose 'name' member is a
* null pointer. */
Expand Down Expand Up @@ -1396,7 +1425,7 @@ dpctl_unixctl_handler(struct unixctl_conn *conn, int argc, const char *argv[],
}

if (!opt_parse_err) {
dpctl_p.usage = NULL;
dpctl_p.is_appctl = true;
dpctl_p.output = dpctl_unixctl_print;
dpctl_p.aux = &ds;

Expand Down
3 changes: 3 additions & 0 deletions lib/dpctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include "compiler.h"

struct dpctl_params {
/* True if it is called by ovs-appctl command. */
bool is_appctl;

/* -s, --statistics: Print port/flow statistics? */
bool print_statistics;

Expand Down
1 change: 1 addition & 0 deletions utilities/ovs-dpctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ main(int argc, char *argv[])
parse_options(argc, argv);
fatal_ignore_sigpipe();

dpctl_p.is_appctl = false;
dpctl_p.output = dpctl_print;
dpctl_p.usage = usage;

Expand Down

0 comments on commit 56cad66

Please sign in to comment.