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 | * |
Harald Welte | 3dcdd20 | 2019-03-09 13:06:46 +0100 | [diff] [blame] | 17 | */ |
| 18 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 19 | |
| 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 30 | #include <osmocom/core/utils.h> |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 31 | |
| 32 | #include "slotmap.h" |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 33 | #include "debug.h" |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 34 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 35 | const struct value_string slot_map_state_name[] = { |
| 36 | { SLMAP_S_NEW, "NEW" }, |
| 37 | { SLMAP_S_UNACKNOWLEDGED, "UNACKNOWLEDGED" }, |
| 38 | { SLMAP_S_ACTIVE, "ACTIVE" }, |
Harald Welte | 2bf39e0 | 2019-03-06 16:05:20 +0100 | [diff] [blame] | 39 | { SLMAP_S_DELETE_REQ, "DELETE_REQ" }, |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 40 | { SLMAP_S_DELETING, "DELETING" }, |
| 41 | { 0, NULL } |
| 42 | }; |
| 43 | |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 44 | const 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 Welte | c865683 | 2019-03-06 16:05:42 +0100 | [diff] [blame] | 51 | uint32_t slotmap_get_id(const struct slot_mapping *map) |
| 52 | { |
| 53 | return (map->bank.bank_id << 16) | map->bank.slot_nr; |
| 54 | } |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 55 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 56 | /* thread-safe lookup of map by client:slot */ |
| 57 | struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client) |
| 58 | { |
| 59 | struct slot_mapping *map; |
| 60 | |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 61 | slotmaps_rdlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 62 | llist_for_each_entry(map, &maps->mappings, list) { |
| 63 | if (client_slot_equals(&map->client, client)) { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 64 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 65 | return map; |
| 66 | } |
| 67 | } |
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 NULL; |
| 70 | } |
| 71 | |
| 72 | /* thread-safe lookup of map by bank:slot */ |
| 73 | struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank) |
| 74 | { |
| 75 | struct slot_mapping *map; |
| 76 | |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 77 | slotmaps_rdlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 78 | llist_for_each_entry(map, &maps->mappings, list) { |
| 79 | if (bank_slot_equals(&map->bank, bank)) { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 80 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 81 | return map; |
| 82 | } |
| 83 | } |
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 NULL; |
| 86 | |
| 87 | } |
| 88 | |
| 89 | /* thread-safe creating of a new bank<->client map */ |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 90 | struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, |
| 91 | const struct client_slot *client) |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 92 | { |
| 93 | struct slot_mapping *map; |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 94 | char mapname[64]; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 95 | |
| 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 Welte | a9d7ad1 | 2021-12-08 21:09:12 +0100 | [diff] [blame] | 102 | LOGP(DSLOTMAP, LOGL_ERROR, "BANKD %u:%u already in use, cannot add new map\n", |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 103 | bank->bank_id, bank->slot_nr); |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 104 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | map = slotmap_by_client(maps, client); |
| 108 | if (map) { |
Harald Welte | a9d7ad1 | 2021-12-08 21:09:12 +0100 | [diff] [blame] | 109 | LOGP(DSLOTMAP, LOGL_ERROR, "CLIENT %u:%u already in use, cannot add new map\n", |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 110 | client->client_id, client->slot_nr); |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 111 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* allocate new mapping and add to list of mappings */ |
| 115 | map = talloc_zero(maps, struct slot_mapping); |
| 116 | if (!map) |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 117 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 118 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 119 | map->maps = maps; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 120 | map->bank = *bank; |
| 121 | map->client = *client; |
| 122 | |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 123 | slotmaps_wrlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 124 | llist_add_tail(&map->list, &maps->mappings); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 125 | #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 Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 129 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 130 | |
Harald Welte | a9d7ad1 | 2021-12-08 21:09:12 +0100 | [diff] [blame] | 131 | LOGP(DSLOTMAP, LOGL_INFO, "Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map)); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 132 | |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 133 | return map; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* thread-safe removal of a bank<->client map */ |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 137 | void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map) |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 138 | { |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 139 | char mapname[64]; |
| 140 | |
Harald Welte | a9d7ad1 | 2021-12-08 21:09:12 +0100 | [diff] [blame] | 141 | LOGP(DSLOTMAP, LOGL_INFO, "Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map)); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 142 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 143 | llist_del(&map->list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 144 | #ifdef REMSIM_SERVER |
| 145 | llist_del(&map->bank_list); |
| 146 | #endif |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 147 | |
| 148 | talloc_free(map); |
| 149 | } |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 150 | /* thread-safe removal of a bank<->client map */ |
| 151 | void slotmap_del(struct slotmaps *maps, struct slot_mapping *map) |
| 152 | { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 153 | slotmaps_wrlock(maps); |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 154 | _slotmap_del(maps, map); |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 155 | slotmaps_unlock(maps); |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 156 | } |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 157 | |
Harald Welte | f34fb79 | 2019-12-04 19:51:32 +0100 | [diff] [blame] | 158 | |
| 159 | /* thread-safe removal of all bank<->client maps */ |
| 160 | void 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 Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 171 | struct 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 180 | |
| 181 | #ifdef REMSIM_SERVER |
| 182 | |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 183 | void _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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 185 | { |
| 186 | char mapname[64]; |
| 187 | |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 188 | LOGPSRC(DMAIN, LOGL_INFO, file, line, "Slot Map %s state change: %s -> %s\n", |
| 189 | slotmap_name(mapname, sizeof(mapname), map), |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 190 | 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 194 | llist_del(&map->bank_list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 195 | if (new_bank_list) |
| 196 | llist_add_tail(&map->bank_list, new_bank_list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 197 | else |
| 198 | INIT_LLIST_HEAD(&map->bank_list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 202 | void 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 204 | { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 205 | slotmaps_wrlock(map->maps); |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 206 | _Slotmap_state_change(map, new_state, new_bank_list, file, line); |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 207 | slotmaps_unlock(map->maps); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | #endif |