jolly has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-remsim/+/43745?usp=email )


Change subject: Add maintenance mapping support to osmo-remsim-server
......................................................................

Add maintenance mapping support to osmo-remsim-server

A normal mapping and a maintenance mapping may share the same bank/slot.
In this case the normal mapping will be inactive until the maintenance
mapping has been deleted. The maintenance mapping has priority over the
normal mapping.

If a maintenance mapping share the same bank/slot of an existing normal
mapping, the normal mapping is suspended. The maintenance mapping hat
priority over the mapping. The following steps are performed:

* The maintenance mapping is put into INACTIVE state.
* The normal mapping is deleted and put into INACTIVE state.
* The maintenance mapping is created.

Once the maintenance mapping is removed, the normal mapping is restored.
The following steps are performed:

* The maintenance mapping is deleted and then removed.
* The normal mapping in INACTIVE state is created again.

To control the maintenance flag, the rest API is expanded. An optional
'maintenance' tag is added to the URL.

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(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-remsim refs/changes/45/43745/1

diff --git a/src/server/rest_api.c b/src/server/rest_api.c
index 27391f5..62e4c45 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 *maintenance_str = u_map_get(req->map_url, "maintenance");
        struct slot_mapping *map;
        json_t *json_body = json_object();
        json_t *json_maps = json_array();
+       bool maintenance;
+
+       maintenance = (maintenance_str && !strcmp(maintenance_str, "true"));

        slotmaps_rdlock(g_rps->slotmaps);
        llist_for_each_entry(map, &g_rps->slotmaps->mappings, list) {
+               if (map->maintenance != maintenance)
+                       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 *maintenance_str = u_map_get(req->map_url, "maintenance");
        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;
+       bool maintenance;
        int rc;

+       maintenance = (maintenance_str && !strcmp(maintenance_str, "true"));
+
        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,46 @@
        rc = json2slotmap(&slotmap, json_req);
        if (rc < 0)
                goto err;
-       map = slotmap_add(g_rps->slotmaps, &slotmap.bank, &slotmap.client, 
false);
+
+       /* Suspend existing normal mapping. */
+       if (maintenance) {
+               other_map = slotmap_by_bank_and_maintenance(g_rps->slotmaps, 
&slotmap.bank, false);
+               if (other_map && !other_map->suspend) {
+                       suspending_map = true;
+                       other_map->suspend = 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, 
maintenance);
        if (!map) {
                LOGP(DREST, LOGL_NOTICE, "REST: Cannot add slotmap\n");
                goto err;
        }
        slotmap_state_change(map, SLMAP_S_NEW, NULL);

+       if (!map->maintenance) {
+               /* If there is already a maintenance mapping. */
+               other_map = slotmap_by_bank_and_maintenance(g_rps->slotmaps, 
&map->bank, true);
+               if (other_map) {
+                       /* Put our map (not other map) into inactive state. */
+                       slotmap_state_change(map, SLMAP_S_INACTIVE, NULL);
+                       goto done;
+               }
+       }
+
+       /* If we are suspended an exisisting maping, we stay inactive until the 
mapping was removed. */
+       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 +388,7 @@
        }
        pthread_rwlock_unlock(&srv->rwlock);
 
-
+done:
        json_decref(json_req);
        ulfius_set_empty_body_response(resp, 201);

@@ -402,9 +446,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 *maintenance_str = u_map_get(req->map_url, "maintenance");
        struct slot_mapping *map;
        int status = 404;
        unsigned long map_id;
+       bool maintenance;

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

+       maintenance = (maintenance_str && !strcmp(maintenance_str, "true"));
+
        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->maintenance != 
maintenance)
+                       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..9fbdaa9 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;
@@ -410,7 +412,7 @@
        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;

        switch (event) {
        case CLNTC_E_CREATE_MAP_RES: /* Bankd acknowledges mapping was created 
*/
@@ -427,6 +429,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 beeing activated. */
+               if (map->suspend) {
+                       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,6 +453,40 @@
                /* 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) {
+                       slotmap_state_change(map, SLMAP_S_INACTIVE, NULL);
+                       /* Search for maintenance mapping, so it can be 
created. */
+                       other_map = slotmap_by_bank_and_maintenance(slotmaps, 
&map->bank, true);
+                       if (!other_map) {
+                               LOGPFSML(fi, LOGL_NOTICE, "no maintenance 
mapping found, resuming clinet=%d:%d\n",
+                                        map->client.client_id, 
map->client.slot_nr);
+                               /* If not found, resume suspended mapping by 
sending a create request.
+                                * This solves the race condition, where the 
maintenance mapping gets removed,
+                                * while the normal mapping is beein suspended. 
*/
+                               RsproPDU_t *pdu = slotmap2CreateMappingReq(map);
+                               client_conn_send(conn, pdu);
+                               slotmap_state_change(map, 
SLMAP_S_UNACKNOWLEDGED, &conn->bank.maps_unack);
+                               map->suspend = false;
+                               break;
+                       }
+                       /* Send create request for maintenance 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;
+               }
+               /* If maintenance mapping is to be deleted, reactivate inactive 
mapping. */
+               if (map->maintenance) {
+                       /* Search for non maintenance mapping. */
+                       other_map = slotmap_by_bank_and_maintenance(slotmaps, 
&map->bank, false);
+                       if (other_map) {
+                               /* Send create request for maintenance mapping. 
*/
+                               RsproPDU_t *pdu = 
slotmap2CreateMappingReq(other_map);
+                               client_conn_send(conn, pdu);
+                               slotmap_state_change(other_map, 
SLMAP_S_UNACKNOWLEDGED, &conn->bank.maps_unack);
+                       }
+               }
                /* slotmap_del() will remove it from both global and bank list 
*/
                slotmap_del(map->maps, map);
                break;
diff --git a/src/slotmap.h b/src/slotmap.h
index fbcc356..225d453 100644
--- a/src/slotmap.h
+++ b/src/slotmap.h
@@ -62,6 +62,8 @@
        enum slot_mapping_state state;
        /* this mapping is for maintenenance */
        bool maintenance;
+       /* this mapping is (to be) suspended */
+       bool suspend;
 #endif
 };


--
To view, visit https://gerrit.osmocom.org/c/osmo-remsim/+/43745?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: osmo-remsim
Gerrit-Branch: master
Gerrit-Change-Id: I885b3b529276f46fbbe6d86be2170b0bffbb8434
Gerrit-Change-Number: 43745
Gerrit-PatchSet: 1
Gerrit-Owner: jolly <[email protected]>

Reply via email to