blob: b1be0abbd4ab10717ff15077f4351a7176eff837 [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 *
Harald Welte3dcdd202019-03-09 13:06:46 +010017 */
18
Harald Weltecbd18962019-03-03 19:02:38 +010019
20#include <stdint.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <errno.h>
24
25#include <pthread.h>
26
27#include <talloc.h>
28
29#include <osmocom/core/linuxlist.h>
Harald Welte91a0a4a2019-03-03 20:56:06 +010030#include <osmocom/core/utils.h>
Harald Weltecbd18962019-03-03 19:02:38 +010031
32#include "slotmap.h"
Harald Welte4b676bc2019-03-07 21:01:38 +010033#include "debug.h"
Harald Weltecbd18962019-03-03 19:02:38 +010034
Harald Welte91a0a4a2019-03-03 20:56:06 +010035const struct value_string slot_map_state_name[] = {
36 { SLMAP_S_NEW, "NEW" },
37 { SLMAP_S_UNACKNOWLEDGED, "UNACKNOWLEDGED" },
38 { SLMAP_S_ACTIVE, "ACTIVE" },
Harald Welte2bf39e02019-03-06 16:05:20 +010039 { SLMAP_S_DELETE_REQ, "DELETE_REQ" },
Harald Welte91a0a4a2019-03-03 20:56:06 +010040 { SLMAP_S_DELETING, "DELETING" },
41 { 0, NULL }
42};
43
Harald Weltefaef8f02019-03-03 20:55:22 +010044const char *slotmap_name(char *buf, size_t buf_len, const struct slot_mapping *map)
45{
46 snprintf(buf, buf_len, "B(%u:%u) <-> C(%u:%u)",
47 map->bank.bank_id, map->bank.slot_nr, map->client.client_id, map->client.slot_nr);
48 return buf;
49}
50
Harald Weltec8656832019-03-06 16:05:42 +010051uint32_t slotmap_get_id(const struct slot_mapping *map)
52{
53 return (map->bank.bank_id << 16) | map->bank.slot_nr;
54}
Harald Weltefaef8f02019-03-03 20:55:22 +010055
Harald Weltecbd18962019-03-03 19:02:38 +010056/* thread-safe lookup of map by client:slot */
57struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client)
58{
59 struct slot_mapping *map;
60
Harald Welte648f4e32019-03-07 21:14:47 +010061 slotmaps_rdlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010062 llist_for_each_entry(map, &maps->mappings, list) {
63 if (client_slot_equals(&map->client, client)) {
Harald Welte648f4e32019-03-07 21:14:47 +010064 slotmaps_unlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010065 return map;
66 }
67 }
Harald Welte648f4e32019-03-07 21:14:47 +010068 slotmaps_unlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010069 return NULL;
70}
71
72/* thread-safe lookup of map by bank:slot */
73struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank)
74{
75 struct slot_mapping *map;
76
Harald Welte648f4e32019-03-07 21:14:47 +010077 slotmaps_rdlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010078 llist_for_each_entry(map, &maps->mappings, list) {
79 if (bank_slot_equals(&map->bank, bank)) {
Harald Welte648f4e32019-03-07 21:14:47 +010080 slotmaps_unlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010081 return map;
82 }
83 }
Harald Welte648f4e32019-03-07 21:14:47 +010084 slotmaps_unlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +010085 return NULL;
86
87}
88
89/* thread-safe creating of a new bank<->client map */
Harald Weltef4e75042019-03-03 21:54:52 +010090struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank,
91 const struct client_slot *client)
Harald Weltecbd18962019-03-03 19:02:38 +010092{
93 struct slot_mapping *map;
Harald Weltefaef8f02019-03-03 20:55:22 +010094 char mapname[64];
Harald Weltecbd18962019-03-03 19:02:38 +010095
96 /* We assume a single thread (main thread) will ever update the mappings,
97 * and hence we don't have any races by first grabbing + releasing the read
98 * lock twice before grabbing the writelock below */
99
100 map = slotmap_by_bank(maps, bank);
101 if (map) {
Harald Weltea9d7ad12021-12-08 21:09:12 +0100102 LOGP(DSLOTMAP, LOGL_ERROR, "BANKD %u:%u already in use, cannot add new map\n",
Harald Weltecbd18962019-03-03 19:02:38 +0100103 bank->bank_id, bank->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +0100104 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +0100105 }
106
107 map = slotmap_by_client(maps, client);
108 if (map) {
Harald Weltea9d7ad12021-12-08 21:09:12 +0100109 LOGP(DSLOTMAP, LOGL_ERROR, "CLIENT %u:%u already in use, cannot add new map\n",
Harald Weltecbd18962019-03-03 19:02:38 +0100110 client->client_id, client->slot_nr);
Harald Weltef4e75042019-03-03 21:54:52 +0100111 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +0100112 }
113
114 /* allocate new mapping and add to list of mappings */
115 map = talloc_zero(maps, struct slot_mapping);
116 if (!map)
Harald Weltef4e75042019-03-03 21:54:52 +0100117 return NULL;
Harald Weltecbd18962019-03-03 19:02:38 +0100118
Harald Welte91a0a4a2019-03-03 20:56:06 +0100119 map->maps = maps;
Harald Weltecbd18962019-03-03 19:02:38 +0100120 map->bank = *bank;
121 map->client = *client;
122
Harald Welte648f4e32019-03-07 21:14:47 +0100123 slotmaps_wrlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +0100124 llist_add_tail(&map->list, &maps->mappings);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100125#ifdef REMSIM_SERVER
126 map->state = SLMAP_S_NEW;
127 INIT_LLIST_HEAD(&map->bank_list); /* to ensure llist_del() always succeeds */
128#endif
Harald Welte648f4e32019-03-07 21:14:47 +0100129 slotmaps_unlock(maps);
Harald Weltecbd18962019-03-03 19:02:38 +0100130
Harald Weltea9d7ad12021-12-08 21:09:12 +0100131 LOGP(DSLOTMAP, LOGL_INFO, "Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100132
Harald Weltef4e75042019-03-03 21:54:52 +0100133 return map;
Harald Weltecbd18962019-03-03 19:02:38 +0100134}
135
136/* thread-safe removal of a bank<->client map */
Harald Welte294298c2019-03-07 10:08:25 +0100137void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
Harald Weltecbd18962019-03-03 19:02:38 +0100138{
Harald Weltefaef8f02019-03-03 20:55:22 +0100139 char mapname[64];
140
Harald Weltea9d7ad12021-12-08 21:09:12 +0100141 LOGP(DSLOTMAP, LOGL_INFO, "Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map));
Harald Weltecbd18962019-03-03 19:02:38 +0100142
Harald Weltecbd18962019-03-03 19:02:38 +0100143 llist_del(&map->list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100144#ifdef REMSIM_SERVER
145 llist_del(&map->bank_list);
146#endif
Harald Weltecbd18962019-03-03 19:02:38 +0100147
148 talloc_free(map);
149}
Harald Welte294298c2019-03-07 10:08:25 +0100150/* thread-safe removal of a bank<->client map */
151void slotmap_del(struct slotmaps *maps, struct slot_mapping *map)
152{
Harald Welte648f4e32019-03-07 21:14:47 +0100153 slotmaps_wrlock(maps);
Harald Welte294298c2019-03-07 10:08:25 +0100154 _slotmap_del(maps, map);
Harald Welte648f4e32019-03-07 21:14:47 +0100155 slotmaps_unlock(maps);
Harald Welte294298c2019-03-07 10:08:25 +0100156}
Harald Weltecbd18962019-03-03 19:02:38 +0100157
Harald Weltef34fb792019-12-04 19:51:32 +0100158
159/* thread-safe removal of all bank<->client maps */
160void slotmap_del_all(struct slotmaps *maps)
161{
162 struct slot_mapping *map, *map2;
163
164 slotmaps_wrlock(maps);
165 llist_for_each_entry_safe(map, map2, &maps->mappings, list) {
166 _slotmap_del(maps, map);
167 }
168 slotmaps_unlock(maps);
169}
170
Harald Weltecbd18962019-03-03 19:02:38 +0100171struct slotmaps *slotmap_init(void *ctx)
172{
173 struct slotmaps *sm = talloc_zero(ctx, struct slotmaps);
174
175 INIT_LLIST_HEAD(&sm->mappings);
176 pthread_rwlock_init(&sm->rwlock, NULL);
177
178 return sm;
179}
Harald Welte91a0a4a2019-03-03 20:56:06 +0100180
181#ifdef REMSIM_SERVER
182
Harald Welte4b676bc2019-03-07 21:01:38 +0100183void _Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
184 struct llist_head *new_bank_list, const char *file, int line)
Harald Welte91a0a4a2019-03-03 20:56:06 +0100185{
186 char mapname[64];
187
Harald Welte4b676bc2019-03-07 21:01:38 +0100188 LOGPSRC(DMAIN, LOGL_INFO, file, line, "Slot Map %s state change: %s -> %s\n",
189 slotmap_name(mapname, sizeof(mapname), map),
Harald Welte91a0a4a2019-03-03 20:56:06 +0100190 get_value_string(slot_map_state_name, map->state),
191 get_value_string(slot_map_state_name, new_state));
192
193 map->state = new_state;
Harald Welte91a0a4a2019-03-03 20:56:06 +0100194 llist_del(&map->bank_list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100195 if (new_bank_list)
196 llist_add_tail(&map->bank_list, new_bank_list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100197 else
198 INIT_LLIST_HEAD(&map->bank_list);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100199}
200
201
Harald Welte4b676bc2019-03-07 21:01:38 +0100202void Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state,
203 struct llist_head *new_bank_list, const char *file, int line)
Harald Welte91a0a4a2019-03-03 20:56:06 +0100204{
Harald Welte648f4e32019-03-07 21:14:47 +0100205 slotmaps_wrlock(map->maps);
Harald Welte4b676bc2019-03-07 21:01:38 +0100206 _Slotmap_state_change(map, new_state, new_bank_list, file, line);
Harald Welte648f4e32019-03-07 21:14:47 +0100207 slotmaps_unlock(map->maps);
Harald Welte91a0a4a2019-03-03 20:56:06 +0100208}
209
210#endif