laforge submitted this change.
Add optional timeout to mapping
To prevent a broken or crashed application from blocking a mapping for
an undefined time, a timeout can be specified. The application that
controls the mapping, chooses a timeout that is long enough to complete
the operation before the timeout occurs.
This patch applies to any mapping, but it was created to support timeout
for maintenance mapping (with higher priority).
Related: SYS#8159
Change-Id: I2f7ee24e6c926b65876af2b14da3534555398769
---
M src/server/rest_api.c
M src/server/rspro_server.c
M src/slotmap.c
M src/slotmap.h
4 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/src/server/rest_api.c b/src/server/rest_api.c
index f26096a..766b367 100644
--- a/src/server/rest_api.c
+++ b/src/server/rest_api.c
@@ -316,6 +316,7 @@
static int api_cb_slotmaps_post(const struct _u_request *req, struct _u_response *resp, void *user_data)
{
const char *priority_str = u_map_get(req->map_url, "priority");
+ const char *timeout_str = u_map_get(req->map_url, "timeout");
struct rspro_server *srv = g_rps;
struct slot_mapping slotmap, *map, *other_map;
struct rspro_client_conn *conn;
@@ -323,9 +324,11 @@
json_t *json_req = NULL;
bool suspending_map = false;
int priority;
+ int timeout;
int rc;
priority = (priority_str) ? atoi(priority_str) : 0;
+ timeout = (timeout_str) ? atoi(timeout_str) : 0;
json_req = ulfius_get_json_body_request(req, &json_err);
if (!json_req) {
@@ -362,6 +365,7 @@
LOGP(DREST, LOGL_NOTICE, "REST: Cannot add slotmap\n");
goto err;
}
+ map->timeout = timeout;
slotmap_state_change(map, SLMAP_S_NEW, NULL);
/* If there is already a mapping with higher priority. */
diff --git a/src/server/rspro_server.c b/src/server/rspro_server.c
index a6172b8..e6b56f6 100644
--- a/src/server/rspro_server.c
+++ b/src/server/rspro_server.c
@@ -410,6 +410,25 @@
}
}
+/* Handle timeout for mapping. */
+static void mapping_timeout(void *data)
+{
+ struct slot_mapping *map = data;
+ struct rspro_client_conn *conn;
+
+ conn = bankd_conn_by_id(g_rps, map->bank.bank_id);
+ if (!conn)
+ return;
+
+ /* The timer fires in ACTIVE state, so we trigger deletion now. */
+ if (map->state == SLMAP_S_ACTIVE) {
+ LOGPFSML(conn->fi, LOGL_INFO, "Temporary mapping timed out, delete from B%u:%u.\n",
+ map->bank.bank_id, map->bank.slot_nr);
+ slotmap_state_change(map, SLMAP_S_DELETE_REQ, &conn->bank.maps_delreq);
+ osmo_fsm_inst_dispatch(conn->fi, CLNTC_E_PUSH, NULL);
+ }
+}
+
static void clnt_st_connected_bankd(struct osmo_fsm_inst *fi, uint32_t event, void *data)
{
struct rspro_client_conn *conn = fi->priv;
@@ -440,7 +459,11 @@
RsproPDU_t *pdu = slotmap2RemoveMappingReq(map);
client_conn_send(conn, pdu);
slotmap_state_change(map, SLMAP_S_DELETING, &conn->bank.maps_deleting);
+ break;
}
+ /* If mapping has been created, start mapping timeout timer. */
+ if (map->timeout > 0)
+ slotmap_start_timer(map, mapping_timeout);
break;
case CLNTC_E_REMOVE_MAP_RES: /* Bankd acknowledges mapping was removed */
rx = data;
diff --git a/src/slotmap.c b/src/slotmap.c
index 9f53ff7..5323fbb 100644
--- a/src/slotmap.c
+++ b/src/slotmap.c
@@ -181,6 +181,15 @@
return map;
}
+#ifdef REMSIM_SERVER
+void slotmap_start_timer(struct slot_mapping *map, void(*timeout_cb)(void *data))
+{
+ osmo_timer_setup(&map->timer, timeout_cb, map);
+ osmo_timer_schedule(&map->timer, map->timeout, 0);
+}
+
+#endif
+
/* thread-safe removal of a bank<->client map */
void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
{
@@ -190,6 +199,7 @@
llist_del(&map->list);
#ifdef REMSIM_SERVER
+ osmo_timer_del(&map->timer);
llist_del(&map->bank_list);
#endif
diff --git a/src/slotmap.h b/src/slotmap.h
index 756c29f..60af877 100644
--- a/src/slotmap.h
+++ b/src/slotmap.h
@@ -3,6 +3,7 @@
#include <stdbool.h>
#include <pthread.h>
#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/timer.h>
#define REMSIM_SERVER 1
@@ -60,6 +61,9 @@
#ifdef REMSIM_SERVER
struct llist_head bank_list;
enum slot_mapping_state state;
+ /* optional timer to limit mapping duration */
+ struct osmo_timer_list timer;
+ int timeout;
/* priority of mapping: any signed integer (negative, 0, or positive);
* higher value means higher priority; default is 0 */
int priority;
@@ -97,6 +101,10 @@
struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, const struct client_slot *client,
int priority);
+#ifdef REMSIM_SERVER
+void slotmap_start_timer(struct slot_mapping *map, void(*timeout_cb)(void *data));
+#endif
+
/* thread-safe removal of a bank<->client map */
void slotmap_del(struct slotmaps *maps, struct slot_mapping *map);
void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map);
To view, visit change 43746. To unsubscribe, or for help writing mail filters, visit settings.