From 8ca79daaa04ca3d5edcacf84646d953569f55cb6 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 29 Oct 2009 15:20:21 -0700 Subject: [PATCH] unixctl: Allow passing auxiliary data to unixctl commands. This will allow users of unixctl to avoid using global variables, leading to cleaner code. --- lib/coverage.c | 5 ++-- lib/unixctl.c | 14 ++++++----- lib/unixctl.h | 5 ++-- lib/vlog.c | 14 ++++++----- vswitchd/bridge.c | 52 +++++++++++++++++++++++++---------------- vswitchd/ovs-vswitchd.c | 6 ++--- 6 files changed, 57 insertions(+), 39 deletions(-) diff --git a/lib/coverage.c b/lib/coverage.c index cdc796ec..8b5e9d9c 100644 --- a/lib/coverage.c +++ b/lib/coverage.c @@ -30,7 +30,8 @@ static unsigned int epoch; static void -coverage_unixctl_log(struct unixctl_conn *conn, const char *args UNUSED) +coverage_unixctl_log(struct unixctl_conn *conn, const char *args UNUSED, + void *aux UNUSED) { coverage_log(VLL_WARN, false); unixctl_command_reply(conn, 200, NULL); @@ -39,7 +40,7 @@ coverage_unixctl_log(struct unixctl_conn *conn, const char *args UNUSED) void coverage_init(void) { - unixctl_command_register("coverage/log", coverage_unixctl_log); + unixctl_command_register("coverage/log", coverage_unixctl_log, NULL); } /* Sorts coverage counters in descending order by count, within equal counts diff --git a/lib/unixctl.c b/lib/unixctl.c index e774ffe4..8565e588 100644 --- a/lib/unixctl.c +++ b/lib/unixctl.c @@ -44,7 +44,8 @@ #include "vlog.h" struct unixctl_command { - void (*cb)(struct unixctl_conn *, const char *args); + unixctl_cb_func *cb; + void *aux; }; struct unixctl_conn { @@ -76,7 +77,8 @@ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); static struct shash commands = SHASH_INITIALIZER(&commands); static void -unixctl_help(struct unixctl_conn *conn, const char *args UNUSED) +unixctl_help(struct unixctl_conn *conn, const char *args UNUSED, + void *aux UNUSED) { struct ds ds = DS_EMPTY_INITIALIZER; struct shash_node *node; @@ -90,8 +92,7 @@ unixctl_help(struct unixctl_conn *conn, const char *args UNUSED) } void -unixctl_command_register(const char *name, - void (*cb)(struct unixctl_conn *, const char *args)) +unixctl_command_register(const char *name, unixctl_cb_func *cb, void *aux) { struct unixctl_command *command; @@ -99,6 +100,7 @@ unixctl_command_register(const char *name, || shash_find_data(&commands, name) == cb); command = xmalloc(sizeof *command); command->cb = cb; + command->aux = aux; shash_add(&commands, name, command); } @@ -178,7 +180,7 @@ unixctl_server_create(const char *path, struct unixctl_server **serverp) struct unixctl_server *server; int error; - unixctl_command_register("help", unixctl_help); + unixctl_command_register("help", unixctl_help, NULL); server = xmalloc(sizeof *server); list_init(&server->conns); @@ -282,7 +284,7 @@ process_command(struct unixctl_conn *conn, char *s) command = shash_find_data(&commands, name); if (command) { - command->cb(conn, args); + command->cb(conn, args, command->aux); } else { char *msg = xasprintf("\"%s\" is not a valid command", name); unixctl_command_reply(conn, 400, msg); diff --git a/lib/unixctl.h b/lib/unixctl.h index 18748aa7..0b6cbf33 100644 --- a/lib/unixctl.h +++ b/lib/unixctl.h @@ -35,9 +35,10 @@ const char *unixctl_client_target(const struct unixctl_client *); /* Command registration. */ struct unixctl_conn; +typedef void unixctl_cb_func(struct unixctl_conn *, + const char *args, void *aux); void unixctl_command_register(const char *name, - void (*cb)(struct unixctl_conn *, - const char *args)); + unixctl_cb_func *cb, void *aux); void unixctl_command_reply(struct unixctl_conn *, int code, const char *body); diff --git a/lib/vlog.c b/lib/vlog.c index 5496f010..b2d0c069 100644 --- a/lib/vlog.c +++ b/lib/vlog.c @@ -385,7 +385,7 @@ vlog_set_verbosity(const char *arg) } static void -vlog_unixctl_set(struct unixctl_conn *conn, const char *args) +vlog_unixctl_set(struct unixctl_conn *conn, const char *args, void *aux UNUSED) { char *msg = vlog_set_levels_from_string(args); unixctl_command_reply(conn, msg ? 501 : 202, msg); @@ -393,7 +393,8 @@ vlog_unixctl_set(struct unixctl_conn *conn, const char *args) } static void -vlog_unixctl_list(struct unixctl_conn *conn, const char *args UNUSED) +vlog_unixctl_list(struct unixctl_conn *conn, + const char *args UNUSED, void *aux UNUSED) { char *msg = vlog_get_levels(); unixctl_command_reply(conn, 200, msg); @@ -401,7 +402,8 @@ vlog_unixctl_list(struct unixctl_conn *conn, const char *args UNUSED) } static void -vlog_unixctl_reopen(struct unixctl_conn *conn, const char *args UNUSED) +vlog_unixctl_reopen(struct unixctl_conn *conn, + const char *args UNUSED, void *aux UNUSED) { if (log_file_name) { int error = vlog_reopen_log_file(); @@ -435,9 +437,9 @@ vlog_init(void) VLOG_ERR("current time is negative: %s (%ld)", s, (long int) now); } - unixctl_command_register("vlog/set", vlog_unixctl_set); - unixctl_command_register("vlog/list", vlog_unixctl_list); - unixctl_command_register("vlog/reopen", vlog_unixctl_reopen); + unixctl_command_register("vlog/set", vlog_unixctl_set, NULL); + unixctl_command_register("vlog/list", vlog_unixctl_list, NULL); + unixctl_command_register("vlog/reopen", vlog_unixctl_reopen, NULL); } /* Closes the logging subsystem. */ diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c index b2c051d0..b0db9ab0 100644 --- a/vswitchd/bridge.c +++ b/vswitchd/bridge.c @@ -194,7 +194,7 @@ enum { DP_MAX = 256 }; static struct bridge *bridge_create(const char *name); static void bridge_destroy(struct bridge *); static struct bridge *bridge_lookup(const char *name); -static void bridge_unixctl_dump_flows(struct unixctl_conn *, const char *); +static unixctl_cb_func bridge_unixctl_dump_flows; static int bridge_run_one(struct bridge *); static void bridge_reconfigure_one(struct bridge *); static void bridge_reconfigure_controller(struct bridge *); @@ -210,7 +210,7 @@ static uint64_t bridge_pick_datapath_id(struct bridge *, static struct iface *bridge_get_local_iface(struct bridge *); static uint64_t dpid_from_hash(const void *, size_t nbytes); -static void bridge_unixctl_fdb_show(struct unixctl_conn *, const char *args); +static unixctl_cb_func bridge_unixctl_fdb_show; static void bond_init(void); static void bond_run(struct bridge *); @@ -287,7 +287,7 @@ bridge_init(void) struct svec dpif_names; size_t i; - unixctl_command_register("fdb/show", bridge_unixctl_fdb_show); + unixctl_command_register("fdb/show", bridge_unixctl_fdb_show, NULL); svec_init(&dpif_names); dp_enumerate(&dpif_names); @@ -316,7 +316,8 @@ bridge_init(void) } svec_destroy(&dpif_names); - unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows); + unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows, + NULL); bond_init(); bridge_reconfigure(); @@ -926,7 +927,8 @@ bridge_get_local_iface(struct bridge *br) /* Bridge unixctl user interface functions. */ static void -bridge_unixctl_fdb_show(struct unixctl_conn *conn, const char *args) +bridge_unixctl_fdb_show(struct unixctl_conn *conn, + const char *args, void *aux UNUSED) { struct ds ds = DS_EMPTY_INITIALIZER; const struct bridge *br; @@ -1061,7 +1063,8 @@ bridge_get_datapathid(const char *name) /* Handle requests for a listing of all flows known by the OpenFlow * stack, including those normally hidden. */ static void -bridge_unixctl_dump_flows(struct unixctl_conn *conn, const char *args) +bridge_unixctl_dump_flows(struct unixctl_conn *conn, + const char *args, void *aux UNUSED) { struct bridge *br; struct ds results; @@ -2521,7 +2524,8 @@ bond_send_learning_packets(struct port *port) /* Bonding unixctl user interface functions. */ static void -bond_unixctl_list(struct unixctl_conn *conn, const char *args UNUSED) +bond_unixctl_list(struct unixctl_conn *conn, + const char *args UNUSED, void *aux UNUSED) { struct ds ds = DS_EMPTY_INITIALIZER; const struct bridge *br; @@ -2571,7 +2575,8 @@ bond_find(const char *name) } static void -bond_unixctl_show(struct unixctl_conn *conn, const char *args) +bond_unixctl_show(struct unixctl_conn *conn, + const char *args, void *aux UNUSED) { struct ds ds = DS_EMPTY_INITIALIZER; const struct port *port; @@ -2640,7 +2645,8 @@ bond_unixctl_show(struct unixctl_conn *conn, const char *args) } static void -bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_) +bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_, + void *aux UNUSED) { char *args = (char *) args_; char *save_ptr = NULL; @@ -2696,7 +2702,8 @@ bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_) } static void -bond_unixctl_set_active_slave(struct unixctl_conn *conn, const char *args_) +bond_unixctl_set_active_slave(struct unixctl_conn *conn, const char *args_, + void *aux UNUSED) { char *args = (char *) args_; char *save_ptr = NULL; @@ -2776,19 +2783,22 @@ enable_slave(struct unixctl_conn *conn, const char *args_, bool enable) } static void -bond_unixctl_enable_slave(struct unixctl_conn *conn, const char *args) +bond_unixctl_enable_slave(struct unixctl_conn *conn, const char *args, + void *aux UNUSED) { enable_slave(conn, args, true); } static void -bond_unixctl_disable_slave(struct unixctl_conn *conn, const char *args) +bond_unixctl_disable_slave(struct unixctl_conn *conn, const char *args, + void *aux UNUSED) { enable_slave(conn, args, false); } static void -bond_unixctl_hash(struct unixctl_conn *conn, const char *args) +bond_unixctl_hash(struct unixctl_conn *conn, const char *args, + void *aux UNUSED) { uint8_t mac[ETH_ADDR_LEN]; uint8_t hash; @@ -2809,14 +2819,16 @@ bond_unixctl_hash(struct unixctl_conn *conn, const char *args) static void bond_init(void) { - unixctl_command_register("bond/list", bond_unixctl_list); - unixctl_command_register("bond/show", bond_unixctl_show); - unixctl_command_register("bond/migrate", bond_unixctl_migrate); + unixctl_command_register("bond/list", bond_unixctl_list, NULL); + unixctl_command_register("bond/show", bond_unixctl_show, NULL); + unixctl_command_register("bond/migrate", bond_unixctl_migrate, NULL); unixctl_command_register("bond/set-active-slave", - bond_unixctl_set_active_slave); - unixctl_command_register("bond/enable-slave", bond_unixctl_enable_slave); - unixctl_command_register("bond/disable-slave", bond_unixctl_disable_slave); - unixctl_command_register("bond/hash", bond_unixctl_hash); + bond_unixctl_set_active_slave, NULL); + unixctl_command_register("bond/enable-slave", bond_unixctl_enable_slave, + NULL); + unixctl_command_register("bond/disable-slave", bond_unixctl_disable_slave, + NULL); + unixctl_command_register("bond/hash", bond_unixctl_hash, NULL); } /* Port functions. */ diff --git a/vswitchd/ovs-vswitchd.c b/vswitchd/ovs-vswitchd.c index 28491fc6..fd2144ab 100644 --- a/vswitchd/ovs-vswitchd.c +++ b/vswitchd/ovs-vswitchd.c @@ -50,7 +50,7 @@ static void parse_options(int argc, char *argv[]); static void usage(void) NO_RETURN; -static void reload(struct unixctl_conn *, const char *args); +static unixctl_cb_func reload; static bool need_reconfigure; static struct unixctl_conn **conns; @@ -79,7 +79,7 @@ main(int argc, char *argv[]) if (retval) { ovs_fatal(retval, "could not listen for control connections"); } - unixctl_command_register("vswitchd/reload", reload); + unixctl_command_register("vswitchd/reload", reload, NULL); retval = cfg_read(); if (retval) { @@ -122,7 +122,7 @@ main(int argc, char *argv[]) } static void -reload(struct unixctl_conn *conn, const char *args UNUSED) +reload(struct unixctl_conn *conn, const char *args UNUSED, void *aux UNUSED) { need_reconfigure = true; conns = xrealloc(conns, sizeof *conns * (n_conns + 1)); -- 2.30.2