laforge submitted this change.

View Change

Approvals: laforge: Looks good to me, approved Jenkins Builder: Verified pespin: Looks good to me, but someone else must approve
Add priority to slot_mapping structure

In order to store two or more mappings to the same bank/slot, a priority
attribute is introduced. Multiple mappings to the same bank/slot is only
allowed if the priority values differ. This will be used in later
patches to allow mappings to maintenance clients (higher priority),
while having inactive mappings to regular clients (lower/default
priority).

Related: SYS#8159
Change-Id: Ibc9f771cda5d3ba94934cb04e00e60acd466e8bf
---
M src/bankd/bankd_main.c
M src/server/rest_api.c
M src/slotmap.c
M src/slotmap.h
4 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/src/bankd/bankd_main.c b/src/bankd/bankd_main.c
index f924cfb..c85f678 100644
--- a/src/bankd/bankd_main.c
+++ b/src/bankd/bankd_main.c
@@ -229,7 +229,7 @@
}

/* Add a new mapping */
- map = slotmap_add(g_bankd->slotmaps, &bs, &cs);
+ map = slotmap_add(g_bankd->slotmaps, &bs, &cs, 0);
if (!map) {
LOGPFSML(srvc->fi, LOGL_ERROR, "could not create slotmap\n");
resp = rspro_gen_CreateMappingRes(ResultCode_illegalSlotId);
diff --git a/src/server/rest_api.c b/src/server/rest_api.c
index 6e025ad..2dc54a2 100644
--- a/src/server/rest_api.c
+++ b/src/server/rest_api.c
@@ -325,7 +325,7 @@
rc = json2slotmap(&slotmap, json_req);
if (rc < 0)
goto err;
- map = slotmap_add(g_rps->slotmaps, &slotmap.bank, &slotmap.client);
+ map = slotmap_add(g_rps->slotmaps, &slotmap.bank, &slotmap.client, 0);
if (!map) {
LOGP(DREST, LOGL_NOTICE, "REST: Cannot add slotmap\n");
goto err;
diff --git a/src/slotmap.c b/src/slotmap.c
index 1883cf5..57cb5b4 100644
--- a/src/slotmap.c
+++ b/src/slotmap.c
@@ -92,9 +92,45 @@
return NULL;
}

+#ifdef REMSIM_SERVER
+/* thread-safe lookup of map by bank:slot with specific priority */
+struct slot_mapping *slotmap_by_bank_and_prio(struct slotmaps *maps, const struct bank_slot *bank, int priority)
+{
+ struct slot_mapping *map;
+
+ slotmaps_rdlock(maps);
+ llist_for_each_entry(map, &maps->mappings, list) {
+ if (map->priority != priority)
+ continue;
+ if (bank_slot_equals(&map->bank, bank)) {
+ slotmaps_unlock(maps);
+ return map;
+ }
+ }
+ slotmaps_unlock(maps);
+ return NULL;
+}
+
+/* thread-safe lookup of map by bank:slot with highest priority */
+struct slot_mapping *slotmap_by_bank_and_highest_prio(struct slotmaps *maps, const struct bank_slot *bank)
+{
+ struct slot_mapping *map_found = NULL, *map;
+
+ slotmaps_rdlock(maps);
+ llist_for_each_entry(map, &maps->mappings, list) {
+ if (bank_slot_equals(&map->bank, bank)) {
+ if (!map_found || map->priority > map_found->priority)
+ map_found = map;
+ }
+ }
+ slotmaps_unlock(maps);
+ return map_found;
+}
+#endif
+
/* thread-safe creating of a new bank<->client map */
-struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank,
- const struct client_slot *client)
+struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, const struct client_slot *client,
+ int priority)
{
struct slot_mapping *map;
char mapname[64];
@@ -103,10 +139,14 @@
* and hence we don't have any races by first grabbing + releasing the read
* lock twice before grabbing the writelock below */

+#ifdef REMSIM_SERVER
+ map = slotmap_by_bank_and_prio(maps, bank, priority);
+#else
map = slotmap_by_bank(maps, bank);
+#endif
if (map) {
- LOGP(DSLOTMAP, LOGL_ERROR, "BANKD %u:%u already in use, cannot add new map\n",
- bank->bank_id, bank->slot_nr);
+ LOGP(DSLOTMAP, LOGL_ERROR, "BANKD %u:%u (priority %d) already in use, cannot add new map\n",
+ bank->bank_id, bank->slot_nr, priority);
return NULL;
}

@@ -131,6 +171,7 @@
#ifdef REMSIM_SERVER
map->state = SLMAP_S_NEW;
INIT_LLIST_HEAD(&map->bank_list); /* to ensure llist_del() always succeeds */
+ map->priority = priority;
#endif
slotmaps_unlock(maps);

diff --git a/src/slotmap.h b/src/slotmap.h
index a14e1cf..9128e70 100644
--- a/src/slotmap.h
+++ b/src/slotmap.h
@@ -59,6 +59,9 @@
#ifdef REMSIM_SERVER
struct llist_head bank_list;
enum slot_mapping_state state;
+ /* priority of mapping: any signed integer (negative, 0, or positive);
+ * higher value means higher priority; default is 0 */
+ int priority;
#endif
};

@@ -79,8 +82,17 @@
/* thread-safe lookup of map by bank:slot */
struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank);

+#ifdef REMSIM_SERVER
+/* thread-safe lookup of map by bank:slot with specific priority */
+struct slot_mapping *slotmap_by_bank_and_prio(struct slotmaps *maps, const struct bank_slot *bank, int priority);
+
+/* thread-safe lookup of map by bank:slot with highest priority */
+struct slot_mapping *slotmap_by_bank_and_highest_prio(struct slotmaps *maps, const struct bank_slot *bank);
+#endif
+
/* thread-safe creating of a new bank<->client map */
-struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, const struct client_slot *client);
+struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, const struct client_slot *client,
+ int priority);

/* thread-safe removal of a bank<->client map */
void slotmap_del(struct slotmaps *maps, struct slot_mapping *map);

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

Gerrit-MessageType: merged
Gerrit-Project: osmo-remsim
Gerrit-Branch: master
Gerrit-Change-Id: Ibc9f771cda5d3ba94934cb04e00e60acd466e8bf
Gerrit-Change-Number: 43743
Gerrit-PatchSet: 9
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>