laforge submitted this change.

View Change

Approvals: pespin: Looks good to me, approved Jenkins Builder: Verified
Add priority mapping support to osmo-remsim-server

A normal mapping and a mapping with higher priority may share the same
bank/slot. In this case the normal mapping will be inactive until the
mapping with higher priority has been removed. This allows temporary
mapping for maintenance purposes without removing the existing mapping
to a bank/slot.

If a mappings with different priority share the same bank/slot, the
mapping with higher priority is activated and the mapping with the lower
priority is suspended. The following steps are performed, if a mapping
to the same bank/slot is added with higher priority:

* The higher priority mapping is created and put into INACTIVE state.
* The lower priority mapping is suspended and put into INACTIVE state.
* The higher priority mapping is activated and becomes ACTIVE.

Once the higher priority mapping is removed, the lower priority mapping
is resumed. The following steps are performed:

* The higher priority mapping is deactivated and then removed.
* The lower priority mapping in INACTIVE state is activated again and
becomes ACTIVE.

If a lower priority mapping is added while a higher priority mapping to
the same bank/slot exists, the added mapping will stay in INACTIVE state
until it is removed or the higher priority mapping is removed.

To control the priority attribute, the rest API is expanded. An optional
'priority' tag is added to the URL. If not specified, the default
priority of 0 is used.

The rest API allows to show only mappings with given or default
priority, not all at once. This prevents prioritized mappings from
interfering with user's applications.

Related: SYS#8159
Change-Id: I885b3b529276f46fbbe6d86be2170b0bffbb8434
---
M src/server/rest_api.c
M src/server/rspro_server.c
M src/slotmap.h
3 files changed, 103 insertions(+), 10 deletions(-)

diff --git a/src/server/rest_api.c b/src/server/rest_api.c
index 99c3238..f26096a 100644
--- a/src/server/rest_api.c
+++ b/src/server/rest_api.c
@@ -278,12 +278,18 @@

