Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 1 | /* (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 Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 23 | |
| 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 34 | #include <osmocom/core/utils.h> |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 35 | |
| 36 | #include "slotmap.h" |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 37 | #include "debug.h" |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 38 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 39 | const struct value_string slot_map_state_name[] = { |
| 40 | { SLMAP_S_NEW, "NEW" }, |
| 41 | { SLMAP_S_UNACKNOWLEDGED, "UNACKNOWLEDGED" }, |
| 42 | { SLMAP_S_ACTIVE, "ACTIVE" }, |
Harald Welte | 2bf39e0 | 2019-03-06 16:05:20 +0100 | [diff] [blame] | 43 | { SLMAP_S_DELETE_REQ, "DELETE_REQ" }, |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 44 | { SLMAP_S_DELETING, "DELETING" }, |
| 45 | { 0, NULL } |
| 46 | }; |
| 47 | |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 48 | const 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 Welte | c865683 | 2019-03-06 16:05:42 +0100 | [diff] [blame] | 55 | uint32_t slotmap_get_id(const struct slot_mapping *map) |
| 56 | { |
| 57 | return (map->bank.bank_id << 16) | map->bank.slot_nr; |
| 58 | } |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 59 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 60 | /* thread-safe lookup of map by client:slot */ |
| 61 | struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client) |
| 62 | { |
| 63 | struct slot_mapping *map; |
| 64 | |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 65 | slotmaps_rdlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 66 | llist_for_each_entry(map, &maps->mappings, list) { |
| 67 | if (client_slot_equals(&map->client, client)) { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 68 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 69 | return map; |
| 70 | } |
| 71 | } |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 72 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | /* thread-safe lookup of map by bank:slot */ |
| 77 | struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank) |
| 78 | { |
| 79 | struct slot_mapping *map; |
| 80 | |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 81 | slotmaps_rdlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 82 | llist_for_each_entry(map, &maps->mappings, list) { |
| 83 | if (bank_slot_equals(&map->bank, bank)) { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 84 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 85 | return map; |
| 86 | } |
| 87 | } |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 88 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 89 | return NULL; |
| 90 | |
| 91 | } |
| 92 | |
| 93 | /* thread-safe creating of a new bank<->client map */ |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 94 | struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, |
| 95 | const struct client_slot *client) |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 96 | { |
| 97 | struct slot_mapping *map; |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 98 | char mapname[64]; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 99 | |
| 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) { |
| 106 | fprintf(stderr, "BANKD %u:%u already in use, cannot add new map\n", |
| 107 | bank->bank_id, bank->slot_nr); |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 108 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | map = slotmap_by_client(maps, client); |
| 112 | if (map) { |
| 113 | fprintf(stderr, "CLIENT %u:%u already in use, cannot add new map\n", |
| 114 | client->client_id, client->slot_nr); |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 115 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* allocate new mapping and add to list of mappings */ |
| 119 | map = talloc_zero(maps, struct slot_mapping); |
| 120 | if (!map) |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 121 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 122 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 123 | map->maps = maps; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 124 | map->bank = *bank; |
| 125 | map->client = *client; |
| 126 | |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 127 | slotmaps_wrlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 128 | llist_add_tail(&map->list, &maps->mappings); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 129 | #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 Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 133 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 134 | |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 135 | printf("Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map)); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 136 | |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 137 | return map; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* thread-safe removal of a bank<->client map */ |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 141 | void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map) |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 142 | { |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 143 | char mapname[64]; |
| 144 | |
| 145 | printf("Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map)); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 146 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 147 | llist_del(&map->list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 148 | #ifdef REMSIM_SERVER |
| 149 | llist_del(&map->bank_list); |
| 150 | #endif |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 151 | |
| 152 | talloc_free(map); |
| 153 | } |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 154 | /* thread-safe removal of a bank<->client map */ |
| 155 | void slotmap_del(struct slotmaps *maps, struct slot_mapping *map) |
| 156 | { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 157 | slotmaps_wrlock(maps); |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 158 | _slotmap_del(maps, map); |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 159 | slotmaps_unlock(maps); |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 160 | } |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 161 | |
| 162 | struct slotmaps *slotmap_init(void *ctx) |
| 163 | { |
| 164 | struct slotmaps *sm = talloc_zero(ctx, struct slotmaps); |
| 165 | |
| 166 | INIT_LLIST_HEAD(&sm->mappings); |
| 167 | pthread_rwlock_init(&sm->rwlock, NULL); |
| 168 | |
| 169 | return sm; |
| 170 | } |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 171 | |
| 172 | #ifdef REMSIM_SERVER |
| 173 | |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 174 | void _Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state, |
| 175 | struct llist_head *new_bank_list, const char *file, int line) |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 176 | { |
| 177 | char mapname[64]; |
| 178 | |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 179 | LOGPSRC(DMAIN, LOGL_INFO, file, line, "Slot Map %s state change: %s -> %s\n", |
| 180 | slotmap_name(mapname, sizeof(mapname), map), |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 181 | get_value_string(slot_map_state_name, map->state), |
| 182 | get_value_string(slot_map_state_name, new_state)); |
| 183 | |
| 184 | map->state = new_state; |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 185 | llist_del(&map->bank_list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 186 | if (new_bank_list) |
| 187 | llist_add_tail(&map->bank_list, new_bank_list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 188 | else |
| 189 | INIT_LLIST_HEAD(&map->bank_list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 193 | void Slotmap_state_change(struct slot_mapping *map, enum slot_mapping_state new_state, |
| 194 | struct llist_head *new_bank_list, const char *file, int line) |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 195 | { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 196 | slotmaps_wrlock(map->maps); |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 197 | _Slotmap_state_change(map, new_state, new_bank_list, file, line); |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 198 | slotmaps_unlock(map->maps); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | #endif |