2018-08-14 21:47:30 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include <talloc.h>
|
|
|
|
|
|
|
|
#include <osmocom/core/linuxlist.h>
|
2019-03-03 19:56:06 +00:00
|
|
|
#include <osmocom/core/utils.h>
|
2018-08-14 21:47:30 +00:00
|
|
|
|
2019-03-03 18:02:38 +00:00
|
|
|
#include "slotmap.h"
|
2019-03-07 20:01:38 +00:00
|
|
|
#include "debug.h"
|
2018-08-14 21:47:30 +00:00
|
|
|
|
2019-03-03 19:56:06 +00:00
|
|
|
const struct value_string slot_map_state_name[] = {
|
|
|
|
{ SLMAP_S_NEW, "NEW" },
|
|
|
|
{ SLMAP_S_UNACKNOWLEDGED, "UNACKNOWLEDGED" },
|
|
|
|
{ SLMAP_S_ACTIVE, "ACTIVE" },
|
2019-03-06 15:05:20 +00:00
|
|
|
{ SLMAP_S_DELETE_REQ, "DELETE_REQ" },
|
2019-03-03 19:56:06 +00:00
|
|
|
{ SLMAP_S_DELETING, "DELETING" },
|
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
2019-03-03 19:55:22 +00:00
|
|
|
const char *slotmap_name(char *buf, size_t buf_len, const struct slot_mapping *map)
|
|
|
|
{
|
|
|
|
snprintf(buf, buf_len, "B(%u:%u) <-> C(%u:%u)",
|
|
|
|
map->bank.bank_id, map->bank.slot_nr, map->client.client_id, map->client.slot_nr);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2019-03-06 15:05:42 +00:00
|
|
|
uint32_t slotmap_get_id(const struct slot_mapping *map)
|
|
|
|
{
|
|
|
|
return (map->bank.bank_id << 16) | map->bank.slot_nr;
|
|
|
|
}
|
2019-03-03 19:55:22 +00:00
|
|
|
|
2018-08-14 21:47:30 +00:00
|
|
|
/* thread-safe lookup of map by client:slot */
|
2019-03-03 18:02:38 +00:00
|
|
|
struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client)
|
2018-08-14 21:47:30 +00:00
|
|
|
{
|
2019-03-03 18:02:38 +00:00
|
|
|
struct slot_mapping *map;
|
2018-08-14 21:47:30 +00:00
|
|
|
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_rdlock(maps);
|
2019-03-03 18:02:38 +00:00
|
|
|
llist_for_each_entry(map, &maps->mappings, list) {
|
2018-08-14 21:47:30 +00:00
|
|
|
if (client_slot_equals(&map->client, client)) {
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_unlock(maps);
|
2018-08-14 21:47:30 +00:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_unlock(maps);
|
2018-08-14 21:47:30 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thread-safe lookup of map by bank:slot */
|
2019-03-03 18:02:38 +00:00
|
|
|
struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank)
|
2018-08-14 21:47:30 +00:00
|
|
|
{
|
2019-03-03 18:02:38 +00:00
|
|
|
struct slot_mapping *map;
|
2018-08-14 21:47:30 +00:00
|
|
|
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_rdlock(maps);
|
2019-03-03 18:02:38 +00:00
|
|
|
llist_for_each_entry(map, &maps->mappings, list) {
|
2018-08-14 21:47:30 +00:00
|
|
|
if (bank_slot_equals(&map->bank, bank)) {
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_unlock(maps);
|
2018-08-14 21:47:30 +00:00
|
|
|
return map;
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_unlock(maps);
|
2018-08-14 21:47:30 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thread-safe creating of a new bank<->client map */
|
2019-03-03 20:54:52 +00:00
|
|
|
struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank,
|
|
|
|
const struct client_slot *client)
|
2018-08-14 21:47:30 +00:00
|
|
|
{
|
2019-03-03 18:02:38 +00:00
|
|
|
struct slot_mapping *map;
|
2019-03-03 19:55:22 +00:00
|
|
|
char mapname[64];
|
2018-08-14 21:47:30 +00:00
|
|
|
|
|
|
|
/* We assume a single thread (main thread) will ever update the mappings,
|
|
|
|
* and hence we don't have any races by first grabbing + releasing the read
|
|
|
|
* lock twice before grabbing the writelock below */
|
|
|
|
|
2019-03-03 18:02:38 +00:00
|
|
|
map = slotmap_by_bank(maps, bank);
|
2018-08-14 21:47:30 +00:00
|
|
|
if (map) {
|
|
|
|
fprintf(stderr, "BANKD %u:%u already in use, cannot add new map\n",
|
|
|
|
bank->bank_id, bank->slot_nr);
|
2019-03-03 20:54:52 +00:00
|
|
|
return NULL;
|
2018-08-14 21:47:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 18:02:38 +00:00
|
|
|
map = slotmap_by_client(maps, client);
|
2018-08-14 21:47:30 +00:00
|
|
|
if (map) {
|
|
|
|
fprintf(stderr, "CLIENT %u:%u already in use, cannot add new map\n",
|
|
|
|
client->client_id, client->slot_nr);
|
2019-03-03 20:54:52 +00:00
|
|
|
return NULL;
|
2018-08-14 21:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate new mapping and add to list of mappings */
|
2019-03-03 18:02:38 +00:00
|
|
|
map = talloc_zero(maps, struct slot_mapping);
|
2018-08-14 21:47:30 +00:00
|
|
|
if (!map)
|
2019-03-03 20:54:52 +00:00
|
|
|
return NULL;
|
2018-08-14 21:47:30 +00:00
|
|
|
|
2019-03-03 19:56:06 +00:00
|
|
|
map->maps = maps;
|
2018-08-14 21:47:30 +00:00
|
|
|
map->bank = *bank;
|
|
|
|
map->client = *client;
|
|
|
|
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_wrlock(maps);
|
2019-03-03 18:02:38 +00:00
|
|
|
llist_add_tail(&map->list, &maps->mappings);
|
2019-03-03 19:56:06 +00:00
|
|
|
#ifdef REMSIM_SERVER
|
|
|
|
map->state = SLMAP_S_NEW;
|
|
|
|
INIT_LLIST_HEAD(&map->bank_list); /* to ensure llist_del() always succeeds */
|
|
|
|
#endif
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_unlock(maps);
|
2018-08-14 21:47:30 +00:00
|
|
|
|
2019-03-03 19:55:22 +00:00
|
|
|
printf("Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map));
|
2018-08-14 21:47:30 +00:00
|
|
|
|
2019-03-03 20:54:52 +00:00
|
|
|
return map;
|
2018-08-14 21:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* thread-safe removal of a bank<->client map */
|
2019-03-07 09:08:25 +00:00
|
|
|
void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
|
2018-08-14 21:47:30 +00:00
|
|
|
{
|
2019-03-03 19:55:22 +00:00
|
|
|
char mapname[64];
|
|
|
|
|
|
|
|
printf("Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map));
|
2018-08-14 21:47:30 +00:00
|
|
|
|
|
|
|
llist_del(&map->list);
|
2019-03-03 19:56:06 +00:00
|
|
|
#ifdef REMSIM_SERVER
|
|
|
|
llist_del(&map->bank_list);
|
|
|
|
#endif
|
2018-08-14 21:47:30 +00:00
|
|
|
|
|
|
|
talloc_free(map);
|
|
|
|
}
|
2019-03-07 09:08:25 +00:00
|
|
|
/* thread-safe removal of a bank<->client map */
|
|
|
|
void slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
|
|
|
|
{
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_wrlock(maps);
|
2019-03-07 09:08:25 +00:00
|
|
|
_slotmap_del(maps, map);
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_unlock(maps);
|
2019-03-07 09:08:25 +00:00
|
|
|
}
|
2019-03-03 18:02:38 +00:00
|
|
|
|
|
|
|
struct slotmaps *slotmap_init(void *ctx)
|
|
|
|
{
|
|
|
|
struct slotmaps *sm = talloc_zero(ctx, struct slotmaps);
|
|
|
|
|
|
|
|
INIT_LLIST_HEAD(&sm->mappings);
|
|
|
|
pthread_rwlock_init(&sm->rwlock, NULL);
|
|
|
|
|
|
|
|
return sm;
|
|
|
|
}
|
2019-03-03 19:56:06 +00:00
|
|
|
|
|
|
|
#ifdef REMSIM_SERVER
|
|
|
|
|
2019-03-07 20:01:38 +00:00
|
|
|
void _Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
|
|
|
|
struct llist_head *new_bank_list, const char *file, int line)
|
2019-03-03 19:56:06 +00:00
|
|
|
{
|
|
|
|
char mapname[64];
|
|
|
|
|
2019-03-07 20:01:38 +00:00
|
|
|
LOGPSRC(DMAIN, LOGL_INFO, file, line, "Slot Map %s state change: %s -> %s\n",
|
|
|
|
slotmap_name(mapname, sizeof(mapname), map),
|
2019-03-03 19:56:06 +00:00
|
|
|
get_value_string(slot_map_state_name, map->state),
|
|
|
|
get_value_string(slot_map_state_name, new_state));
|
|
|
|
|
|
|
|
map->state = new_state;
|
|
|
|
llist_del(&map->bank_list);
|
|
|
|
if (new_bank_list)
|
|
|
|
llist_add_tail(&map->bank_list, new_bank_list);
|
|
|
|
else
|
|
|
|
INIT_LLIST_HEAD(&map->bank_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-07 20:01:38 +00:00
|
|
|
void Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
|
|
|
|
struct llist_head *new_bank_list, const char *file, int line)
|
2019-03-03 19:56:06 +00:00
|
|
|
{
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_wrlock(map->maps);
|
2019-03-07 20:01:38 +00:00
|
|
|
_Slotmap_state_change(map, new_state, new_bank_list, file, line);
|
2019-03-07 20:14:47 +00:00
|
|
|
slotmaps_unlock(map->maps);
|
2019-03-03 19:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|