static int api_cb_slotmaps_get(const struct _u_request *req, struct _u_response *resp, void *user_data)
{
+ const char *priority_str = u_map_get(req->map_url, "priority");
struct slot_mapping *map;
json_t *json_body = json_object();
json_t *json_maps = json_array();
+ int priority;
+
+ priority = (priority_str) ? atoi(priority_str) : 0;

slotmaps_rdlock(g_rps->slotmaps);
llist_for_each_entry(map, &g_rps->slotmaps->mappings, list) {
+ if (map->priority != priority)
+ continue;
json_array_append_new(json_maps, slotmap2json(map));
}
slotmaps_unlock(g_rps->slotmaps);
@@ -309,13 +315,18 @@

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");
struct rspro_server *srv = g_rps;
- struct slot_mapping slotmap, *map;
+ struct slot_mapping slotmap, *map, *other_map;
struct rspro_client_conn *conn;
json_error_t json_err;
json_t *json_req = NULL;
+ bool suspending_map = false;
+ int priority;
int rc;

+ priority = (priority_str) ? atoi(priority_str) : 0;
+
json_req = ulfius_get_json_body_request(req, &json_err);
if (!json_req) {
LOGP(DREST, LOGL_NOTICE, "REST: No JSON Body\n");
@@ -325,13 +336,47 @@
rc = json2slotmap(&slotmap, json_req);
if (rc < 0)
goto err;
- map = slotmap_add(g_rps->slotmaps, &slotmap.bank, &slotmap.client, 0);
+
+ if (slotmap_by_bank_and_prio(g_rps->slotmaps, &slotmap.bank, priority)) {
+ LOGP(DREST, LOGL_NOTICE, "REST: Slotmap to bank with same priority already exits.\n");
+ goto err;
+ }
+
+ other_map = slotmap_by_bank_and_highest_prio(g_rps->slotmaps, &slotmap.bank);
+
+ /* Request suspension of mapping with lower priority, if not already requested. */
+ if (other_map && priority > other_map->priority && !other_map->suspend_requested) {
+ suspending_map = true;
+ other_map->suspend_requested = true;
+ /* If the normal mapping is already active, trigger suspension. */
+ if (other_map->state == SLMAP_S_ACTIVE) {
+ conn = bankd_conn_by_id(g_rps, other_map->bank.bank_id);
+ slotmap_state_change(other_map, SLMAP_S_DELETE_REQ, &conn->bank.maps_delreq);
+ /* Notify the conn FSM about delete request. */
+ trigger_main_thread_via_eventfd();
+ }
+ }
+
+ map = slotmap_add(g_rps->slotmaps, &slotmap.bank, &slotmap.client, priority);
if (!map) {
LOGP(DREST, LOGL_NOTICE, "REST: Cannot add slotmap\n");
goto err;
}
slotmap_state_change(map, SLMAP_S_NEW, NULL);

+ /* If there is already a mapping with higher priority. */
+ if (other_map && other_map->priority > priority) {
+ /* Put our map (not other map) into inactive state. */
+ slotmap_state_change(map, SLMAP_S_INACTIVE, NULL);
+ goto done;
+ }
+
+ /* If mapping with lower priority is suspending, wait in INACTIVE state. */
+ if (suspending_map) {
+ slotmap_state_change(map, SLMAP_S_INACTIVE, NULL);
+ goto done;
+ }
+
/* check if any already-connected bankd matches this new map. If yes, associate it */
pthread_rwlock_rdlock(&srv->rwlock);
llist_for_each_entry(conn, &srv->banks, list) {
@@ -344,7 +389,7 @@
}
pthread_rwlock_unlock(&srv->rwlock);

-
+done:
json_decref(json_req);
ulfius_set_empty_body_response(resp, 201);

@@ -402,9 +447,11 @@
static int api_cb_slotmaps_del(const struct _u_request *req, struct _u_response *resp, void *user_data)
{
const char *slotmap_id_str = u_map_get(req->map_url, "slotmap_id");
+ const char *priority_str = u_map_get(req->map_url, "priority");
struct slot_mapping *map;
int status = 404;
unsigned long map_id;
+ int priority;

if (!slotmap_id_str) {
status = 400;
@@ -416,16 +463,17 @@
goto err;
}

+ priority = (priority_str) ? atoi(priority_str) : 0;
+
slotmaps_wrlock(g_rps->slotmaps);
llist_for_each_entry(map, &g_rps->slotmaps->mappings, list) {
- if (slotmap_get_id(map) == map_id) {
- _slotmap_mark_deleted(map);
- status = 200;
- break;
- }
+ if (slotmap_get_id(map) != map_id || map->priority != priority)
+ continue;
+ _slotmap_mark_deleted(map);
+ status = 200;
+ break;
}
slotmaps_unlock(g_rps->slotmaps);
- trigger_main_thread_via_eventfd();


ulfius_set_empty_body_response(resp, status);
diff --git a/src/server/rspro_server.c b/src/server/rspro_server.c
index 10c74e5..a6172b8 100644
--- a/src/server/rspro_server.c
+++ b/src/server/rspro_server.c
@@ -21,6 +21,8 @@

#define S(x) (1 << (x))

+extern struct rspro_server *g_rps;
+
static RsproPDU_t *slotmap2CreateMappingReq(const struct slot_mapping *slotmap)
{
ClientSlot_t clslot;
@@ -381,6 +383,9 @@
/* Link all known mappings to this new bank */
slotmaps_wrlock(slotmaps);
llist_for_each_entry(map, &slotmaps->mappings, list) {
+ /* Skip inactive mappings. */
+ if (map->state == SLMAP_S_INACTIVE)
+ continue;
if (map->bank.bank_id == conn->bank.bank_id)
_slotmap_state_change(map, SLMAP_S_NEW, &conn->bank.maps_new);
}
@@ -410,7 +415,8 @@
struct rspro_client_conn *conn = fi->priv;
struct slotmaps *slotmaps = conn->srv->slotmaps;
const __attribute__((unused)) RsproPDU_t *rx = NULL;
- struct slot_mapping *map, *map2;
+ struct slot_mapping *map, *map2, *other_map;
+ struct bank_slot bank;

switch (event) {
case CLNTC_E_CREATE_MAP_RES: /* Bankd acknowledges mapping was created */
@@ -427,6 +433,14 @@
_slotmap_state_change(map, SLMAP_S_ACTIVE, &conn->bank.maps_active);
slotmaps_unlock(slotmaps);
_update_client_for_slotmap(map, conn->srv, conn);
+ /* If mapping needs to be suspended while it was created, suspend it now.
+ * This solves the race condition, where the mapping needs to be suspended
+ * while it is being activated. */
+ if (map->suspend_requested) {
+ RsproPDU_t *pdu = slotmap2RemoveMappingReq(map);
+ client_conn_send(conn, pdu);
+ slotmap_state_change(map, SLMAP_S_DELETING, &conn->bank.maps_deleting);
+ }
break;
case CLNTC_E_REMOVE_MAP_RES: /* Bankd acknowledges mapping was removed */
rx = data;
@@ -443,8 +457,37 @@
/* update client! */
OSMO_ASSERT(map->state == SLMAP_S_DELETING);
_update_client_for_slotmap(map, conn->srv, conn);
+ /* If mapping has been suspended, put it into INACTIVE state. */
+ if (map->suspend_requested) {
+ slotmap_state_change(map, SLMAP_S_INACTIVE, NULL);
+ map->suspend_requested = false;
+ /* Search for mapping with the highest priority, so it can be created. */
+ other_map = slotmap_by_bank_and_highest_prio(slotmaps, &map->bank);
+ if (other_map == map) {
+ /* If there is no higher priority mapping found, resume suspended mapping by sending
+ * a create request. This solves the race condition, where the higher priority mapping
+ * gets removed, while the lower priority mapping was suspended. */
+ LOGPFSML(fi, LOGL_NOTICE,
+ "Mapping with higher priority is gone, resuming client=%d:%d\n",
+ map->client.client_id, map->client.slot_nr);
+ }
+ /* Send create request for the currently highest priority mapping. */
+ RsproPDU_t *pdu = slotmap2CreateMappingReq(other_map);
+ client_conn_send(conn, pdu);
+ slotmap_state_change(other_map, SLMAP_S_UNACKNOWLEDGED, &conn->bank.maps_unack);
+ break;
+ }
/* slotmap_del() will remove it from both global and bank list */
+ bank = map->bank;
slotmap_del(map->maps, map);
+ /* If there is a lower priority mapping, resume it. */
+ other_map = slotmap_by_bank_and_highest_prio(slotmaps, &bank);
+ if (other_map) {
+ /* Send create request for resuming normal mapping. */
+ RsproPDU_t *pdu = slotmap2CreateMappingReq(other_map);
+ client_conn_send(conn, pdu);
+ slotmap_state_change(other_map, SLMAP_S_UNACKNOWLEDGED, &conn->bank.maps_unack);
+ }
break;
case CLNTC_E_PUSH: /* check if any create or delete requests are pending */
slotmaps_wrlock(slotmaps);
diff --git a/src/slotmap.h b/src/slotmap.h
index b1180b4..756c29f 100644
--- a/src/slotmap.h
+++ b/src/slotmap.h
@@ -63,6 +63,8 @@
/* priority of mapping: any signed integer (negative, 0, or positive);
* higher value means higher priority; default is 0 */
int priority;
+ /* this mapping needs to be suspended */
+ bool suspend_requested;
#endif
};

To view, visit change 43745. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: osmo-remsim
Gerrit-Branch: master
Gerrit-Change-Id: I885b3b529276f46fbbe6d86be2170b0bffbb8434
Gerrit-Change-Number: 43745
Gerrit-PatchSet: 12
Gerrit-Owner: jolly <andreas@eversberg.eu>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge@osmocom.org>
Gerrit-Reviewer: lynxis <lynxis@fe80.eu>
Gerrit-Reviewer: pespin <pespin@sysmocom.de>