From 39c9e7b471f29ec1df8b4fc407bb3fe2ac96cb9e Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 22 Aug 2014 00:28:51 +0200 Subject: libctrl: Add support for 'deferred control commands' Sometimes a control interface command cannot be processed and responded immediately, but we need to process it asynchronously. In order to support this, we introduce the 'ctrl_cmd_def', which represents such a deferred command. It is created by the service implementing the command using ctrl_cmd_def_make(), and a response is later sent using ctrl_cmd_def_send(). ctrl_cmd_def_is_zombie() must be called to handle the case where the control connection has disconnected/died between receiving the command and sending the response. --- include/osmocom/ctrl/control_cmd.h | 15 ++++++++++ src/ctrl/control_cmd.c | 60 ++++++++++++++++++++++++++++++++++++++ src/ctrl/control_if.c | 17 +++++++++++ 3 files changed, 92 insertions(+) diff --git a/include/osmocom/ctrl/control_cmd.h b/include/osmocom/ctrl/control_cmd.h index 9e949631..8f2eaa25 100644 --- a/include/osmocom/ctrl/control_cmd.h +++ b/include/osmocom/ctrl/control_cmd.h @@ -45,6 +45,9 @@ struct ctrl_connection { /* Pending commands for this connection */ struct llist_head cmds; + + /* Pending deferred commands for this connection */ + struct llist_head def_cmds; }; struct ctrl_cmd { @@ -75,6 +78,18 @@ struct ctrl_cmd_map { enum ctrl_type type; }; +/* deferred control command, i.e. responded asynchronously */ +struct ctrl_cmd_def { + struct llist_head list; /* ctrl_connection.def_cmds */ + struct ctrl_cmd *cmd; + void *data; /* opaque user data */ +}; + +struct ctrl_cmd_def * +ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs); +int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd); +int ctrl_cmd_def_send(struct ctrl_cmd_def *cd); + int ctrl_cmd_exec(vector vline, struct ctrl_cmd *command, vector node, void *data); int ctrl_cmd_install(enum ctrl_node_type node, struct ctrl_cmd_element *cmd); int ctrl_cmd_send(struct osmo_wqueue *queue, struct ctrl_cmd *cmd); diff --git a/src/ctrl/control_cmd.c b/src/ctrl/control_cmd.c index 88a30a2a..4e156b81 100644 --- a/src/ctrl/control_cmd.c +++ b/src/ctrl/control_cmd.c @@ -487,3 +487,63 @@ err: msgb_free(msg); return NULL; } + +struct ctrl_cmd_def * +ctrl_cmd_def_make(const void *ctx, struct ctrl_cmd *cmd, void *data, unsigned int secs) +{ + struct ctrl_cmd_def *cd; + + if (!cmd->ccon) + return NULL; + + cd = talloc_zero(ctx, struct ctrl_cmd_def); + + cd->cmd = cmd; + cd->data = data; + + /* add to per-connection list of deferred commands */ + llist_add(&cd->list, &cmd->ccon->def_cmds); + + return cd; +} + +int ctrl_cmd_def_is_zombie(struct ctrl_cmd_def *cd) +{ + /* luckily we're still alive */ + if (cd->cmd) + return 0; + + /* if we are a zombie, make sure we really die */ + llist_del(&cd->list); + talloc_free(cd); + + return 1; +} + +int ctrl_cmd_def_send(struct ctrl_cmd_def *cd) +{ + struct ctrl_cmd *cmd = cd->cmd; + + int rc; + + /* Deferred commands can only be responses to GET/SET or ERROR, but + * never TRAP or anything else */ + switch (cmd->type) { + case CTRL_TYPE_GET: + cmd->type = CTRL_TYPE_GET_REPLY; + break; + case CTRL_TYPE_SET: + cmd->type = CTRL_TYPE_SET_REPLY; + break; + default: + cmd->type = CTRL_TYPE_ERROR; + } + + rc = ctrl_cmd_send(&cmd->ccon->write_queue, cmd); + + talloc_free(cmd); + llist_del(&cd->list); + talloc_free(cd); + + return rc; +} diff --git a/src/ctrl/control_if.c b/src/ctrl/control_if.c index 59cf2552..c20c1e0f 100644 --- a/src/ctrl/control_if.c +++ b/src/ctrl/control_if.c @@ -128,6 +128,8 @@ struct ctrl_cmd *ctrl_cmd_trap(struct ctrl_cmd *cmd) static void control_close_conn(struct ctrl_connection *ccon) { + struct ctrl_cmd_def *cd, *cd2; + osmo_wqueue_clear(&ccon->write_queue); close(ccon->write_queue.bfd.fd); osmo_fd_unregister(&ccon->write_queue.bfd); @@ -135,6 +137,19 @@ static void control_close_conn(struct ctrl_connection *ccon) if (ccon->closed_cb) ccon->closed_cb(ccon); msgb_free(ccon->pending_msg); + + /* clean up deferred commands */ + llist_for_each_entry_safe(cd, cd2, &ccon->def_cmds, list) { + /* delete from list of def_cmds for this ccon */ + llist_del(&cd->list); + /* not strictly needed as this is a slave to the ccon which we + * are about to free anyway */ + talloc_free(cd->cmd); + /* set the CMD to null, this is the indication to the user that + * the connection for this command has gone */ + cd->cmd = NULL; + } + talloc_free(ccon); } @@ -338,6 +353,8 @@ static struct ctrl_connection *ctrl_connection_alloc(void *ctx) /* Error handling here? */ INIT_LLIST_HEAD(&ccon->cmds); + INIT_LLIST_HEAD(&ccon->def_cmds); + return ccon; } -- cgit v1.2.3