Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 1 | |
| 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 12 | #include <osmocom/core/utils.h> |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 13 | |
| 14 | #include "slotmap.h" |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 15 | #include "debug.h" |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 16 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 17 | const struct value_string slot_map_state_name[] = { |
| 18 | { SLMAP_S_NEW, "NEW" }, |
| 19 | { SLMAP_S_UNACKNOWLEDGED, "UNACKNOWLEDGED" }, |
| 20 | { SLMAP_S_ACTIVE, "ACTIVE" }, |
Harald Welte | 2bf39e0 | 2019-03-06 16:05:20 +0100 | [diff] [blame] | 21 | { SLMAP_S_DELETE_REQ, "DELETE_REQ" }, |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 22 | { SLMAP_S_DELETING, "DELETING" }, |
| 23 | { 0, NULL } |
| 24 | }; |
| 25 | |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 26 | const 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 Welte | c865683 | 2019-03-06 16:05:42 +0100 | [diff] [blame] | 33 | uint32_t slotmap_get_id(const struct slot_mapping *map) |
| 34 | { |
| 35 | return (map->bank.bank_id << 16) | map->bank.slot_nr; |
| 36 | } |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 37 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 38 | /* thread-safe lookup of map by client:slot */ |
| 39 | struct slot_mapping *slotmap_by_client(struct slotmaps *maps, const struct client_slot *client) |
| 40 | { |
| 41 | struct slot_mapping *map; |
| 42 | |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 43 | slotmaps_rdlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 44 | llist_for_each_entry(map, &maps->mappings, list) { |
| 45 | if (client_slot_equals(&map->client, client)) { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 46 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 47 | return map; |
| 48 | } |
| 49 | } |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 50 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | /* thread-safe lookup of map by bank:slot */ |
| 55 | struct slot_mapping *slotmap_by_bank(struct slotmaps *maps, const struct bank_slot *bank) |
| 56 | { |
| 57 | struct slot_mapping *map; |
| 58 | |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 59 | slotmaps_rdlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 60 | llist_for_each_entry(map, &maps->mappings, list) { |
| 61 | if (bank_slot_equals(&map->bank, bank)) { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 62 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 63 | return map; |
| 64 | } |
| 65 | } |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 66 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 67 | return NULL; |
| 68 | |
| 69 | } |
| 70 | |
| 71 | /* thread-safe creating of a new bank<->client map */ |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 72 | struct slot_mapping *slotmap_add(struct slotmaps *maps, const struct bank_slot *bank, |
| 73 | const struct client_slot *client) |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 74 | { |
| 75 | struct slot_mapping *map; |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 76 | char mapname[64]; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 77 | |
| 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 Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 86 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 87 | } |
| 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 Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 93 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* allocate new mapping and add to list of mappings */ |
| 97 | map = talloc_zero(maps, struct slot_mapping); |
| 98 | if (!map) |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 99 | return NULL; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 100 | |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 101 | map->maps = maps; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 102 | map->bank = *bank; |
| 103 | map->client = *client; |
| 104 | |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 105 | slotmaps_wrlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 106 | llist_add_tail(&map->list, &maps->mappings); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 107 | #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 Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 111 | slotmaps_unlock(maps); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 112 | |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 113 | printf("Slot Map %s added\n", slotmap_name(mapname, sizeof(mapname), map)); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 114 | |
Harald Welte | f4e7504 | 2019-03-03 21:54:52 +0100 | [diff] [blame] | 115 | return map; |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* thread-safe removal of a bank<->client map */ |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 119 | void _slotmap_del(struct slotmaps *maps, struct slot_mapping *map) |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 120 | { |
Harald Welte | faef8f0 | 2019-03-03 20:55:22 +0100 | [diff] [blame] | 121 | char mapname[64]; |
| 122 | |
| 123 | printf("Slot Map %s deleted\n", slotmap_name(mapname, sizeof(mapname), map)); |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 124 | |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 125 | llist_del(&map->list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 126 | #ifdef REMSIM_SERVER |
| 127 | llist_del(&map->bank_list); |
| 128 | #endif |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 129 | |
| 130 | talloc_free(map); |
| 131 | } |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 132 | /* thread-safe removal of a bank<->client map */ |
| 133 | void slotmap_del(struct slotmaps *maps, struct slot_mapping *map) |
| 134 | { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 135 | slotmaps_wrlock(maps); |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 136 | _slotmap_del(maps, map); |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 137 | slotmaps_unlock(maps); |
Harald Welte | 294298c | 2019-03-07 10:08:25 +0100 | [diff] [blame] | 138 | } |
Harald Welte | cbd1896 | 2019-03-03 19:02:38 +0100 | [diff] [blame] | 139 | |
| 140 | struct 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 149 | |
| 150 | #ifdef REMSIM_SERVER |
| 151 | |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 152 | void _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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 154 | { |
| 155 | char mapname[64]; |
| 156 | |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 157 | LOGPSRC(DMAIN, LOGL_INFO, file, line, "Slot Map %s state change: %s -> %s\n", |
| 158 | slotmap_name(mapname, sizeof(mapname), map), |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 159 | 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 163 | llist_del(&map->bank_list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 164 | if (new_bank_list) |
| 165 | llist_add_tail(&map->bank_list, new_bank_list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 166 | else |
| 167 | INIT_LLIST_HEAD(&map->bank_list); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 171 | void 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 Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 173 | { |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 174 | slotmaps_wrlock(map->maps); |
Harald Welte | 4b676bc | 2019-03-07 21:01:38 +0100 | [diff] [blame] | 175 | _Slotmap_state_change(map, new_state, new_bank_list, file, line); |
Harald Welte | 648f4e3 | 2019-03-07 21:14:47 +0100 | [diff] [blame] | 176 | slotmaps_unlock(map->maps); |
Harald Welte | 91a0a4a | 2019-03-03 20:56:06 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | #endif |