blob: f4a22e186df0e098f44633169e91859cb5ffdf5f [file] [log] [blame]
Harald Weltecbd18962019-03-03 19:02:38 +01001
2#include <stdint.h>
3#include <stdlib.h>
4#include <stdio.h>
5#include <errno.h>
6
7#include <pthread.h>
8
9#include <talloc.h>
10
11#include <osmocom/core/linuxlist.h>
Harald Welte91a0a4a2019-03-03 20:56:06 +010012#include <osmocom/core/utils.h>
Harald Weltecbd18962019-03-03 19:02:38 +010013
14#include "slotmap.h"
Harald Welte4b676bc2019-03-07 21:01:38 +010015#include "debug.h"
Harald Weltecbd18962019-03-03 19:02:38 +010016
Harald Welte91a0a4a2019-03-03 20:56:06 +010017const struct value_string slot_map_state_name[] = {
18 { SLMAP_S_NEW, "NEW" },
19 { SLMAP_S_UNACKNOWLEDGED, "UNACKNOWLEDGED" },
20 { SLMAP_S_ACTIVE, "ACTIVE" },
Harald Welte2bf39e02019-03-06 16:05:20 +010021 { SLMAP_S_DELETE_REQ, "DELETE_REQ" },
Harald Welte91a0a4a2019-03-03 20:56:06 +010022 { SLMAP_S_DELETING, "DELETING" },
23 { 0, NULL }
24};
25
Harald Weltefaef8f02019-03-03 20:55:22 +010026const char *slotmap_name(char *buf, size_t buf_len, const struct slot_mapping *map)
27{
28 snprintf(buf, buf_len, "B(%u:%u) <-> C(%u:%u)",
29 map->bank.bank_id, map->bank.slot_nr, map->client.client_id, map->client.slot_nr);
30 return buf;
31}
32
Harald Weltec8656832019-03-06 16:05:42 +010033uint32_t slotmap_get_id(const struct slot_mapping *map)
34{
35 return (map->bank.bank_id << 16) | map->bank.slot_nr;
36}
Harald Weltefaef8f02019-03-03 20:55:22 +010037
Harald Weltecbd18962019-03-03 19:02:38 +010038/* thread-safe lookup of map by client:slot */
39struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client)
40{
41 struct slot_mapping *map;
42
43 pthread_rwlock_rdlock(&maps->rwlock);
44 llist_for_each_entry(map, &maps->mappings, list) {
45 if (client_slot_equals(&map->client, client)) {
46 pthread_rwlock_unlock(&maps->rwlock);
47 return map;
48 }
49 }
50 pthread_rwlock_unlock(&maps->rwlock);
51 return NULL;
52}
53
54/* thread-safe lookup of map by bank:slot */
55struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank)
56{
57 struct slot_mapping *map;
58
59 pthread_rwlock_rdlock(&maps->rwlock);
60 llist_for_each_entry(map, &maps->mappings, list) {
61 if (bank_slot_equals(&map->bank, bank)) {
62 pthread_rwlock_unlock(&maps->rwlock);
63 return map;
64 }
65 }
66 pthread_rwlock_unlock(&maps->rwlock);
67 return NULL;
68
69}
70
71/* thread-safe creating of a new bank<->client map */
Harald Weltef4e75042019-03-03 21:54:52 +010072struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank,
73 const struct client_slot *client)
Harald Weltecbd18962019-03-03 19:02:38 +010074{
75 struct slot_mapping *map;
Harald Weltefaef8f02019-03-03 20:55:22 +010076 char mapname[64];
Harald Weltecbd18962019-03-03 19:02:38 +010077
78 /* We assume a single thread (main thread) will ever update the mappings,
79 * and hence we don't have any races by first grabbing + releasing the read
80 * lock twice before grabbing the writelock below */
81
82 map = slotmap_by_bank(maps, bank);
83 if (map) {
84 fprintf(stderr, "BANKD %u:%u already in use, cannot add new map\n",
85 bank->bank_id, bank->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +010086 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010087 }
88
89 map = slotmap_by_client(maps, client);
90 if (map) {
91 fprintf(stderr, "CLIENT %u:%u already in use, cannot add new map\n",
92 client->client_id, client->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +010093 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +010094 }
95
96 /* allocate new mapping and add to list of mappings */
97 map = talloc_zero(maps, struct slot_mapping);
98 if (!map)
Harald Weltef4e75042019-03-03 21:54:52 +010099 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +0100100
Harald Welte91a0a4a2019-03-03 20:56:06 +0100101 map->maps = maps;
Harald Weltecbd18962019-03-03 19:02:38 +0100102 map->bank = *bank;
103 map->client = *client;
104
105 pthread_rwlock_wrlock(&maps->rwlock);
106 llist_add_tail(&map->list, &maps->mappings);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100107#ifdef REMSIM_SERVER
108 map->state = SLMAP_S_NEW;
109 INIT_LLIST_HEAD(&map->bank_list); /* to ensure llist_del() always succeeds */
110#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100111 pthread_rwlock_unlock(&maps->rwlock);
112
Harald Weltefaef8f02019-03-03 20:55:22 +0100113 printf("Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100114
Harald Weltef4e75042019-03-03 21:54:52 +0100115 return map;
Harald Weltecbd18962019-03-03 19:02:38 +0100116}
117
118/* thread-safe removal of a bank<->client map */
Harald Welte294298c2019-03-07 10:08:25 +0100119void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
Harald Weltecbd18962019-03-03 19:02:38 +0100120{
Harald Weltefaef8f02019-03-03 20:55:22 +0100121 char mapname[64];
122
123 printf("Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100124
Harald Weltecbd18962019-03-03 19:02:38 +0100125 llist_del(&map->list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100126#ifdef REMSIM_SERVER
127 llist_del(&map->bank_list);
128#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100129
130 talloc_free(map);
131}
Harald Welte294298c2019-03-07 10:08:25 +0100132/* thread-safe removal of a bank<->client map */
133void slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
134{
135 pthread_rwlock_wrlock(&maps->rwlock);
136 _slotmap_del(maps, map);
137 pthread_rwlock_unlock(&maps->rwlock);
138}
Harald Weltecbd18962019-03-03 19:02:38 +0100139
140struct slotmaps *slotmap_init(void *ctx)
141{
142 struct slotmaps *sm = talloc_zero(ctx, struct slotmaps);
143
144 INIT_LLIST_HEAD(&sm->mappings);
145 pthread_rwlock_init(&sm->rwlock, NULL);
146
147 return sm;
148}
Harald Welte91a0a4a2019-03-03 20:56:06 +0100149
150#ifdef REMSIM_SERVER
151
Harald Welte4b676bc2019-03-07 21:01:38 +0100152void _Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
153 struct llist_head *new_bank_list, const char *file, int line)
Harald Welte91a0a4a2019-03-03 20:56:06 +0100154{
155 char mapname[64];
156
Harald Welte4b676bc2019-03-07 21:01:38 +0100157 LOGPSRC(DMAIN, LOGL_INFO, file, line, "Slot Map %s state change: %s -> %s\n",
158 slotmap_name(mapname, sizeof(mapname), map),
Harald Welte91a0a4a2019-03-03 20:56:06 +0100159 get_value_string(slot_map_state_name, map->state),
160 get_value_string(slot_map_state_name, new_state));
161
162 map->state = new_state;
Harald Welte91a0a4a2019-03-03 20:56:06 +0100163 llist_del(&map->bank_list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100164 if (new_bank_list)
165 llist_add_tail(&map->bank_list, new_bank_list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100166 else
167 INIT_LLIST_HEAD(&map->bank_list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100168}
169
170
Harald Welte4b676bc2019-03-07 21:01:38 +0100171void Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
172 struct llist_head *new_bank_list, const char *file, int line)
Harald Welte91a0a4a2019-03-03 20:56:06 +0100173{
174 pthread_rwlock_wrlock(&map->maps->rwlock);
Harald Welte4b676bc2019-03-07 21:01:38 +0100175 _Slotmap_state_change(map, new_state, new_bank_list, file, line);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100176 pthread_rwlock_unlock(&map->maps->rwlock);
177}
178
179#endif