blob: fe0c6b7eece6f7651fac77c995ec4d5381bc0477 [file] [log] [blame]
Harald Welte3dcdd202019-03-09 13:06:46 +01001/* (C) 2018-2019 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Weltecbd18962019-03-03 19:02:38 +010023
24#include <stdint.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <errno.h>
28
29#include <pthread.h>
30
31#include <talloc.h>
32
33#include <osmocom/core/linuxlist.h>
Harald Welte91a0a4a2019-03-03 20:56:06 +010034#include <osmocom/core/utils.h>
Harald Weltecbd18962019-03-03 19:02:38 +010035
36#include "slotmap.h"
Harald Welte4b676bc2019-03-07 21:01:38 +010037#include "debug.h"
Harald Weltecbd18962019-03-03 19:02:38 +010038
Harald Welte91a0a4a2019-03-03 20:56:06 +010039const struct value_string slot_map_state_name[] = {
40 { SLMAP_S_NEW, "NEW" },
41 { SLMAP_S_UNACKNOWLEDGED, "UNACKNOWLEDGED" },
42 { SLMAP_S_ACTIVE, "ACTIVE" },
Harald Welte2bf39e02019-03-06 16:05:20 +010043 { SLMAP_S_DELETE_REQ, "DELETE_REQ" },
Harald Welte91a0a4a2019-03-03 20:56:06 +010044 { SLMAP_S_DELETING, "DELETING" },
45 { 0, NULL }
46};
47
Harald Weltefaef8f02019-03-03 20:55:22 +010048const char *slotmap_name(char *buf, size_t buf_len, const struct slot_mapping *map)
49{
50 snprintf(buf, buf_len, "B(%u:%u) <-> C(%u:%u)",
51 map->bank.bank_id, map->bank.slot_nr, map->client.client_id, map->client.slot_nr);
52 return buf;
53}
54
Harald Weltec8656832019-03-06 16:05:42 +010055uint32_t slotmap_get_id(const struct slot_mapping *map)
56{
57 return (map->bank.bank_id << 16) | map->bank.slot_nr;
58}
Harald Weltefaef8f02019-03-03 20:55:22 +010059
Harald Weltecbd18962019-03-03 19:02:38 +010060/* thread-safe lookup of map by client:slot */
61struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client)
62{
63 struct slot_mapping *map;
64
Harald Welte648f4e32019-03-07 21:14:47 +010065 slotmaps_rdlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010066 llist_for_each_entry(map, &maps->mappings, list) {
67 if (client_slot_equals(&map->client, client)) {
Harald Welte648f4e32019-03-07 21:14:47 +010068 slotmaps_unlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010069 return map;
70 }
71 }
Harald Welte648f4e32019-03-07 21:14:47 +010072 slotmaps_unlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010073 return NULL;
74}
75
76/* thread-safe lookup of map by bank:slot */
77struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank)
78{
79 struct slot_mapping *map;
80
Harald Welte648f4e32019-03-07 21:14:47 +010081 slotmaps_rdlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010082 llist_for_each_entry(map, &maps->mappings, list) {
83 if (bank_slot_equals(&map->bank, bank)) {
Harald Welte648f4e32019-03-07 21:14:47 +010084 slotmaps_unlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010085 return map;
86 }
87 }
Harald Welte648f4e32019-03-07 21:14:47 +010088 slotmaps_unlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010089 return NULL;
90
91}
92
93/* thread-safe creating of a new bank<->client map */
Harald Weltef4e75042019-03-03 21:54:52 +010094struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank,
95 const struct client_slot *client)
Harald Weltecbd18962019-03-03 19:02:38 +010096{
97 struct slot_mapping *map;
Harald Weltefaef8f02019-03-03 20:55:22 +010098 char mapname[64];
Harald Weltecbd18962019-03-03 19:02:38 +010099
100 /* We assume a single thread (main thread) will ever update the mappings,
101 * and hence we don't have any races by first grabbing + releasing the read
102 * lock twice before grabbing the writelock below */
103
104 map = slotmap_by_bank(maps, bank);
105 if (map) {
Harald Weltea9d7ad12021-12-08 21:09:12 +0100106 LOGP(DSLOTMAP, LOGL_ERROR, "BANKD %u:%u already in use, cannot add new map\n",
Harald Weltecbd18962019-03-03 19:02:38 +0100107 bank->bank_id, bank->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +0100108 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +0100109 }
110
111 map = slotmap_by_client(maps, client);
112 if (map) {
Harald Weltea9d7ad12021-12-08 21:09:12 +0100113 LOGP(DSLOTMAP, LOGL_ERROR, "CLIENT %u:%u already in use, cannot add new map\n",
Harald Weltecbd18962019-03-03 19:02:38 +0100114 client->client_id, client->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +0100115 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +0100116 }
117
118 /* allocate new mapping and add to list of mappings */
119 map = talloc_zero(maps, struct slot_mapping);
120 if (!map)
Harald Weltef4e75042019-03-03 21:54:52 +0100121 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +0100122
Harald Welte91a0a4a2019-03-03 20:56:06 +0100123 map->maps = maps;
Harald Weltecbd18962019-03-03 19:02:38 +0100124 map->bank = *bank;
125 map->client = *client;
126
Harald Welte648f4e32019-03-07 21:14:47 +0100127 slotmaps_wrlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +0100128 llist_add_tail(&map->list, &maps->mappings);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100129#ifdef REMSIM_SERVER
130 map->state = SLMAP_S_NEW;
131 INIT_LLIST_HEAD(&map->bank_list); /* to ensure llist_del() always succeeds */
132#endif
Harald Welte648f4e32019-03-07 21:14:47 +0100133 slotmaps_unlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +0100134
Harald Weltea9d7ad12021-12-08 21:09:12 +0100135 LOGP(DSLOTMAP, LOGL_INFO, "Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100136
Harald Weltef4e75042019-03-03 21:54:52 +0100137 return map;
Harald Weltecbd18962019-03-03 19:02:38 +0100138}
139
140/* thread-safe removal of a bank<->client map */
Harald Welte294298c2019-03-07 10:08:25 +0100141void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
Harald Weltecbd18962019-03-03 19:02:38 +0100142{
Harald Weltefaef8f02019-03-03 20:55:22 +0100143 char mapname[64];
144
Harald Weltea9d7ad12021-12-08 21:09:12 +0100145 LOGP(DSLOTMAP, LOGL_INFO, "Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100146
Harald Weltecbd18962019-03-03 19:02:38 +0100147 llist_del(&map->list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100148#ifdef REMSIM_SERVER
149 llist_del(&map->bank_list);
150#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100151
152 talloc_free(map);
153}
Harald Welte294298c2019-03-07 10:08:25 +0100154/* thread-safe removal of a bank<->client map */
155void slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
156{
Harald Welte648f4e32019-03-07 21:14:47 +0100157 slotmaps_wrlock(maps);
Harald Welte294298c2019-03-07 10:08:25 +0100158 _slotmap_del(maps, map);
Harald Welte648f4e32019-03-07 21:14:47 +0100159 slotmaps_unlock(maps);
Harald Welte294298c2019-03-07 10:08:25 +0100160}
Harald Weltecbd18962019-03-03 19:02:38 +0100161
Harald Weltef34fb792019-12-04 19:51:32 +0100162
163/* thread-safe removal of all bank<->client maps */
164void slotmap_del_all(struct slotmaps *maps)
165{
166 struct slot_mapping *map, *map2;
167
168 slotmaps_wrlock(maps);
169 llist_for_each_entry_safe(map, map2, &maps->mappings, list) {
170 _slotmap_del(maps, map);
171 }
172 slotmaps_unlock(maps);
173}
174
Harald Weltecbd18962019-03-03 19:02:38 +0100175struct slotmaps *slotmap_init(void *ctx)
176{
177 struct slotmaps *sm = talloc_zero(ctx, struct slotmaps);
178
179 INIT_LLIST_HEAD(&sm->mappings);
180 pthread_rwlock_init(&sm->rwlock, NULL);
181
182 return sm;
183}
Harald Welte91a0a4a2019-03-03 20:56:06 +0100184
185#ifdef REMSIM_SERVER
186
Harald Welte4b676bc2019-03-07 21:01:38 +0100187void _Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
188 struct llist_head *new_bank_list, const char *file, int line)
Harald Welte91a0a4a2019-03-03 20:56:06 +0100189{
190 char mapname[64];
191
Harald Welte4b676bc2019-03-07 21:01:38 +0100192 LOGPSRC(DMAIN, LOGL_INFO, file, line, "Slot Map %s state change: %s -> %s\n",
193 slotmap_name(mapname, sizeof(mapname), map),
Harald Welte91a0a4a2019-03-03 20:56:06 +0100194 get_value_string(slot_map_state_name, map->state),
195 get_value_string(slot_map_state_name, new_state));
196
197 map->state = new_state;
Harald Welte91a0a4a2019-03-03 20:56:06 +0100198 llist_del(&map->bank_list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100199 if (new_bank_list)
200 llist_add_tail(&map->bank_list, new_bank_list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100201 else
202 INIT_LLIST_HEAD(&map->bank_list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100203}
204
205
Harald Welte4b676bc2019-03-07 21:01:38 +0100206void Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
207 struct llist_head *new_bank_list, const char *file, int line)
Harald Welte91a0a4a2019-03-03 20:56:06 +0100208{
Harald Welte648f4e32019-03-07 21:14:47 +0100209 slotmaps_wrlock(map->maps);
Harald Welte4b676bc2019-03-07 21:01:38 +0100210 _Slotmap_state_change(map, new_state, new_bank_list, file, line);
Harald Welte648f4e32019-03-07 21:14:47 +0100211 slotmaps_unlock(map->maps);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100212}
213
214#endif