From 1636c76112b63c50bb586186eb0c3aa16f9541c7 Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Tue, 17 Mar 2015 10:35:26 -0400 Subject: [PATCH] command-line: add ovs_cmdl_context I started working on a new command line utility that used this shared code. I wanted the ability to pass some data from common initialization code to all of the commands. You can find a similar pattern in ovs-vsctl. This patch updates the command handler to take a new struct, ovs_cmdl_context, instead of argc and argv directly. It includes argc and argv, but also includes an opaque type (void *), where the user of this API can attach its custom data it wants passed along to command handlers. This patch affected the ovstest sub-programs, as well. The patch includes a bit of an odd hack to OVSTEST_REGISTER() to avoid making the main() function of the sub-programs take a ovs_cmdl_context. The test main() functions still receive argc and argv directly, as that seems more natural. The test-subprograms themselves are able to make use of a context internally, though. Signed-off-by: Russell Bryant Signed-off-by: Ben Pfaff --- lib/command-line.c | 20 ++- lib/command-line.h | 16 +- ovsdb/ovsdb-tool.c | 63 ++++---- tests/ovstest.c | 10 +- tests/ovstest.h | 12 +- tests/test-bitmap.c | 13 +- tests/test-classifier.c | 28 ++-- tests/test-cmap.c | 23 +-- tests/test-heap.c | 23 +-- tests/test-jsonrpc.c | 33 ++-- tests/test-ovsdb.c | 216 +++++++++++++------------ tests/test-reconnect.c | 50 +++--- tests/test-util.c | 45 +++--- tests/test-vconn.c | 39 +++-- utilities/ovs-benchmark.c | 13 +- utilities/ovs-ofctl.c | 329 +++++++++++++++++++------------------- 16 files changed, 504 insertions(+), 429 deletions(-) diff --git a/lib/command-line.c b/lib/command-line.c index d9cec0db7..7b668a139 100644 --- a/lib/command-line.c +++ b/lib/command-line.c @@ -92,19 +92,25 @@ ovs_cmdl_print_options(const struct option options[]) * null pointer. * * Command-line options should be stripped off, so that a typical invocation - * looks like "run_command(argc - optind, argv + optind, my_commands);". */ + * looks like: + * struct ovs_cmdl_context ctx = { + * .argc = argc - optind, + * .argv = argv + optind, + * }; + * ovs_cmdl_run_command(&ctx, my_commands); + * */ void -ovs_cmdl_run_command(int argc, char *argv[], const struct ovs_cmdl_command commands[]) +ovs_cmdl_run_command(struct ovs_cmdl_context *ctx, const struct ovs_cmdl_command commands[]) { const struct ovs_cmdl_command *p; - if (argc < 1) { + if (ctx->argc < 1) { ovs_fatal(0, "missing command name; use --help for help"); } for (p = commands; p->name != NULL; p++) { - if (!strcmp(p->name, argv[0])) { - int n_arg = argc - 1; + if (!strcmp(p->name, ctx->argv[0])) { + int n_arg = ctx->argc - 1; if (n_arg < p->min_args) { VLOG_FATAL( "'%s' command requires at least %d arguments", p->name, p->min_args); @@ -112,7 +118,7 @@ ovs_cmdl_run_command(int argc, char *argv[], const struct ovs_cmdl_command comma VLOG_FATAL("'%s' command takes at most %d arguments", p->name, p->max_args); } else { - p->handler(argc, argv); + p->handler(ctx); if (ferror(stdout)) { VLOG_FATAL("write to stdout failed"); } @@ -124,7 +130,7 @@ ovs_cmdl_run_command(int argc, char *argv[], const struct ovs_cmdl_command comma } } - VLOG_FATAL("unknown command '%s'; use --help for help", argv[0]); + VLOG_FATAL("unknown command '%s'; use --help for help", ctx->argv[0]); } /* Process title. */ diff --git a/lib/command-line.h b/lib/command-line.h index b6da20585..e9e3b7bec 100644 --- a/lib/command-line.h +++ b/lib/command-line.h @@ -23,18 +23,30 @@ struct option; +/* Command handler context */ +struct ovs_cmdl_context { + /* number of command line arguments */ + int argc; + /* array of command line arguments */ + char **argv; + /* private context data defined by the API user */ + void *pvt; +}; + +typedef void (*ovs_cmdl_handler)(struct ovs_cmdl_context *); + struct ovs_cmdl_command { const char *name; const char *usage; int min_args; int max_args; - void (*handler)(int argc, char *argv[]); + ovs_cmdl_handler handler; }; char *ovs_cmdl_long_options_to_short_options(const struct option *options); void ovs_cmdl_print_options(const struct option *options); void ovs_cmdl_print_commands(const struct ovs_cmdl_command *commands); -void ovs_cmdl_run_command(int argc, char *argv[], const struct ovs_cmdl_command[]); +void ovs_cmdl_run_command(struct ovs_cmdl_context *, const struct ovs_cmdl_command[]); void ovs_cmdl_proctitle_init(int argc, char **argv); #if defined(__FreeBSD__) || defined(__NetBSD__) diff --git a/ovsdb/ovsdb-tool.c b/ovsdb/ovsdb-tool.c index f6487070c..32883e20c 100644 --- a/ovsdb/ovsdb-tool.c +++ b/ovsdb/ovsdb-tool.c @@ -55,10 +55,13 @@ static const char *default_schema(void); int main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { .argc = 0, }; set_program_name(argv[0]); parse_options(argc, argv); fatal_ignore_sigpipe(); - ovs_cmdl_run_command(argc - optind, argv + optind, get_all_commands()); + ctx.argc = argc - optind; + ctx.argv = argv + optind; + ovs_cmdl_run_command(&ctx, get_all_commands()); return 0; } @@ -187,10 +190,10 @@ check_ovsdb_error(struct ovsdb_error *error) } static void -do_create(int argc, char *argv[]) +do_create(struct ovs_cmdl_context *ctx) { - const char *db_file_name = argc >= 2 ? argv[1] : default_db(); - const char *schema_file_name = argc >= 3 ? argv[2] : default_schema(); + const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db(); + const char *schema_file_name = ctx->argc >= 3 ? ctx->argv[2] : default_schema(); struct ovsdb_schema *schema; struct ovsdb_log *log; struct json *json; @@ -272,20 +275,20 @@ compact_or_convert(const char *src_name_, const char *dst_name_, } static void -do_compact(int argc, char *argv[]) +do_compact(struct ovs_cmdl_context *ctx) { - const char *db = argc >= 2 ? argv[1] : default_db(); - const char *target = argc >= 3 ? argv[2] : NULL; + const char *db = ctx->argc >= 2 ? ctx->argv[1] : default_db(); + const char *target = ctx->argc >= 3 ? ctx->argv[2] : NULL; compact_or_convert(db, target, NULL, "compacted by ovsdb-tool "VERSION); } static void -do_convert(int argc, char *argv[]) +do_convert(struct ovs_cmdl_context *ctx) { - const char *db = argc >= 2 ? argv[1] : default_db(); - const char *schema = argc >= 3 ? argv[2] : default_schema(); - const char *target = argc >= 4 ? argv[3] : NULL; + const char *db = ctx->argc >= 2 ? ctx->argv[1] : default_db(); + const char *schema = ctx->argc >= 3 ? ctx->argv[2] : default_schema(); + const char *target = ctx->argc >= 4 ? ctx->argv[3] : NULL; struct ovsdb_schema *new_schema; check_ovsdb_error(ovsdb_schema_from_file(schema, &new_schema)); @@ -295,10 +298,10 @@ do_convert(int argc, char *argv[]) } static void -do_needs_conversion(int argc, char *argv[]) +do_needs_conversion(struct ovs_cmdl_context *ctx) { - const char *db_file_name = argc >= 2 ? argv[1] : default_db(); - const char *schema_file_name = argc >= 3 ? argv[2] : default_schema(); + const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db(); + const char *schema_file_name = ctx->argc >= 3 ? ctx->argv[2] : default_schema(); struct ovsdb_schema *schema1, *schema2; check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema1)); @@ -309,9 +312,9 @@ do_needs_conversion(int argc, char *argv[]) } static void -do_db_version(int argc, char *argv[]) +do_db_version(struct ovs_cmdl_context *ctx) { - const char *db_file_name = argc >= 2 ? argv[1] : default_db(); + const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db(); struct ovsdb_schema *schema; check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema)); @@ -320,9 +323,9 @@ do_db_version(int argc, char *argv[]) } static void -do_db_cksum(int argc OVS_UNUSED, char *argv[]) +do_db_cksum(struct ovs_cmdl_context *ctx) { - const char *db_file_name = argc >= 2 ? argv[1] : default_db(); + const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db(); struct ovsdb_schema *schema; check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema)); @@ -331,9 +334,9 @@ do_db_cksum(int argc OVS_UNUSED, char *argv[]) } static void -do_schema_version(int argc, char *argv[]) +do_schema_version(struct ovs_cmdl_context *ctx) { - const char *schema_file_name = argc >= 2 ? argv[1] : default_schema(); + const char *schema_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_schema(); struct ovsdb_schema *schema; check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema)); @@ -342,9 +345,9 @@ do_schema_version(int argc, char *argv[]) } static void -do_schema_cksum(int argc, char *argv[]) +do_schema_cksum(struct ovs_cmdl_context *ctx) { - const char *schema_file_name = argc >= 2 ? argv[1] : default_schema(); + const char *schema_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_schema(); struct ovsdb_schema *schema; check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema)); @@ -371,15 +374,15 @@ transact(bool read_only, int argc, char *argv[]) } static void -do_query(int argc, char *argv[]) +do_query(struct ovs_cmdl_context *ctx) { - transact(true, argc, argv); + transact(true, ctx->argc, ctx->argv); } static void -do_transact(int argc, char *argv[]) +do_transact(struct ovs_cmdl_context *ctx) { - transact(false, argc, argv); + transact(false, ctx->argc, ctx->argv); } static void @@ -495,9 +498,9 @@ print_db_changes(struct shash *tables, struct shash *names, } static void -do_show_log(int argc, char *argv[]) +do_show_log(struct ovs_cmdl_context *ctx) { - const char *db_file_name = argc >= 2 ? argv[1] : default_db(); + const char *db_file_name = ctx->argc >= 2 ? ctx->argv[1] : default_db(); struct shash names; struct ovsdb_log *log; struct ovsdb_schema *schema; @@ -558,13 +561,13 @@ do_show_log(int argc, char *argv[]) } static void -do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_help(struct ovs_cmdl_context *ctx OVS_UNUSED) { usage(); } static void -do_list_commands(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_list_commands(struct ovs_cmdl_context *ctx OVS_UNUSED) { ovs_cmdl_print_commands(get_all_commands()); } diff --git a/tests/ovstest.c b/tests/ovstest.c index f05da0584..3f3430df4 100644 --- a/tests/ovstest.c +++ b/tests/ovstest.c @@ -60,7 +60,7 @@ flush_help_string(struct ds *ds) } static void -help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +help(struct ovs_cmdl_context *ctx OVS_UNUSED) { const struct ovs_cmdl_command *p; struct ds test_names = DS_EMPTY_INITIALIZER; @@ -92,7 +92,7 @@ add_top_level_commands(void) } void -ovstest_register(const char *test_name, ovstest_func f) +ovstest_register(const char *test_name, ovs_cmdl_handler f) { struct ovs_cmdl_command test_cmd; @@ -125,7 +125,11 @@ main(int argc, char *argv[]) add_top_level_commands(); if (argc > 1) { - ovs_cmdl_run_command(argc - 1, argv + 1, commands); + struct ovs_cmdl_context ctx = { + .argc = argc - 1, + .argv = argv + 1, + }; + ovs_cmdl_run_command(&ctx, commands); } cleanup(); diff --git a/tests/ovstest.h b/tests/ovstest.h index 520b642a3..ddd7f3eb9 100644 --- a/tests/ovstest.h +++ b/tests/ovstest.h @@ -19,6 +19,8 @@ #include "compiler.h" +#include "command-line.h" + /* Overview * ======== * @@ -41,7 +43,8 @@ */ typedef void (*ovstest_func)(int argc, char *argv[]); -void ovstest_register(const char *test_name, ovstest_func f); + +void ovstest_register(const char *test_name, ovs_cmdl_handler f); /* Usage * ===== @@ -72,8 +75,13 @@ void ovstest_register(const char *test_name, ovstest_func f); * OVSTEST_REGISTER("my-test", my_test_main); */ #define OVSTEST_REGISTER(name, function) \ + static void \ + ovstest_wrapper_##function##__(struct ovs_cmdl_context *ctx) \ + { \ + function(ctx->argc, ctx->argv); \ + } \ OVS_CONSTRUCTOR(register_##function) { \ - ovstest_register(name, function); \ + ovstest_register(name, ovstest_wrapper_##function##__); \ } #endif diff --git a/tests/test-bitmap.c b/tests/test-bitmap.c index 9c09e1491..3dbc8df6f 100644 --- a/tests/test-bitmap.c +++ b/tests/test-bitmap.c @@ -121,7 +121,7 @@ run_test(void (*function)(void)) } static void -run_tests(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +run_tests(struct ovs_cmdl_context *ctx OVS_UNUSED) { run_test(test_bitmap_equal); run_test(test_bitmap_scan); @@ -129,9 +129,9 @@ run_tests(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -run_benchmarks(int argc OVS_UNUSED, char *argv[]) +run_benchmarks(struct ovs_cmdl_context *ctx) { - int n_iter = strtol(argv[1], NULL, 10); + int n_iter = strtol(ctx->argv[1], NULL, 10); struct timeval start; xgettimeofday(&start); @@ -157,8 +157,13 @@ static const struct ovs_cmdl_command commands[] = { static void test_bitmap_main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { + .argc = argc - 1, + .argv = argv + 1, + }; + set_program_name(argv[0]); - ovs_cmdl_run_command(argc - 1, argv + 1, commands); + ovs_cmdl_run_command(&ctx, commands); } OVSTEST_REGISTER("test-bitmap", test_bitmap_main); diff --git a/tests/test-classifier.c b/tests/test-classifier.c index 5d0e875c1..a615438c0 100644 --- a/tests/test-classifier.c +++ b/tests/test-classifier.c @@ -695,7 +695,7 @@ set_prefix_fields(struct classifier *cls) /* Tests an empty classifier. */ static void -test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_empty(struct ovs_cmdl_context *ctx OVS_UNUSED) { struct classifier cls; struct tcls tcls; @@ -712,14 +712,14 @@ test_empty(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) /* Destroys a null classifier. */ static void -test_destroy_null(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_destroy_null(struct ovs_cmdl_context *ctx OVS_UNUSED) { classifier_destroy(NULL); } /* Tests classification with one rule at a time. */ static void -test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_single_rule(struct ovs_cmdl_context *ctx OVS_UNUSED) { unsigned int wc_fields; /* Hilarious. */ @@ -754,7 +754,7 @@ test_single_rule(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) /* Tests replacing one rule by another. */ static void -test_rule_replacement(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_rule_replacement(struct ovs_cmdl_context *ctx OVS_UNUSED) { unsigned int wc_fields; @@ -849,7 +849,7 @@ next_permutation(int *a, int n) /* Tests classification with rules that have the same matching criteria. */ static void -test_many_rules_in_one_list (int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_many_rules_in_one_list (struct ovs_cmdl_context *ctx OVS_UNUSED) { enum { N_RULES = 3 }; int n_pris; @@ -968,7 +968,7 @@ array_contains(int *array, int n, int value) /* Tests classification with two rules at a time that fall into the same * table but different lists. */ static void -test_many_rules_in_one_table(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_many_rules_in_one_table(struct ovs_cmdl_context *ctx OVS_UNUSED) { int iteration; @@ -1090,13 +1090,13 @@ test_many_rules_in_n_tables(int n_tables) } static void -test_many_rules_in_two_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_many_rules_in_two_tables(struct ovs_cmdl_context *ctx OVS_UNUSED) { test_many_rules_in_n_tables(2); } static void -test_many_rules_in_five_tables(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_many_rules_in_five_tables(struct ovs_cmdl_context *ctx OVS_UNUSED) { test_many_rules_in_n_tables(5); } @@ -1256,7 +1256,7 @@ wildcard_extra_bits(struct flow_wildcards *mask) } static void -test_miniflow(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_miniflow(struct ovs_cmdl_context *ctx OVS_UNUSED) { struct flow flow; unsigned int idx; @@ -1322,7 +1322,7 @@ test_miniflow(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -test_minimask_has_extra(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_minimask_has_extra(struct ovs_cmdl_context *ctx OVS_UNUSED) { struct flow_wildcards catchall; struct minimask minicatchall; @@ -1360,7 +1360,7 @@ test_minimask_has_extra(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -test_minimask_combine(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_minimask_combine(struct ovs_cmdl_context *ctx OVS_UNUSED) { struct flow_wildcards catchall; struct minimask minicatchall; @@ -1422,9 +1422,13 @@ static const struct ovs_cmdl_command commands[] = { static void test_classifier_main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { + .argc = argc - 1, + .argv = argv + 1, + }; set_program_name(argv[0]); init_values(); - ovs_cmdl_run_command(argc - 1, argv + 1, commands); + ovs_cmdl_run_command(&ctx, commands); } OVSTEST_REGISTER("test-classifier", test_classifier_main); diff --git a/tests/test-cmap.c b/tests/test-cmap.c index f429cdd8e..74037f086 100644 --- a/tests/test-cmap.c +++ b/tests/test-cmap.c @@ -249,12 +249,12 @@ run_test(void (*function)(hash_func *)) } static void -run_tests(int argc, char *argv[]) +run_tests(struct ovs_cmdl_context *ctx) { int n; int i; - n = argc >= 2 ? atoi(argv[1]) : 100; + n = ctx->argc >= 2 ? atoi(ctx->argv[1]) : 100; for (i = 0; i < n; i++) { run_test(test_cmap_insert_replace_delete); } @@ -282,12 +282,12 @@ elapsed(const struct timeval *start) } static void -run_benchmarks(int argc, char *argv[] OVS_UNUSED) +run_benchmarks(struct ovs_cmdl_context *ctx) { - n_elems = strtol(argv[1], NULL, 10); - n_threads = strtol(argv[2], NULL, 10); - mutation_frac = strtod(argv[3], NULL) / 100.0 * UINT32_MAX; - n_batch = argc > 4 ? strtol(argv[4], NULL, 10) : 1; + n_elems = strtol(ctx->argv[1], NULL, 10); + n_threads = strtol(ctx->argv[2], NULL, 10); + mutation_frac = strtod(ctx->argv[3], NULL) / 100.0 * UINT32_MAX; + n_batch = ctx->argc > 4 ? strtol(ctx->argv[4], NULL, 10) : 1; if (n_batch > N_BATCH_MAX) { n_batch = N_BATCH_MAX; @@ -643,10 +643,15 @@ static const struct ovs_cmdl_command commands[] = { }; static void -test_cmap_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_cmap_main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { + .argc = argc - optind, + .argv = argv + optind, + }; + set_program_name(argv[0]); - ovs_cmdl_run_command(argc - optind, argv + optind, commands); + ovs_cmdl_run_command(&ctx, commands); } OVSTEST_REGISTER("test-cmap", test_cmap_main); diff --git a/tests/test-heap.c b/tests/test-heap.c index 5340860c7..6dab22b51 100644 --- a/tests/test-heap.c +++ b/tests/test-heap.c @@ -273,8 +273,7 @@ test_insert_delete_raw__(struct element *elements, } static void -test_heap_insert_delete_same_order(int argc OVS_UNUSED, - char *argv[] OVS_UNUSED) +test_heap_insert_delete_same_order(struct ovs_cmdl_context *ctx OVS_UNUSED) { enum { N_ELEMS = 7 }; @@ -297,8 +296,7 @@ test_heap_insert_delete_same_order(int argc OVS_UNUSED, } static void -test_heap_insert_delete_reverse_order(int argc OVS_UNUSED, - char *argv[] OVS_UNUSED) +test_heap_insert_delete_reverse_order(struct ovs_cmdl_context *ctx OVS_UNUSED) { enum { N_ELEMS = 7 }; @@ -327,8 +325,7 @@ test_heap_insert_delete_reverse_order(int argc OVS_UNUSED, } static void -test_heap_insert_delete_every_order(int argc OVS_UNUSED, - char *argv[] OVS_UNUSED) +test_heap_insert_delete_every_order(struct ovs_cmdl_context *ctx OVS_UNUSED) { enum { N_ELEMS = 5 }; @@ -363,8 +360,7 @@ test_heap_insert_delete_every_order(int argc OVS_UNUSED, } static void -test_heap_insert_delete_same_order_with_dups(int argc OVS_UNUSED, - char *argv[] OVS_UNUSED) +test_heap_insert_delete_same_order_with_dups(struct ovs_cmdl_context *ctx OVS_UNUSED) { enum { N_ELEMS = 7 }; @@ -410,7 +406,7 @@ test_heap_insert_delete_same_order_with_dups(int argc OVS_UNUSED, } static void -test_heap_raw_insert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_heap_raw_insert(struct ovs_cmdl_context *ctx OVS_UNUSED) { enum { N_ELEMS = 7 }; @@ -436,7 +432,7 @@ test_heap_raw_insert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -test_heap_raw_delete(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_heap_raw_delete(struct ovs_cmdl_context *ctx OVS_UNUSED) { enum { N_ELEMS = 16 }; @@ -480,9 +476,14 @@ static const struct ovs_cmdl_command commands[] = { static void test_heap_main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { + .argc = argc - 1, + .argv = argv + 1, + }; + set_program_name(argv[0]); - ovs_cmdl_run_command(argc - 1, argv + 1, commands); + ovs_cmdl_run_command(&ctx, commands); } OVSTEST_REGISTER("test-heap", test_heap_main); diff --git a/tests/test-jsonrpc.c b/tests/test-jsonrpc.c index d44543224..f66dc651b 100644 --- a/tests/test-jsonrpc.c +++ b/tests/test-jsonrpc.c @@ -40,11 +40,14 @@ static struct ovs_cmdl_command *get_all_commands(void); static void test_jsonrpc_main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { .argc = 0, }; ovs_cmdl_proctitle_init(argc, argv); set_program_name(argv[0]); service_start(&argc, &argv); parse_options(argc, argv); - ovs_cmdl_run_command(argc - optind, argv + optind, get_all_commands()); + ctx.argc = argc - optind; + ctx.argv = argv + optind; + ovs_cmdl_run_command(&ctx, get_all_commands()); } static void @@ -166,7 +169,7 @@ handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done) } static void -do_listen(int argc OVS_UNUSED, char *argv[]) +do_listen(struct ovs_cmdl_context *ctx) { struct pstream *pstream; struct jsonrpc **rpcs; @@ -174,9 +177,9 @@ do_listen(int argc OVS_UNUSED, char *argv[]) bool done; int error; - error = jsonrpc_pstream_open(argv[1], &pstream, DSCP_DEFAULT); + error = jsonrpc_pstream_open(ctx->argv[1], &pstream, DSCP_DEFAULT); if (error) { - ovs_fatal(error, "could not listen on \"%s\"", argv[1]); + ovs_fatal(error, "could not listen on \"%s\"", ctx->argv[1]); } daemonize(); @@ -249,7 +252,7 @@ do_listen(int argc OVS_UNUSED, char *argv[]) } static void -do_request(int argc OVS_UNUSED, char *argv[]) +do_request(struct ovs_cmdl_context *ctx) { struct jsonrpc_msg *msg; struct jsonrpc *rpc; @@ -259,18 +262,18 @@ do_request(int argc OVS_UNUSED, char *argv[]) char *string; int error; - method = argv[2]; - params = parse_json(argv[3]); + method = ctx->argv[2]; + params = parse_json(ctx->argv[3]); msg = jsonrpc_create_request(method, params, NULL); string = jsonrpc_msg_is_valid(msg); if (string) { ovs_fatal(0, "not a valid JSON-RPC request: %s", string); } - error = stream_open_block(jsonrpc_stream_open(argv[1], &stream, + error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream, DSCP_DEFAULT), &stream); if (error) { - ovs_fatal(error, "could not open \"%s\"", argv[1]); + ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]); } rpc = jsonrpc_open(stream); @@ -289,7 +292,7 @@ do_request(int argc OVS_UNUSED, char *argv[]) } static void -do_notify(int argc OVS_UNUSED, char *argv[]) +do_notify(struct ovs_cmdl_context *ctx) { struct jsonrpc_msg *msg; struct jsonrpc *rpc; @@ -299,18 +302,18 @@ do_notify(int argc OVS_UNUSED, char *argv[]) char *string; int error; - method = argv[2]; - params = parse_json(argv[3]); + method = ctx->argv[2]; + params = parse_json(ctx->argv[3]); msg = jsonrpc_create_notify(method, params); string = jsonrpc_msg_is_valid(msg); if (string) { ovs_fatal(0, "not a JSON RPC-valid notification: %s", string); } - error = stream_open_block(jsonrpc_stream_open(argv[1], &stream, + error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream, DSCP_DEFAULT), &stream); if (error) { - ovs_fatal(error, "could not open \"%s\"", argv[1]); + ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]); } rpc = jsonrpc_open(stream); @@ -322,7 +325,7 @@ do_notify(int argc OVS_UNUSED, char *argv[]) } static void -do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_help(struct ovs_cmdl_context *ctx OVS_UNUSED) { usage(); } diff --git a/tests/test-ovsdb.c b/tests/test-ovsdb.c index 540dcda16..791376304 100644 --- a/tests/test-ovsdb.c +++ b/tests/test-ovsdb.c @@ -58,9 +58,12 @@ static struct ovs_cmdl_command *get_all_commands(void); int main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { .argc = 0, }; set_program_name(argv[0]); parse_options(argc, argv); - ovs_cmdl_run_command(argc - optind, argv + optind, get_all_commands()); + ctx.argc = argc - optind; + ctx.argv = argv + optind; + ovs_cmdl_run_command(&ctx, get_all_commands()); return 0; } @@ -256,10 +259,10 @@ die_if_error(char *error) /* Command implementations. */ static void -do_log_io(int argc, char *argv[]) +do_log_io(struct ovs_cmdl_context *ctx) { - const char *name = argv[1]; - char *mode_string = argv[2]; + const char *name = ctx->argv[1]; + char *mode_string = ctx->argv[2]; struct ovsdb_error *error; enum ovsdb_log_open_mode mode; @@ -279,8 +282,8 @@ do_log_io(int argc, char *argv[]) check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log)); printf("%s: open successful\n", name); - for (i = 3; i < argc; i++) { - const char *command = argv[i]; + for (i = 3; i < ctx->argc; i++) { + const char *command = ctx->argv[i]; if (!strcmp(command, "read")) { struct json *json; @@ -317,7 +320,7 @@ do_log_io(int argc, char *argv[]) } static void -do_default_atoms(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_default_atoms(struct ovs_cmdl_context *ctx OVS_UNUSED) { int type; @@ -342,7 +345,7 @@ do_default_atoms(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -do_default_data(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_default_data(struct ovs_cmdl_context *ctx OVS_UNUSED) { unsigned int n_min; int key, value; @@ -382,24 +385,24 @@ do_default_data(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -do_parse_atomic_type(int argc OVS_UNUSED, char *argv[]) +do_parse_atomic_type(struct ovs_cmdl_context *ctx) { enum ovsdb_atomic_type type; struct json *json; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_atomic_type_from_json(&type, json)); json_destroy(json); print_and_free_json(ovsdb_atomic_type_to_json(type)); } static void -do_parse_base_type(int argc OVS_UNUSED, char *argv[]) +do_parse_base_type(struct ovs_cmdl_context *ctx) { struct ovsdb_base_type base; struct json *json; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_base_type_from_json(&base, json)); json_destroy(json); print_and_free_json(ovsdb_base_type_to_json(&base)); @@ -407,12 +410,12 @@ do_parse_base_type(int argc OVS_UNUSED, char *argv[]) } static void -do_parse_type(int argc OVS_UNUSED, char *argv[]) +do_parse_type(struct ovs_cmdl_context *ctx) { struct ovsdb_type type; struct json *json; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_type_from_json(&type, json)); json_destroy(json); print_and_free_json(ovsdb_type_to_json(&type)); @@ -420,21 +423,21 @@ do_parse_type(int argc OVS_UNUSED, char *argv[]) } static void -do_parse_atoms(int argc, char *argv[]) +do_parse_atoms(struct ovs_cmdl_context *ctx) { struct ovsdb_base_type base; struct json *json; int i; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_base_type_from_json(&base, json)); json_destroy(json); - for (i = 2; i < argc; i++) { + for (i = 2; i < ctx->argc; i++) { struct ovsdb_error *error; union ovsdb_atom atom; - json = unbox_json(parse_json(argv[i])); + json = unbox_json(parse_json(ctx->argv[i])); error = ovsdb_atom_from_json(&atom, &base, json, NULL); json_destroy(json); @@ -449,21 +452,21 @@ do_parse_atoms(int argc, char *argv[]) } static void -do_parse_atom_strings(int argc, char *argv[]) +do_parse_atom_strings(struct ovs_cmdl_context *ctx) { struct ovsdb_base_type base; struct json *json; int i; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_base_type_from_json(&base, json)); json_destroy(json); - for (i = 2; i < argc; i++) { + for (i = 2; i < ctx->argc; i++) { union ovsdb_atom atom; struct ds out; - die_if_error(ovsdb_atom_from_string(&atom, &base, argv[i], NULL)); + die_if_error(ovsdb_atom_from_string(&atom, &base, ctx->argv[i], NULL)); ds_init(&out); ovsdb_atom_to_string(&atom, base.type, &out); @@ -506,27 +509,27 @@ do_parse_data__(int argc, char *argv[], } static void -do_parse_data(int argc, char *argv[]) +do_parse_data(struct ovs_cmdl_context *ctx) { - do_parse_data__(argc, argv, ovsdb_datum_from_json); + do_parse_data__(ctx->argc, ctx->argv, ovsdb_datum_from_json); } static void -do_parse_data_strings(int argc, char *argv[]) +do_parse_data_strings(struct ovs_cmdl_context *ctx) { struct ovsdb_type type; struct json *json; int i; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_type_from_json(&type, json)); json_destroy(json); - for (i = 2; i < argc; i++) { + for (i = 2; i < ctx->argc; i++) { struct ovsdb_datum datum; struct ds out; - die_if_error(ovsdb_datum_from_string(&datum, &type, argv[i], NULL)); + die_if_error(ovsdb_datum_from_string(&datum, &type, ctx->argv[i], NULL)); ds_init(&out); ovsdb_datum_to_string(&datum, &type, &out); @@ -550,7 +553,7 @@ compare_atoms(const void *a_, const void *b_) } static void -do_sort_atoms(int argc OVS_UNUSED, char *argv[]) +do_sort_atoms(struct ovs_cmdl_context *ctx) { struct ovsdb_base_type base; union ovsdb_atom *atoms; @@ -558,11 +561,11 @@ do_sort_atoms(int argc OVS_UNUSED, char *argv[]) size_t n_atoms; int i; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_base_type_from_json(&base, json)); json_destroy(json); - json = unbox_json(parse_json(argv[2])); + json = unbox_json(parse_json(ctx->argv[2])); if (json->type != JSON_ARRAY) { ovs_fatal(0, "second argument must be array"); } @@ -592,36 +595,36 @@ do_sort_atoms(int argc OVS_UNUSED, char *argv[]) } static void -do_parse_column(int argc OVS_UNUSED, char *argv[]) +do_parse_column(struct ovs_cmdl_context *ctx) { struct ovsdb_column *column; struct json *json; - json = parse_json(argv[2]); - check_ovsdb_error(ovsdb_column_from_json(json, argv[1], &column)); + json = parse_json(ctx->argv[2]); + check_ovsdb_error(ovsdb_column_from_json(json, ctx->argv[1], &column)); json_destroy(json); print_and_free_json(ovsdb_column_to_json(column)); ovsdb_column_destroy(column); } static void -do_parse_table(int argc OVS_UNUSED, char *argv[]) +do_parse_table(struct ovs_cmdl_context *ctx) { struct ovsdb_table_schema *ts; bool default_is_root; struct json *json; - default_is_root = argc > 3 && !strcmp(argv[3], "true"); + default_is_root = ctx->argc > 3 && !strcmp(ctx->argv[3], "true"); - json = parse_json(argv[2]); - check_ovsdb_error(ovsdb_table_schema_from_json(json, argv[1], &ts)); + json = parse_json(ctx->argv[2]); + check_ovsdb_error(ovsdb_table_schema_from_json(json, ctx->argv[1], &ts)); json_destroy(json); print_and_free_json(ovsdb_table_schema_to_json(ts, default_is_root)); ovsdb_table_schema_destroy(ts); } static void -do_parse_rows(int argc, char *argv[]) +do_parse_rows(struct ovs_cmdl_context *ctx) { struct ovsdb_column_set all_columns; struct ovsdb_table_schema *ts; @@ -629,7 +632,7 @@ do_parse_rows(int argc, char *argv[]) struct json *json; int i; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts)); json_destroy(json); @@ -637,14 +640,14 @@ do_parse_rows(int argc, char *argv[]) ovsdb_column_set_init(&all_columns); ovsdb_column_set_add_all(&all_columns, table); - for (i = 2; i < argc; i++) { + for (i = 2; i < ctx->argc; i++) { struct ovsdb_column_set columns; struct ovsdb_row *row; ovsdb_column_set_init(&columns); row = ovsdb_row_create(table); - json = unbox_json(parse_json(argv[i])); + json = unbox_json(parse_json(ctx->argv[i])); check_ovsdb_error(ovsdb_row_from_json(row, json, NULL, &columns)); json_destroy(json); @@ -677,7 +680,7 @@ do_parse_rows(int argc, char *argv[]) } static void -do_compare_rows(int argc, char *argv[]) +do_compare_rows(struct ovs_cmdl_context *ctx) { struct ovsdb_column_set all_columns; struct ovsdb_table_schema *ts; @@ -688,7 +691,7 @@ do_compare_rows(int argc, char *argv[]) int n_rows; int i, j; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts)); json_destroy(json); @@ -696,17 +699,17 @@ do_compare_rows(int argc, char *argv[]) ovsdb_column_set_init(&all_columns); ovsdb_column_set_add_all(&all_columns, table); - n_rows = argc - 2; + n_rows = ctx->argc - 2; rows = xmalloc(sizeof *rows * n_rows); names = xmalloc(sizeof *names * n_rows); for (i = 0; i < n_rows; i++) { rows[i] = ovsdb_row_create(table); - json = parse_json(argv[i + 2]); + json = parse_json(ctx->argv[i + 2]); if (json->type != JSON_ARRAY || json->u.array.n != 2 || json->u.array.elems[0]->type != JSON_STRING) { ovs_fatal(0, "\"%s\" does not have expected form " - "[\"name\", {data}]", argv[i]); + "[\"name\", {data}]", ctx->argv[i]); } names[i] = xstrdup(json->u.array.elems[0]->u.string); check_ovsdb_error(ovsdb_row_from_json(rows[i], json->u.array.elems[1], @@ -740,22 +743,22 @@ do_compare_rows(int argc, char *argv[]) } static void -do_parse_conditions(int argc, char *argv[]) +do_parse_conditions(struct ovs_cmdl_context *ctx) { struct ovsdb_table_schema *ts; struct json *json; int exit_code = 0; int i; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts)); json_destroy(json); - for (i = 2; i < argc; i++) { + for (i = 2; i < ctx->argc; i++) { struct ovsdb_condition cnd; struct ovsdb_error *error; - json = parse_json(argv[i]); + json = parse_json(ctx->argv[i]); error = ovsdb_condition_from_json(ts, json, NULL, &cnd); if (!error) { print_and_free_json(ovsdb_condition_to_json(&cnd)); @@ -776,7 +779,7 @@ do_parse_conditions(int argc, char *argv[]) } static void -do_evaluate_conditions(int argc OVS_UNUSED, char *argv[]) +do_evaluate_conditions(struct ovs_cmdl_context *ctx) { struct ovsdb_table_schema *ts; struct ovsdb_table *table; @@ -788,14 +791,14 @@ do_evaluate_conditions(int argc OVS_UNUSED, char *argv[]) size_t i, j; /* Parse table schema, create table. */ - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts)); json_destroy(json); table = ovsdb_table_create(ts); /* Parse conditions. */ - json = parse_json(argv[2]); + json = parse_json(ctx->argv[2]); if (json->type != JSON_ARRAY) { ovs_fatal(0, "CONDITION argument is not JSON array"); } @@ -808,7 +811,7 @@ do_evaluate_conditions(int argc OVS_UNUSED, char *argv[]) json_destroy(json); /* Parse rows. */ - json = parse_json(argv[3]); + json = parse_json(ctx->argv[3]); if (json->type != JSON_ARRAY) { ovs_fatal(0, "ROW argument is not JSON array"); } @@ -845,22 +848,22 @@ do_evaluate_conditions(int argc OVS_UNUSED, char *argv[]) } static void -do_parse_mutations(int argc, char *argv[]) +do_parse_mutations(struct ovs_cmdl_context *ctx) { struct ovsdb_table_schema *ts; struct json *json; int exit_code = 0; int i; - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts)); json_destroy(json); - for (i = 2; i < argc; i++) { + for (i = 2; i < ctx->argc; i++) { struct ovsdb_mutation_set set; struct ovsdb_error *error; - json = parse_json(argv[i]); + json = parse_json(ctx->argv[i]); error = ovsdb_mutation_set_from_json(ts, json, NULL, &set); if (!error) { print_and_free_json(ovsdb_mutation_set_to_json(&set)); @@ -881,7 +884,7 @@ do_parse_mutations(int argc, char *argv[]) } static void -do_execute_mutations(int argc OVS_UNUSED, char *argv[]) +do_execute_mutations(struct ovs_cmdl_context *ctx) { struct ovsdb_table_schema *ts; struct ovsdb_table *table; @@ -893,14 +896,14 @@ do_execute_mutations(int argc OVS_UNUSED, char *argv[]) size_t i, j; /* Parse table schema, create table. */ - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts)); json_destroy(json); table = ovsdb_table_create(ts); /* Parse mutations. */ - json = parse_json(argv[2]); + json = parse_json(ctx->argv[2]); if (json->type != JSON_ARRAY) { ovs_fatal(0, "MUTATION argument is not JSON array"); } @@ -914,7 +917,7 @@ do_execute_mutations(int argc OVS_UNUSED, char *argv[]) json_destroy(json); /* Parse rows. */ - json = parse_json(argv[3]); + json = parse_json(ctx->argv[3]); if (json->type != JSON_ARRAY) { ovs_fatal(0, "ROW argument is not JSON array"); } @@ -1007,7 +1010,7 @@ do_query_cb(const struct ovsdb_row *row, void *cbdata_) } static void -do_query(int argc OVS_UNUSED, char *argv[]) +do_query(struct ovs_cmdl_context *ctx) { struct do_query_cbdata cbdata; struct ovsdb_table_schema *ts; @@ -1017,14 +1020,14 @@ do_query(int argc OVS_UNUSED, char *argv[]) size_t i; /* Parse table schema, create table. */ - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts)); json_destroy(json); table = ovsdb_table_create(ts); /* Parse rows, add to table. */ - json = parse_json(argv[2]); + json = parse_json(ctx->argv[2]); if (json->type != JSON_ARRAY) { ovs_fatal(0, "ROW argument is not JSON array"); } @@ -1046,7 +1049,7 @@ do_query(int argc OVS_UNUSED, char *argv[]) json_destroy(json); /* Parse conditions and execute queries. */ - json = parse_json(argv[3]); + json = parse_json(ctx->argv[3]); if (json->type != JSON_ARRAY) { ovs_fatal(0, "CONDITION argument is not JSON array"); } @@ -1097,7 +1100,7 @@ struct do_query_distinct_row { }; static void -do_query_distinct(int argc OVS_UNUSED, char *argv[]) +do_query_distinct(struct ovs_cmdl_context *ctx) { struct ovsdb_column_set columns; struct ovsdb_table_schema *ts; @@ -1111,20 +1114,20 @@ do_query_distinct(int argc OVS_UNUSED, char *argv[]) size_t i; /* Parse table schema, create table. */ - json = unbox_json(parse_json(argv[1])); + json = unbox_json(parse_json(ctx->argv[1])); check_ovsdb_error(ovsdb_table_schema_from_json(json, "mytable", &ts)); json_destroy(json); table = ovsdb_table_create(ts); /* Parse column set. */ - json = parse_json(argv[4]); + json = parse_json(ctx->argv[4]); check_ovsdb_error(ovsdb_column_set_from_json(json, table->schema, &columns)); json_destroy(json); /* Parse rows, add to table. */ - json = parse_json(argv[2]); + json = parse_json(ctx->argv[2]); if (json->type != JSON_ARRAY) { ovs_fatal(0, "ROW argument is not JSON array"); } @@ -1168,7 +1171,7 @@ do_query_distinct(int argc OVS_UNUSED, char *argv[]) json_destroy(json); /* Parse conditions and execute queries. */ - json = parse_json(argv[3]); + json = parse_json(ctx->argv[3]); if (json->type != JSON_ARRAY) { ovs_fatal(0, "CONDITION argument is not JSON array"); } @@ -1226,12 +1229,12 @@ do_query_distinct(int argc OVS_UNUSED, char *argv[]) } static void -do_parse_schema(int argc OVS_UNUSED, char *argv[]) +do_parse_schema(struct ovs_cmdl_context *ctx) { struct ovsdb_schema *schema; struct json *json; - json = parse_json(argv[1]); + json = parse_json(ctx->argv[1]); check_ovsdb_error(ovsdb_schema_from_json(json, &schema)); json_destroy(json); print_and_free_json(ovsdb_schema_to_json(schema)); @@ -1239,7 +1242,7 @@ do_parse_schema(int argc OVS_UNUSED, char *argv[]) } static void -do_execute(int argc OVS_UNUSED, char *argv[]) +do_execute(struct ovs_cmdl_context *ctx) { struct ovsdb_schema *schema; struct json *json; @@ -1247,16 +1250,16 @@ do_execute(int argc OVS_UNUSED, char *argv[]) int i; /* Create database. */ - json = parse_json(argv[1]); + json = parse_json(ctx->argv[1]); check_ovsdb_error(ovsdb_schema_from_json(json, &schema)); json_destroy(json); db = ovsdb_create(schema); - for (i = 2; i < argc; i++) { + for (i = 2; i < ctx->argc; i++) { struct json *params, *result; char *s; - params = parse_json(argv[i]); + params = parse_json(ctx->argv[i]); result = ovsdb_execute(db, NULL, params, 0, NULL); s = json_to_string(result, JSSF_SORT); printf("%s\n", s); @@ -1289,7 +1292,7 @@ do_trigger_dump(struct test_trigger *t, long long int now, const char *title) } static void -do_trigger(int argc OVS_UNUSED, char *argv[]) +do_trigger(struct ovs_cmdl_context *ctx) { struct ovsdb_schema *schema; struct ovsdb_session session; @@ -1301,7 +1304,7 @@ do_trigger(int argc OVS_UNUSED, char *argv[]) int i; /* Create database. */ - json = parse_json(argv[1]); + json = parse_json(ctx->argv[1]); check_ovsdb_error(ovsdb_schema_from_json(json, &schema)); json_destroy(json); db = ovsdb_create(schema); @@ -1312,8 +1315,8 @@ do_trigger(int argc OVS_UNUSED, char *argv[]) now = 0; number = 0; - for (i = 2; i < argc; i++) { - struct json *params = parse_json(argv[i]); + for (i = 2; i < ctx->argc; i++) { + struct json *params = parse_json(ctx->argv[i]); if (params->type == JSON_ARRAY && json_array(params)->n == 2 && json_array(params)->elems[0]->type == JSON_STRING @@ -1349,7 +1352,7 @@ do_trigger(int argc OVS_UNUSED, char *argv[]) } static void -do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_help(struct ovs_cmdl_context *ctx OVS_UNUSED) { usage(); } @@ -1361,14 +1364,14 @@ static struct ovsdb_txn *do_transact_txn; static struct ovsdb_table *do_transact_table; static void -do_transact_commit(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_transact_commit(struct ovs_cmdl_context *ctx OVS_UNUSED) { ovsdb_error_destroy(ovsdb_txn_commit(do_transact_txn, false)); do_transact_txn = NULL; } static void -do_transact_abort(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_transact_abort(struct ovs_cmdl_context *ctx OVS_UNUSED) { ovsdb_txn_abort(do_transact_txn); do_transact_txn = NULL; @@ -1428,7 +1431,7 @@ do_transact_set_i_j(struct ovsdb_row *row, } static void -do_transact_insert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_transact_insert(struct ovs_cmdl_context *ctx) { struct ovsdb_row *row; struct uuid *uuid; @@ -1437,34 +1440,34 @@ do_transact_insert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) /* Set UUID. */ uuid = ovsdb_row_get_uuid_rw(row); - uuid_from_integer(atoi(argv[1]), uuid); + uuid_from_integer(atoi(ctx->argv[1]), uuid); if (ovsdb_table_get_row(do_transact_table, uuid)) { ovs_fatal(0, "table already contains row with UUID "UUID_FMT, UUID_ARGS(uuid)); } - do_transact_set_i_j(row, argv[2], argv[3]); + do_transact_set_i_j(row, ctx->argv[2], ctx->argv[3]); /* Insert row. */ ovsdb_txn_row_insert(do_transact_txn, row); } static void -do_transact_delete(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_transact_delete(struct ovs_cmdl_context *ctx) { - const struct ovsdb_row *row = do_transact_find_row(argv[1]); + const struct ovsdb_row *row = do_transact_find_row(ctx->argv[1]); ovsdb_txn_row_delete(do_transact_txn, row); } static void -do_transact_modify(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_transact_modify(struct ovs_cmdl_context *ctx) { const struct ovsdb_row *row_ro; struct ovsdb_row *row_rw; - row_ro = do_transact_find_row(argv[1]); + row_ro = do_transact_find_row(ctx->argv[1]); row_rw = ovsdb_txn_row_modify(do_transact_txn, row_ro); - do_transact_set_i_j(row_rw, argv[2], argv[3]); + do_transact_set_i_j(row_rw, ctx->argv[2], ctx->argv[3]); } static int @@ -1477,7 +1480,7 @@ compare_rows_by_uuid(const void *a_, const void *b_) } static void -do_transact_print(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_transact_print(struct ovs_cmdl_context *ctx OVS_UNUSED) { const struct ovsdb_row **rows; const struct ovsdb_row *row; @@ -1505,7 +1508,7 @@ do_transact_print(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -do_transact(int argc, char *argv[]) +do_transact(struct ovs_cmdl_context *ctx) { static const struct ovs_cmdl_command do_transact_commands[] = { { "commit", NULL, 0, 0, do_transact_commit }, @@ -1534,13 +1537,14 @@ do_transact(int argc, char *argv[]) do_transact_table = ovsdb_get_table(do_transact_db, "mytable"); assert(do_transact_table != NULL); - for (i = 1; i < argc; i++) { + for (i = 1; i < ctx->argc; i++) { struct json *command; size_t n_args; char **args; int j; + struct ovs_cmdl_context transact_ctx = { .argc = 0, }; - command = parse_json(argv[i]); + command = parse_json(ctx->argv[i]); if (command->type != JSON_ARRAY) { ovs_fatal(0, "transaction %d must be JSON array " "with at least 1 element", i); @@ -1569,7 +1573,9 @@ do_transact(int argc, char *argv[]) fputs(args[j], stdout); } fputs(":", stdout); - ovs_cmdl_run_command(n_args, args, do_transact_commands); + transact_ctx.argc = n_args; + transact_ctx.argv = args; + ovs_cmdl_run_command(&transact_ctx, do_transact_commands); putchar('\n'); for (j = 0; j < n_args; j++) { @@ -1866,7 +1872,7 @@ idl_set(struct ovsdb_idl *idl, char *commands, int step) } static void -do_idl(int argc, char *argv[]) +do_idl(struct ovs_cmdl_context *ctx) { struct jsonrpc *rpc; struct ovsdb_idl *idl; @@ -1879,14 +1885,14 @@ do_idl(int argc, char *argv[]) idltest_init(); - idl = ovsdb_idl_create(argv[1], &idltest_idl_class, true, true); - if (argc > 2) { + idl = ovsdb_idl_create(ctx->argv[1], &idltest_idl_class, true, true); + if (ctx->argc > 2) { struct stream *stream; - error = stream_open_block(jsonrpc_stream_open(argv[1], &stream, + error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream, DSCP_DEFAULT), &stream); if (error) { - ovs_fatal(error, "failed to connect to \"%s\"", argv[1]); + ovs_fatal(error, "failed to connect to \"%s\"", ctx->argv[1]); } rpc = jsonrpc_open(stream); } else { @@ -1896,8 +1902,8 @@ do_idl(int argc, char *argv[]) setvbuf(stdout, NULL, _IONBF, 0); symtab = ovsdb_symbol_table_create(); - for (i = 2; i < argc; i++) { - char *arg = argv[i]; + for (i = 2; i < ctx->argc; i++) { + char *arg = ctx->argv[i]; struct jsonrpc_msg *request, *reply; if (*arg == '+') { diff --git a/tests/test-reconnect.c b/tests/test-reconnect.c index 2d6f62022..a55c57c3f 100644 --- a/tests/test-reconnect.c +++ b/tests/test-reconnect.c @@ -67,7 +67,11 @@ test_reconnect_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) svec_parse_words(&args, line); svec_terminate(&args); if (!svec_is_empty(&args)) { - ovs_cmdl_run_command(args.n, args.names, get_all_commands()); + struct ovs_cmdl_context ctx = { + .argc = args.n, + .argv = args.names, + }; + ovs_cmdl_run_command(&ctx, get_all_commands()); } svec_destroy(&args); @@ -88,19 +92,19 @@ test_reconnect_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -do_enable(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_enable(struct ovs_cmdl_context *ctx OVS_UNUSED) { reconnect_enable(reconnect, now); } static void -do_disable(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_disable(struct ovs_cmdl_context *ctx OVS_UNUSED) { reconnect_disable(reconnect, now); } static void -do_force_reconnect(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_force_reconnect(struct ovs_cmdl_context *ctx OVS_UNUSED) { reconnect_force_reconnect(reconnect, now); } @@ -120,42 +124,42 @@ error_from_string(const char *s) } static void -do_disconnected(int argc OVS_UNUSED, char *argv[]) +do_disconnected(struct ovs_cmdl_context *ctx) { - reconnect_disconnected(reconnect, now, error_from_string(argv[1])); + reconnect_disconnected(reconnect, now, error_from_string(ctx->argv[1])); } static void -do_connecting(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_connecting(struct ovs_cmdl_context *ctx OVS_UNUSED) { reconnect_connecting(reconnect, now); } static void -do_connect_failed(int argc OVS_UNUSED, char *argv[]) +do_connect_failed(struct ovs_cmdl_context *ctx) { - reconnect_connect_failed(reconnect, now, error_from_string(argv[1])); + reconnect_connect_failed(reconnect, now, error_from_string(ctx->argv[1])); } static void -do_connected(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_connected(struct ovs_cmdl_context *ctx OVS_UNUSED) { reconnect_connected(reconnect, now); } static void -do_activity(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_activity(struct ovs_cmdl_context *ctx OVS_UNUSED) { reconnect_activity(reconnect, now); } static void -do_run(int argc, char *argv[]) +do_run(struct ovs_cmdl_context *ctx) { enum reconnect_action action; - if (argc > 1) { - now += atoi(argv[1]); + if (ctx->argc > 1) { + now += atoi(ctx->argv[1]); } action = reconnect_run(reconnect, now); @@ -181,13 +185,13 @@ do_run(int argc, char *argv[]) } static void -do_advance(int argc OVS_UNUSED, char *argv[]) +do_advance(struct ovs_cmdl_context *ctx) { - now += atoi(argv[1]); + now += atoi(ctx->argv[1]); } static void -do_timeout(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_timeout(struct ovs_cmdl_context *ctx OVS_UNUSED) { int timeout = reconnect_timeout(reconnect, now); if (timeout >= 0) { @@ -199,9 +203,9 @@ do_timeout(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -do_set_max_tries(int argc OVS_UNUSED, char *argv[]) +do_set_max_tries(struct ovs_cmdl_context *ctx) { - reconnect_set_max_tries(reconnect, atoi(argv[1])); + reconnect_set_max_tries(reconnect, atoi(ctx->argv[1])); } static void @@ -251,21 +255,21 @@ diff_stats(const struct reconnect_stats *old, } static void -do_set_passive(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_set_passive(struct ovs_cmdl_context *ctx OVS_UNUSED) { reconnect_set_passive(reconnect, true, now); } static void -do_listening(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +do_listening(struct ovs_cmdl_context *ctx OVS_UNUSED) { reconnect_listening(reconnect, now); } static void -do_listen_error(int argc OVS_UNUSED, char *argv[]) +do_listen_error(struct ovs_cmdl_context *ctx) { - reconnect_listen_error(reconnect, now, atoi(argv[1])); + reconnect_listen_error(reconnect, now, atoi(ctx->argv[1])); } static const struct ovs_cmdl_command all_commands[] = { diff --git a/tests/test-util.c b/tests/test-util.c index 01a1a7829..1d63d68d1 100644 --- a/tests/test-util.c +++ b/tests/test-util.c @@ -40,7 +40,7 @@ check_log_2_floor(uint32_t x, int n) } static void -test_log_2_floor(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_log_2_floor(struct ovs_cmdl_context *ctx OVS_UNUSED) { int n; @@ -79,7 +79,7 @@ check_ctz64(uint64_t x, int n) } static void -test_ctz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_ctz(struct ovs_cmdl_context *ctx OVS_UNUSED) { int n; @@ -132,7 +132,7 @@ check_clz64(uint64_t x, int n) } static void -test_clz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_clz(struct ovs_cmdl_context *ctx OVS_UNUSED) { int n; @@ -182,7 +182,7 @@ check_rup2(uint32_t x, int n) } static void -test_round_up_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_round_up_pow2(struct ovs_cmdl_context *ctx OVS_UNUSED) { int n; @@ -210,7 +210,7 @@ check_rdp2(uint32_t x, int n) } static void -test_round_down_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_round_down_pow2(struct ovs_cmdl_context *ctx OVS_UNUSED) { int n; @@ -248,7 +248,7 @@ check_count_1bits(uint64_t x, int n) } static void -test_count_1bits(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_count_1bits(struct ovs_cmdl_context *ctx OVS_UNUSED) { uint64_t bits[64]; int i; @@ -287,7 +287,7 @@ sum_of_squares(int n) } static void -test_bitwise_copy(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_bitwise_copy(struct ovs_cmdl_context *ctx OVS_UNUSED) { unsigned int n_loops; int src_ofs; @@ -337,7 +337,7 @@ test_bitwise_copy(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -test_bitwise_zero(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_bitwise_zero(struct ovs_cmdl_context *ctx OVS_UNUSED) { unsigned int n_loops; int dst_ofs; @@ -378,7 +378,7 @@ test_bitwise_zero(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -test_bitwise_one(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_bitwise_one(struct ovs_cmdl_context *ctx OVS_UNUSED) { unsigned int n_loops; int dst_ofs; @@ -419,7 +419,7 @@ test_bitwise_one(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -test_bitwise_is_all_zeros(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_bitwise_is_all_zeros(struct ovs_cmdl_context *ctx OVS_UNUSED) { int n_loops; @@ -463,25 +463,25 @@ test_bitwise_is_all_zeros(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -test_follow_symlinks(int argc, char *argv[]) +test_follow_symlinks(struct ovs_cmdl_context *ctx) { int i; - for (i = 1; i < argc; i++) { - char *target = follow_symlinks(argv[i]); + for (i = 1; i < ctx->argc; i++) { + char *target = follow_symlinks(ctx->argv[i]); puts(target); free(target); } } static void -test_assert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_assert(struct ovs_cmdl_context *ctx OVS_UNUSED) { ovs_assert(false); } static void -test_ovs_scan(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_ovs_scan(struct ovs_cmdl_context *ctx OVS_UNUSED) { char str[16], str2[16], str3[16]; long double ld, ld2; @@ -1013,7 +1013,7 @@ test_ovs_scan(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -test_snprintf(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +test_snprintf(struct ovs_cmdl_context *ctx OVS_UNUSED) { char s[16]; @@ -1031,18 +1031,18 @@ test_snprintf(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) #ifndef _WIN32 static void -test_file_name(int argc, char *argv[]) +test_file_name(struct ovs_cmdl_context *ctx) { int i; - for (i = 1; i < argc; i++) { + for (i = 1; i < ctx->argc; i++) { char *dir, *base; - dir = dir_name(argv[i]); + dir = dir_name(ctx->argv[i]); puts(dir); free(dir); - base = base_name(argv[i]); + base = base_name(ctx->argv[i]); puts(base); free(base); } @@ -1104,6 +1104,7 @@ parse_options(int argc, char *argv[]) static void test_util_main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { .argc = 0, }; set_program_name(argv[0]); parse_options(argc, argv); /* On Windows, stderr is fully buffered if connected to a pipe. @@ -1111,7 +1112,9 @@ test_util_main(int argc, char *argv[]) * POSIX doesn't define the circumstances in which stderr is * fully buffered either. */ setvbuf(stderr, NULL, _IONBF, 0); - ovs_cmdl_run_command(argc - optind, argv + optind, commands); + ctx.argc = argc - optind; + ctx.argv = argv + optind; + ovs_cmdl_run_command(&ctx, commands); } OVSTEST_REGISTER("test-util", test_util_main); diff --git a/tests/test-vconn.c b/tests/test-vconn.c index 6e6ec99e3..17f4c1a38 100644 --- a/tests/test-vconn.c +++ b/tests/test-vconn.c @@ -141,9 +141,9 @@ fpv_destroy(struct fake_pvconn *fpv) /* Connects to a fake_pvconn with vconn_open(), then closes the listener and * verifies that vconn_connect() reports 'expected_error'. */ static void -test_refuse_connection(int argc OVS_UNUSED, char *argv[]) +test_refuse_connection(struct ovs_cmdl_context *ctx) { - const char *type = argv[1]; + const char *type = ctx->argv[1]; struct fake_pvconn fpv; struct vconn *vconn; int error; @@ -186,9 +186,9 @@ test_refuse_connection(int argc OVS_UNUSED, char *argv[]) * closes it immediately, and verifies that vconn_connect() reports * 'expected_error'. */ static void -test_accept_then_close(int argc OVS_UNUSED, char *argv[]) +test_accept_then_close(struct ovs_cmdl_context *ctx) { - const char *type = argv[1]; + const char *type = ctx->argv[1]; struct fake_pvconn fpv; struct vconn *vconn; int error; @@ -221,9 +221,9 @@ test_accept_then_close(int argc OVS_UNUSED, char *argv[]) * reads the hello message from it, then closes the connection and verifies * that vconn_connect() reports 'expected_error'. */ static void -test_read_hello(int argc OVS_UNUSED, char *argv[]) +test_read_hello(struct ovs_cmdl_context *ctx) { - const char *type = argv[1]; + const char *type = ctx->argv[1]; struct fake_pvconn fpv; struct vconn *vconn; struct stream *stream; @@ -361,9 +361,9 @@ test_send_hello(const char *type, const void *out, size_t out_size, /* Try connecting and sending a normal hello, which should succeed. */ static void -test_send_plain_hello(int argc OVS_UNUSED, char *argv[]) +test_send_plain_hello(struct ovs_cmdl_context *ctx) { - const char *type = argv[1]; + const char *type = ctx->argv[1]; struct ofpbuf *hello; hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP13_VERSION, @@ -376,9 +376,9 @@ test_send_plain_hello(int argc OVS_UNUSED, char *argv[]) * the specification says that implementations must accept and ignore extra * data). */ static void -test_send_long_hello(int argc OVS_UNUSED, char *argv[]) +test_send_long_hello(struct ovs_cmdl_context *ctx) { - const char *type = argv[1]; + const char *type = ctx->argv[1]; struct ofpbuf *hello; enum { EXTRA_BYTES = 8 }; @@ -393,9 +393,9 @@ test_send_long_hello(int argc OVS_UNUSED, char *argv[]) /* Try connecting and sending an echo request instead of a hello, which should * fail with EPROTO. */ static void -test_send_echo_hello(int argc OVS_UNUSED, char *argv[]) +test_send_echo_hello(struct ovs_cmdl_context *ctx) { - const char *type = argv[1]; + const char *type = ctx->argv[1]; struct ofpbuf *echo; echo = ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, OFP13_VERSION, @@ -407,9 +407,9 @@ test_send_echo_hello(int argc OVS_UNUSED, char *argv[]) /* Try connecting and sending a hello packet that has its length field as 0, * which should fail with EPROTO. */ static void -test_send_short_hello(int argc OVS_UNUSED, char *argv[]) +test_send_short_hello(struct ovs_cmdl_context *ctx) { - const char *type = argv[1]; + const char *type = ctx->argv[1]; struct ofp_header hello; memset(&hello, 0, sizeof hello); @@ -419,9 +419,9 @@ test_send_short_hello(int argc OVS_UNUSED, char *argv[]) /* Try connecting and sending a hello packet that has a bad version, which * should fail with EPROTO. */ static void -test_send_invalid_version_hello(int argc OVS_UNUSED, char *argv[]) +test_send_invalid_version_hello(struct ovs_cmdl_context *ctx) { - const char *type = argv[1]; + const char *type = ctx->argv[1]; struct ofpbuf *hello; hello = ofpraw_alloc_xid(OFPRAW_OFPT_HELLO, OFP13_VERSION, @@ -446,6 +446,11 @@ static const struct ovs_cmdl_command commands[] = { static void test_vconn_main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { + .argc = argc - 1, + .argv = argv + 1, + }; + set_program_name(argv[0]); vlog_set_levels(NULL, VLF_ANY_DESTINATION, VLL_EMER); vlog_set_levels(NULL, VLF_CONSOLE, VLL_DBG); @@ -453,7 +458,7 @@ test_vconn_main(int argc, char *argv[]) time_alarm(10); - ovs_cmdl_run_command(argc - 1, argv + 1, commands); + ovs_cmdl_run_command(&ctx, commands); } OVSTEST_REGISTER("test-vconn", test_vconn_main); diff --git a/utilities/ovs-benchmark.c b/utilities/ovs-benchmark.c index d2f2e8b34..228af6e8f 100644 --- a/utilities/ovs-benchmark.c +++ b/utilities/ovs-benchmark.c @@ -81,10 +81,13 @@ time_in_msec(void) int main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { .argc = 0, }; set_program_name(argv[0]); vlog_set_levels(NULL, VLF_ANY_DESTINATION, VLL_EMER); parse_options(argc, argv); - ovs_cmdl_run_command(argc - optind, argv + optind, get_all_commands()); + ctx.argc = argc - optind; + ctx.argv = argv + optind; + ovs_cmdl_run_command(&ctx, get_all_commands()); return 0; } @@ -244,7 +247,7 @@ Other options:\n\ } static void -cmd_listen(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +cmd_listen(struct ovs_cmdl_context *ctx OVS_UNUSED) { struct pollfd *fds; int n_fds; @@ -370,7 +373,7 @@ bind_local_port(int fd, unsigned short int *local_port, } static void -cmd_rate(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +cmd_rate(struct ovs_cmdl_context *ctx OVS_UNUSED) { unsigned short int local_port; unsigned short int remote_port; @@ -520,7 +523,7 @@ timer_end(long long int start, bool error, } static void -cmd_latency(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +cmd_latency(struct ovs_cmdl_context *ctx OVS_UNUSED) { unsigned short int local_port; unsigned short int remote_port; @@ -611,7 +614,7 @@ cmd_latency(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) } static void -cmd_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +cmd_help(struct ovs_cmdl_context *ctx OVS_UNUSED) { usage(); } diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c index 89b89a903..3d61c4b8a 100644 --- a/utilities/ovs-ofctl.c +++ b/utilities/ovs-ofctl.c @@ -117,11 +117,14 @@ static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid, int main(int argc, char *argv[]) { + struct ovs_cmdl_context ctx = { .argc = 0, }; set_program_name(argv[0]); service_start(&argc, &argv); parse_options(argc, argv); fatal_ignore_sigpipe(); - ovs_cmdl_run_command(argc - optind, argv + optind, get_all_commands()); + ctx.argc = argc - optind; + ctx.argv = argv + optind; + ovs_cmdl_run_command(&ctx, get_all_commands()); return 0; } @@ -649,9 +652,9 @@ set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config) } static void -ofctl_show(int argc OVS_UNUSED, char *argv[]) +ofctl_show(struct ovs_cmdl_context *ctx) { - const char *vconn_name = argv[1]; + const char *vconn_name = ctx->argv[1]; enum ofp_version version; struct vconn *vconn; struct ofpbuf *request; @@ -676,24 +679,24 @@ ofctl_show(int argc OVS_UNUSED, char *argv[]) } static void -ofctl_dump_desc(int argc OVS_UNUSED, char *argv[]) +ofctl_dump_desc(struct ovs_cmdl_context *ctx) { - dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_DESC_REQUEST); + dump_trivial_stats_transaction(ctx->argv[1], OFPRAW_OFPST_DESC_REQUEST); } static void -ofctl_dump_tables(int argc OVS_UNUSED, char *argv[]) +ofctl_dump_tables(struct ovs_cmdl_context *ctx) { - dump_trivial_stats_transaction(argv[1], OFPRAW_OFPST_TABLE_REQUEST); + dump_trivial_stats_transaction(ctx->argv[1], OFPRAW_OFPST_TABLE_REQUEST); } static void -ofctl_dump_table_features(int argc OVS_UNUSED, char *argv[]) +ofctl_dump_table_features(struct ovs_cmdl_context *ctx) { struct ofpbuf *request; struct vconn *vconn; - open_vconn(argv[1], &vconn); + open_vconn(ctx->argv[1], &vconn); request = ofputil_encode_table_features_request(vconn_get_version(vconn)); if (request) { dump_stats_transaction(vconn, request); @@ -1023,10 +1026,10 @@ compare_flows(const void *afs_, const void *bfs_) } static void -ofctl_dump_flows(int argc, char *argv[]) +ofctl_dump_flows(struct ovs_cmdl_context *ctx) { if (!n_criteria) { - ofctl_dump_flows__(argc, argv, false); + ofctl_dump_flows__(ctx->argc, ctx->argv, false); return; } else { struct ofputil_flow_stats *fses; @@ -1039,7 +1042,7 @@ ofctl_dump_flows(int argc, char *argv[]) struct ds s; size_t i; - vconn = prepare_dump_flows(argc, argv, false, &request); + vconn = prepare_dump_flows(ctx->argc, ctx->argv, false, &request); send_xid = ((struct ofp_header *) request->data)->xid; send_openflow_buffer(vconn, request); @@ -1084,27 +1087,27 @@ ofctl_dump_flows(int argc, char *argv[]) } static void -ofctl_dump_aggregate(int argc, char *argv[]) +ofctl_dump_aggregate(struct ovs_cmdl_context *ctx) { - ofctl_dump_flows__(argc, argv, true); + ofctl_dump_flows__(ctx->argc, ctx->argv, true); } static void -ofctl_queue_stats(int argc, char *argv[]) +ofctl_queue_stats(struct ovs_cmdl_context *ctx) { struct ofpbuf *request; struct vconn *vconn; struct ofputil_queue_stats_request oqs; - open_vconn(argv[1], &vconn); + open_vconn(ctx->argv[1], &vconn); - if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) { - oqs.port_no = str_to_port_no(argv[1], argv[2]); + if (ctx->argc > 2 && ctx->argv[2][0] && strcasecmp(ctx->argv[2], "all")) { + oqs.port_no = str_to_port_no(ctx->argv[1], ctx->argv[2]); } else { oqs.port_no = OFPP_ANY; } - if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) { - oqs.queue_id = atoi(argv[3]); + if (ctx->argc > 3 && ctx->argv[3][0] && strcasecmp(ctx->argv[3], "all")) { + oqs.queue_id = atoi(ctx->argv[3]); } else { oqs.queue_id = OFPQ_ALL; } @@ -1115,10 +1118,10 @@ ofctl_queue_stats(int argc, char *argv[]) } static void -ofctl_queue_get_config(int argc OVS_UNUSED, char *argv[]) +ofctl_queue_get_config(struct ovs_cmdl_context *ctx) { - const char *vconn_name = argv[1]; - const char *port_name = argv[2]; + const char *vconn_name = ctx->argv[1]; + const char *port_name = ctx->argv[2]; enum ofputil_protocol protocol; enum ofp_version version; struct ofpbuf *request; @@ -1227,27 +1230,27 @@ ofctl_flow_mod(int argc, char *argv[], uint16_t command) } static void -ofctl_add_flow(int argc, char *argv[]) +ofctl_add_flow(struct ovs_cmdl_context *ctx) { - ofctl_flow_mod(argc, argv, OFPFC_ADD); + ofctl_flow_mod(ctx->argc, ctx->argv, OFPFC_ADD); } static void -ofctl_add_flows(int argc, char *argv[]) +ofctl_add_flows(struct ovs_cmdl_context *ctx) { - ofctl_flow_mod_file(argc, argv, OFPFC_ADD); + ofctl_flow_mod_file(ctx->argc, ctx->argv, OFPFC_ADD); } static void -ofctl_mod_flows(int argc, char *argv[]) +ofctl_mod_flows(struct ovs_cmdl_context *ctx) { - ofctl_flow_mod(argc, argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY); + ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY); } static void -ofctl_del_flows(int argc, char *argv[]) +ofctl_del_flows(struct ovs_cmdl_context *ctx) { - ofctl_flow_mod(argc, argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE); + ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE); } static void @@ -1542,15 +1545,15 @@ monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests) } static void -ofctl_monitor(int argc, char *argv[]) +ofctl_monitor(struct ovs_cmdl_context *ctx) { struct vconn *vconn; int i; enum ofputil_protocol usable_protocols; - open_vconn(argv[1], &vconn); - for (i = 2; i < argc; i++) { - const char *arg = argv[i]; + open_vconn(ctx->argv[1], &vconn); + for (i = 2; i < ctx->argc; i++) { + const char *arg = ctx->argv[i]; if (isdigit((unsigned char) *arg)) { struct ofp_switch_config config; @@ -1618,37 +1621,37 @@ ofctl_monitor(int argc, char *argv[]) } static void -ofctl_snoop(int argc OVS_UNUSED, char *argv[]) +ofctl_snoop(struct ovs_cmdl_context *ctx) { struct vconn *vconn; - open_vconn__(argv[1], SNOOP, &vconn); + open_vconn__(ctx->argv[1], SNOOP, &vconn); monitor_vconn(vconn, false); } static void -ofctl_dump_ports(int argc, char *argv[]) +ofctl_dump_ports(struct ovs_cmdl_context *ctx) { struct ofpbuf *request; struct vconn *vconn; ofp_port_t port; - open_vconn(argv[1], &vconn); - port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY; + open_vconn(ctx->argv[1], &vconn); + port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY; request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port); dump_stats_transaction(vconn, request); vconn_close(vconn); } static void -ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[]) +ofctl_dump_ports_desc(struct ovs_cmdl_context *ctx) { struct ofpbuf *request; struct vconn *vconn; ofp_port_t port; - open_vconn(argv[1], &vconn); - port = argc > 2 ? str_to_port_no(argv[1], argv[2]) : OFPP_ANY; + open_vconn(ctx->argv[1], &vconn); + port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY; request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn), port); dump_stats_transaction(vconn, request); @@ -1656,15 +1659,15 @@ ofctl_dump_ports_desc(int argc OVS_UNUSED, char *argv[]) } static void -ofctl_probe(int argc OVS_UNUSED, char *argv[]) +ofctl_probe(struct ovs_cmdl_context *ctx) { struct ofpbuf *request; struct vconn *vconn; struct ofpbuf *reply; - open_vconn(argv[1], &vconn); + open_vconn(ctx->argv[1], &vconn); request = make_echo_request(vconn_get_version(vconn)); - run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]); + run(vconn_transact(vconn, request, &reply), "talking to %s", ctx->argv[1]); if (reply->size != sizeof(struct ofp_header)) { ovs_fatal(0, "reply does not match request"); } @@ -1673,7 +1676,7 @@ ofctl_probe(int argc OVS_UNUSED, char *argv[]) } static void -ofctl_packet_out(int argc, char *argv[]) +ofctl_packet_out(struct ovs_cmdl_context *ctx) { enum ofputil_protocol protocol; struct ofputil_packet_out po; @@ -1684,23 +1687,23 @@ ofctl_packet_out(int argc, char *argv[]) enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */ ofpbuf_init(&ofpacts, 64); - error = ofpacts_parse_actions(argv[3], &ofpacts, &usable_protocols); + error = ofpacts_parse_actions(ctx->argv[3], &ofpacts, &usable_protocols); if (error) { ovs_fatal(0, "%s", error); } po.buffer_id = UINT32_MAX; - po.in_port = str_to_port_no(argv[1], argv[2]); + po.in_port = str_to_port_no(ctx->argv[1], ctx->argv[2]); po.ofpacts = ofpacts.data; po.ofpacts_len = ofpacts.size; - protocol = open_vconn(argv[1], &vconn); - for (i = 4; i < argc; i++) { + protocol = open_vconn(ctx->argv[1], &vconn); + for (i = 4; i < ctx->argc; i++) { struct dp_packet *packet; struct ofpbuf *opo; const char *error_msg; - error_msg = eth_from_hex(argv[i], &packet); + error_msg = eth_from_hex(ctx->argv[i], &packet); if (error_msg) { ovs_fatal(0, "%s", error_msg); } @@ -1716,7 +1719,7 @@ ofctl_packet_out(int argc, char *argv[]) } static void -ofctl_mod_port(int argc OVS_UNUSED, char *argv[]) +ofctl_mod_port(struct ovs_cmdl_context *ctx) { struct ofp_config_flag { const char *name; /* The flag's name. */ @@ -1742,7 +1745,7 @@ ofctl_mod_port(int argc OVS_UNUSED, char *argv[]) const char *command; bool not; - fetch_ofputil_phy_port(argv[1], argv[2], &pp); + fetch_ofputil_phy_port(ctx->argv[1], ctx->argv[2], &pp); pm.port_no = pp.port_no; memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN); @@ -1750,14 +1753,14 @@ ofctl_mod_port(int argc OVS_UNUSED, char *argv[]) pm.mask = 0; pm.advertise = 0; - if (!strncasecmp(argv[3], "no-", 3)) { - command = argv[3] + 3; + if (!strncasecmp(ctx->argv[3], "no-", 3)) { + command = ctx->argv[3] + 3; not = true; - } else if (!strncasecmp(argv[3], "no", 2)) { - command = argv[3] + 2; + } else if (!strncasecmp(ctx->argv[3], "no", 2)) { + command = ctx->argv[3] + 2; not = true; } else { - command = argv[3]; + command = ctx->argv[3]; not = false; } for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) { @@ -1767,16 +1770,16 @@ ofctl_mod_port(int argc OVS_UNUSED, char *argv[]) goto found; } } - ovs_fatal(0, "unknown mod-port command '%s'", argv[3]); + ovs_fatal(0, "unknown mod-port command '%s'", ctx->argv[3]); found: - protocol = open_vconn(argv[1], &vconn); + protocol = open_vconn(ctx->argv[1], &vconn); transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol)); vconn_close(vconn); } static void -ofctl_mod_table(int argc OVS_UNUSED, char *argv[]) +ofctl_mod_table(struct ovs_cmdl_context *ctx) { enum ofputil_protocol protocol, usable_protocols; struct ofputil_table_mod tm; @@ -1784,12 +1787,12 @@ ofctl_mod_table(int argc OVS_UNUSED, char *argv[]) char *error; int i; - error = parse_ofp_table_mod(&tm, argv[2], argv[3], &usable_protocols); + error = parse_ofp_table_mod(&tm, ctx->argv[2], ctx->argv[3], &usable_protocols); if (error) { ovs_fatal(0, "%s", error); } - protocol = open_vconn(argv[1], &vconn); + protocol = open_vconn(ctx->argv[1], &vconn); if (!(protocol & usable_protocols)) { for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) { enum ofputil_protocol f = 1 << i; @@ -1812,30 +1815,30 @@ ofctl_mod_table(int argc OVS_UNUSED, char *argv[]) } static void -ofctl_get_frags(int argc OVS_UNUSED, char *argv[]) +ofctl_get_frags(struct ovs_cmdl_context *ctx) { struct ofp_switch_config config; struct vconn *vconn; - open_vconn(argv[1], &vconn); + open_vconn(ctx->argv[1], &vconn); fetch_switch_config(vconn, &config); puts(ofputil_frag_handling_to_string(ntohs(config.flags))); vconn_close(vconn); } static void -ofctl_set_frags(int argc OVS_UNUSED, char *argv[]) +ofctl_set_frags(struct ovs_cmdl_context *ctx) { struct ofp_switch_config config; enum ofp_config_flags mode; struct vconn *vconn; ovs_be16 flags; - if (!ofputil_frag_handling_from_string(argv[2], &mode)) { - ovs_fatal(0, "%s: unknown fragment handling mode", argv[2]); + if (!ofputil_frag_handling_from_string(ctx->argv[2], &mode)) { + ovs_fatal(0, "%s: unknown fragment handling mode", ctx->argv[2]); } - open_vconn(argv[1], &vconn); + open_vconn(ctx->argv[1], &vconn); fetch_switch_config(vconn, &config); flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK)); if (flags != config.flags) { @@ -1850,16 +1853,16 @@ ofctl_set_frags(int argc OVS_UNUSED, char *argv[]) if (flags != config.flags) { ovs_fatal(0, "%s: setting fragment handling mode failed (this " "switch probably doesn't support mode \"%s\")", - argv[1], ofputil_frag_handling_to_string(mode)); + ctx->argv[1], ofputil_frag_handling_to_string(mode)); } } vconn_close(vconn); } static void -ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[]) +ofctl_ofp_parse(struct ovs_cmdl_context *ctx) { - const char *filename = argv[1]; + const char *filename = ctx->argv[1]; struct ofpbuf b; FILE *file; @@ -1925,16 +1928,16 @@ is_openflow_port(ovs_be16 port_, char *ports[]) } static void -ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[]) +ofctl_ofp_parse_pcap(struct ovs_cmdl_context *ctx) { struct tcp_reader *reader; FILE *file; int error; bool first; - file = ovs_pcap_open(argv[1], "rb"); + file = ovs_pcap_open(ctx->argv[1], "rb"); if (!file) { - ovs_fatal(errno, "%s: open failed", argv[1]); + ovs_fatal(errno, "%s: open failed", ctx->argv[1]); } reader = tcp_reader_open(); @@ -1952,8 +1955,8 @@ ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[]) flow_extract(packet, &flow); if (flow.dl_type == htons(ETH_TYPE_IP) && flow.nw_proto == IPPROTO_TCP - && (is_openflow_port(flow.tp_src, argv + 2) || - is_openflow_port(flow.tp_dst, argv + 2))) { + && (is_openflow_port(flow.tp_src, ctx->argv + 2) || + is_openflow_port(flow.tp_dst, ctx->argv + 2))) { struct dp_packet *payload = tcp_reader_run(reader, &flow, packet); if (payload) { while (dp_packet_size(payload) >= sizeof(struct ofp_header)) { @@ -1995,19 +1998,19 @@ ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[]) } static void -ofctl_ping(int argc, char *argv[]) +ofctl_ping(struct ovs_cmdl_context *ctx) { size_t max_payload = 65535 - sizeof(struct ofp_header); unsigned int payload; struct vconn *vconn; int i; - payload = argc > 2 ? atoi(argv[2]) : 64; + payload = ctx->argc > 2 ? atoi(ctx->argv[2]) : 64; if (payload > max_payload) { ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload); } - open_vconn(argv[1], &vconn); + open_vconn(ctx->argv[1], &vconn); for (i = 0; i < 10; i++) { struct timeval start, end; struct ofpbuf *request, *reply; @@ -2033,7 +2036,7 @@ ofctl_ping(int argc, char *argv[]) ofp_print(stdout, reply, reply->size, verbosity + 2); } printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n", - reply->size, argv[1], ntohl(rpy_hdr->xid), + reply->size, ctx->argv[1], ntohl(rpy_hdr->xid), (1000*(double)(end.tv_sec - start.tv_sec)) + (.001*(end.tv_usec - start.tv_usec))); ofpbuf_delete(request); @@ -2043,7 +2046,7 @@ ofctl_ping(int argc, char *argv[]) } static void -ofctl_benchmark(int argc OVS_UNUSED, char *argv[]) +ofctl_benchmark(struct ovs_cmdl_context *ctx) { size_t max_payload = 65535 - sizeof(struct ofp_header); struct timeval start, end; @@ -2053,18 +2056,18 @@ ofctl_benchmark(int argc OVS_UNUSED, char *argv[]) int count; int i; - payload_size = atoi(argv[2]); + payload_size = atoi(ctx->argv[2]); if (payload_size > max_payload) { ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload); } message_size = sizeof(struct ofp_header) + payload_size; - count = atoi(argv[3]); + count = atoi(ctx->argv[3]); printf("Sending %d packets * %u bytes (with header) = %u bytes total\n", count, message_size, count * message_size); - open_vconn(argv[1], &vconn); + open_vconn(ctx->argv[1], &vconn); xgettimeofday(&start); for (i = 0; i < count; i++) { struct ofpbuf *request, *reply; @@ -2155,43 +2158,43 @@ ofctl_group_mod(int argc, char *argv[], uint16_t command) } static void -ofctl_add_group(int argc, char *argv[]) +ofctl_add_group(struct ovs_cmdl_context *ctx) { - ofctl_group_mod(argc, argv, OFPGC11_ADD); + ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_ADD); } static void -ofctl_add_groups(int argc, char *argv[]) +ofctl_add_groups(struct ovs_cmdl_context *ctx) { - ofctl_group_mod_file(argc, argv, OFPGC11_ADD); + ofctl_group_mod_file(ctx->argc, ctx->argv, OFPGC11_ADD); } static void -ofctl_mod_group(int argc, char *argv[]) +ofctl_mod_group(struct ovs_cmdl_context *ctx) { - ofctl_group_mod(argc, argv, OFPGC11_MODIFY); + ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_MODIFY); } static void -ofctl_del_groups(int argc, char *argv[]) +ofctl_del_groups(struct ovs_cmdl_context *ctx) { - ofctl_group_mod(argc, argv, OFPGC11_DELETE); + ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_DELETE); } static void -ofctl_insert_bucket(int argc, char *argv[]) +ofctl_insert_bucket(struct ovs_cmdl_context *ctx) { - ofctl_group_mod(argc, argv, OFPGC15_INSERT_BUCKET); + ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_INSERT_BUCKET); } static void -ofctl_remove_bucket(int argc, char *argv[]) +ofctl_remove_bucket(struct ovs_cmdl_context *ctx) { - ofctl_group_mod(argc, argv, OFPGC15_REMOVE_BUCKET); + ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_REMOVE_BUCKET); } static void -ofctl_dump_group_stats(int argc, char *argv[]) +ofctl_dump_group_stats(struct ovs_cmdl_context *ctx) { enum ofputil_protocol usable_protocols; struct ofputil_group_mod gm; @@ -2203,7 +2206,7 @@ ofctl_dump_group_stats(int argc, char *argv[]) memset(&gm, 0, sizeof gm); error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE, - argc > 2 ? argv[2] : "", + ctx->argc > 2 ? ctx->argv[2] : "", &usable_protocols); if (error) { ovs_fatal(0, "%s", error); @@ -2211,7 +2214,7 @@ ofctl_dump_group_stats(int argc, char *argv[]) group_id = gm.group_id; - open_vconn(argv[1], &vconn); + open_vconn(ctx->argv[1], &vconn); request = ofputil_encode_group_stats_request(vconn_get_version(vconn), group_id); if (request) { @@ -2222,15 +2225,15 @@ ofctl_dump_group_stats(int argc, char *argv[]) } static void -ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[]) +ofctl_dump_group_desc(struct ovs_cmdl_context *ctx) { struct ofpbuf *request; struct vconn *vconn; uint32_t group_id; - open_vconn(argv[1], &vconn); + open_vconn(ctx->argv[1], &vconn); - if (argc < 3 || !ofputil_group_from_string(argv[2], &group_id)) { + if (ctx->argc < 3 || !ofputil_group_from_string(ctx->argv[2], &group_id)) { group_id = OFPG11_ALL; } @@ -2244,12 +2247,12 @@ ofctl_dump_group_desc(int argc OVS_UNUSED, char *argv[]) } static void -ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[]) +ofctl_dump_group_features(struct ovs_cmdl_context *ctx) { struct ofpbuf *request; struct vconn *vconn; - open_vconn(argv[1], &vconn); + open_vconn(ctx->argv[1], &vconn); request = ofputil_encode_group_features_request(vconn_get_version(vconn)); if (request) { dump_stats_transaction(vconn, request); @@ -2259,13 +2262,13 @@ ofctl_dump_group_features(int argc OVS_UNUSED, char *argv[]) } static void -ofctl_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +ofctl_help(struct ovs_cmdl_context *ctx OVS_UNUSED) { usage(); } static void -ofctl_list_commands(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +ofctl_list_commands(struct ovs_cmdl_context *ctx OVS_UNUSED) { ovs_cmdl_print_commands(get_all_commands()); } @@ -2591,7 +2594,7 @@ fte_make_flow_mod(const struct fte *fte, int index, uint16_t command, } static void -ofctl_replace_flows(int argc OVS_UNUSED, char *argv[]) +ofctl_replace_flows(struct ovs_cmdl_context *ctx) { enum { FILE_IDX = 0, SWITCH_IDX = 1 }; enum ofputil_protocol usable_protocols, protocol; @@ -2601,9 +2604,9 @@ ofctl_replace_flows(int argc OVS_UNUSED, char *argv[]) struct fte *fte; classifier_init(&cls, NULL); - usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX); + usable_protocols = read_flows_from_file(ctx->argv[2], &cls, FILE_IDX); - protocol = open_vconn(argv[1], &vconn); + protocol = open_vconn(ctx->argv[1], &vconn); protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols); read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX); @@ -2658,7 +2661,7 @@ read_flows_from_source(const char *source, struct classifier *cls, int index) } static void -ofctl_diff_flows(int argc OVS_UNUSED, char *argv[]) +ofctl_diff_flows(struct ovs_cmdl_context *ctx) { bool differences = false; struct classifier cls; @@ -2666,8 +2669,8 @@ ofctl_diff_flows(int argc OVS_UNUSED, char *argv[]) struct fte *fte; classifier_init(&cls, NULL); - read_flows_from_source(argv[1], &cls, 0); - read_flows_from_source(argv[2], &cls, 1); + read_flows_from_source(ctx->argv[1], &cls, 0); + read_flows_from_source(ctx->argv[2], &cls, 1); ds_init(&a_s); ds_init(&b_s); @@ -2759,41 +2762,41 @@ ofctl_meter_request__(const char *bridge, const char *str, static void -ofctl_add_meter(int argc OVS_UNUSED, char *argv[]) +ofctl_add_meter(struct ovs_cmdl_context *ctx) { - ofctl_meter_mod__(argv[1], argv[2], OFPMC13_ADD); + ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_ADD); } static void -ofctl_mod_meter(int argc OVS_UNUSED, char *argv[]) +ofctl_mod_meter(struct ovs_cmdl_context *ctx) { - ofctl_meter_mod__(argv[1], argv[2], OFPMC13_MODIFY); + ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_MODIFY); } static void -ofctl_del_meters(int argc, char *argv[]) +ofctl_del_meters(struct ovs_cmdl_context *ctx) { - ofctl_meter_mod__(argv[1], argc > 2 ? argv[2] : NULL, OFPMC13_DELETE); + ofctl_meter_mod__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, OFPMC13_DELETE); } static void -ofctl_dump_meters(int argc, char *argv[]) +ofctl_dump_meters(struct ovs_cmdl_context *ctx) { - ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL, + ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, OFPUTIL_METER_CONFIG); } static void -ofctl_meter_stats(int argc, char *argv[]) +ofctl_meter_stats(struct ovs_cmdl_context *ctx) { - ofctl_meter_request__(argv[1], argc > 2 ? argv[2] : NULL, + ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, OFPUTIL_METER_STATS); } static void -ofctl_meter_features(int argc OVS_UNUSED, char *argv[]) +ofctl_meter_features(struct ovs_cmdl_context *ctx) { - ofctl_meter_request__(argv[1], NULL, OFPUTIL_METER_FEATURES); + ofctl_meter_request__(ctx->argv[1], NULL, OFPUTIL_METER_FEATURES); } @@ -2839,13 +2842,13 @@ ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms, /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints * it back to stdout. */ static void -ofctl_parse_flow(int argc OVS_UNUSED, char *argv[]) +ofctl_parse_flow(struct ovs_cmdl_context *ctx) { enum ofputil_protocol usable_protocols; struct ofputil_flow_mod fm; char *error; - error = parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, &usable_protocols); + error = parse_ofp_flow_mod_str(&fm, ctx->argv[1], OFPFC_ADD, &usable_protocols); if (error) { ovs_fatal(0, "%s", error); } @@ -2855,14 +2858,14 @@ ofctl_parse_flow(int argc OVS_UNUSED, char *argv[]) /* "parse-flows FILENAME": reads the named file as a sequence of flows (like * add-flows) and prints each of the flows back to stdout. */ static void -ofctl_parse_flows(int argc OVS_UNUSED, char *argv[]) +ofctl_parse_flows(struct ovs_cmdl_context *ctx) { enum ofputil_protocol usable_protocols; struct ofputil_flow_mod *fms = NULL; size_t n_fms = 0; char *error; - error = parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms, + error = parse_ofp_flow_mod_file(ctx->argv[1], OFPFC_ADD, &fms, &n_fms, &usable_protocols); if (error) { ovs_fatal(0, "%s", error); @@ -2945,7 +2948,7 @@ ofctl_parse_nxm__(bool oxm, enum ofp_version version) * stdin, does some internal fussing with them, and then prints them back as * strings on stdout. */ static void -ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +ofctl_parse_nxm(struct ovs_cmdl_context *ctx OVS_UNUSED) { ofctl_parse_nxm__(false, 0); } @@ -2955,11 +2958,11 @@ ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) * them back as strings on stdout. VERSION must specify an OpenFlow version, * e.g. "OpenFlow12". */ static void -ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[]) +ofctl_parse_oxm(struct ovs_cmdl_context *ctx) { - enum ofp_version version = ofputil_version_from_string(argv[1]); + enum ofp_version version = ofputil_version_from_string(ctx->argv[1]); if (version < OFP12_VERSION) { - ovs_fatal(0, "%s: not a valid version for OXM", argv[1]); + ovs_fatal(0, "%s: not a valid version for OXM", ctx->argv[1]); } ofctl_parse_nxm__(true, version); @@ -3086,9 +3089,9 @@ ofctl_parse_actions__(const char *version_s, bool instructions) * prints them as strings on stdout, and then converts them back to hex bytes * and prints any differences from the input. */ static void -ofctl_parse_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +ofctl_parse_actions(struct ovs_cmdl_context *ctx) { - ofctl_parse_actions__(argv[1], false); + ofctl_parse_actions__(ctx->argv[1], false); } /* "parse-actions VERSION": reads a series of instruction specifications for @@ -3096,9 +3099,9 @@ ofctl_parse_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) * ofpacts, prints them as strings on stdout, and then converts them back to * hex bytes and prints any differences from the input. */ static void -ofctl_parse_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +ofctl_parse_instructions(struct ovs_cmdl_context *ctx) { - ofctl_parse_actions__(argv[1], true); + ofctl_parse_actions__(ctx->argv[1], true); } /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex @@ -3111,7 +3114,7 @@ ofctl_parse_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) * them back to hex bytes. ovs-ofctl actually sets "x"s to random bits when * it does the conversion to hex, to ensure that in fact they are ignored. */ static void -ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +ofctl_parse_ofp10_match(struct ovs_cmdl_context *ctx OVS_UNUSED) { struct ds expout; struct ds in; @@ -3185,7 +3188,7 @@ ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) * on stdout, and then converts them back to hex bytes and prints any * differences from the input. */ static void -ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +ofctl_parse_ofp11_match(struct ovs_cmdl_context *ctx OVS_UNUSED) { struct ds in; @@ -3231,13 +3234,13 @@ ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) /* "parse-pcap PCAP": read packets from PCAP and print their flows. */ static void -ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[]) +ofctl_parse_pcap(struct ovs_cmdl_context *ctx) { FILE *pcap; - pcap = ovs_pcap_open(argv[1], "rb"); + pcap = ovs_pcap_open(ctx->argv[1], "rb"); if (!pcap) { - ovs_fatal(errno, "%s: open failed", argv[1]); + ovs_fatal(errno, "%s: open failed", ctx->argv[1]); } for (;;) { @@ -3249,7 +3252,7 @@ ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[]) if (error == EOF) { break; } else if (error) { - ovs_fatal(error, "%s: read failed", argv[1]); + ovs_fatal(error, "%s: read failed", ctx->argv[1]); } packet->md = PKT_METADATA_INITIALIZER(ODPP_NONE); @@ -3263,7 +3266,7 @@ ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[]) /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and * mask values to and from various formats and prints the results. */ static void -ofctl_check_vlan(int argc OVS_UNUSED, char *argv[]) +ofctl_check_vlan(struct ovs_cmdl_context *ctx) { struct match match; @@ -3287,8 +3290,8 @@ ofctl_check_vlan(int argc OVS_UNUSED, char *argv[]) enum ofputil_protocol usable_protocols; /* Unused for now. */ match_init_catchall(&match); - match.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16)); - match.wc.masks.vlan_tci = htons(strtoul(argv[2], NULL, 16)); + match.flow.vlan_tci = htons(strtoul(ctx->argv[1], NULL, 16)); + match.wc.masks.vlan_tci = htons(strtoul(ctx->argv[2], NULL, 16)); /* Convert to and from string. */ string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY); @@ -3369,14 +3372,14 @@ ofctl_check_vlan(int argc OVS_UNUSED, char *argv[]) /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow * version. */ static void -ofctl_print_error(int argc OVS_UNUSED, char *argv[]) +ofctl_print_error(struct ovs_cmdl_context *ctx) { enum ofperr error; int version; - error = ofperr_from_name(argv[1]); + error = ofperr_from_name(ctx->argv[1]); if (!error) { - ovs_fatal(0, "unknown error \"%s\"", argv[1]); + ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]); } for (version = 0; version <= UINT8_MAX; version++) { @@ -3397,19 +3400,19 @@ ofctl_print_error(int argc OVS_UNUSED, char *argv[]) /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the * error named ENUM and prints the error reply in hex. */ static void -ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[]) +ofctl_encode_error_reply(struct ovs_cmdl_context *ctx) { const struct ofp_header *oh; struct ofpbuf request, *reply; enum ofperr error; - error = ofperr_from_name(argv[1]); + error = ofperr_from_name(ctx->argv[1]); if (!error) { - ovs_fatal(0, "unknown error \"%s\"", argv[1]); + ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]); } ofpbuf_init(&request, 0); - if (ofpbuf_put_hex(&request, argv[2], NULL)[0] != '\0') { + if (ofpbuf_put_hex(&request, ctx->argv[2], NULL)[0] != '\0') { ovs_fatal(0, "Trailing garbage in hex data"); } if (request.size < sizeof(struct ofp_header)) { @@ -3435,7 +3438,7 @@ ofctl_encode_error_reply(int argc OVS_UNUSED, char *argv[]) * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where * HEXSTRING_FILE contains the HEXSTRING. */ static void -ofctl_ofp_print(int argc, char *argv[]) +ofctl_ofp_print(struct ovs_cmdl_context *ctx) { struct ofpbuf packet; char *buffer; @@ -3444,18 +3447,18 @@ ofctl_ofp_print(int argc, char *argv[]) ds_init(&line); - if (!strcmp(argv[argc-1], "-")) { + if (!strcmp(ctx->argv[ctx->argc-1], "-")) { if (ds_get_line(&line, stdin)) { VLOG_FATAL("Failed to read stdin"); } buffer = line.string; - verbosity = argc > 2 ? atoi(argv[1]) : verbosity; - } else if (argc > 2) { - buffer = argv[1]; - verbosity = atoi(argv[2]); + verbosity = ctx->argc > 2 ? atoi(ctx->argv[1]) : verbosity; + } else if (ctx->argc > 2) { + buffer = ctx->argv[1]; + verbosity = atoi(ctx->argv[2]); } else { - buffer = argv[1]; + buffer = ctx->argv[1]; } ofpbuf_init(&packet, strlen(buffer) / 2); @@ -3470,9 +3473,9 @@ ofctl_ofp_print(int argc, char *argv[]) /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message * and dumps each message in hex. */ static void -ofctl_encode_hello(int argc OVS_UNUSED, char *argv[]) +ofctl_encode_hello(struct ovs_cmdl_context *ctx) { - uint32_t bitmap = strtol(argv[1], NULL, 0); + uint32_t bitmap = strtol(ctx->argv[1], NULL, 0); struct ofpbuf *hello; hello = ofputil_encode_hello(bitmap); -- 2.20.1